Unity onClick.addlistener无法正常工作 [英] Unity onClick.addlistener not working

查看:1867
本文介绍了Unity onClick.addlistener无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当事件发生时,我会在运行时加载Canvas预制件.画布内部仅包含一个面板,而该面板又具有2个按钮.我正在尝试将OnClick事件添加到脚本中的这两个按钮,但是无论如何它仅适用于第一个按钮! 我有以下两行代码,一个接一个:

I load a Canvas prefab at runtime when an event occurs. The canvas simply has a Panel inside it, which in turn has 2 buttons. I'm trying to add OnClick events to both these buttons in the script, but it only works for the first button somehow! I have the following two lines of code one after the other:

GameObject.Find("RestartButton").GetComponent<Button>().onClick.AddListener(() => RestartClicked());
GameObject.Find("ViewButton").GetComponent<Button>().onClick.AddListener(() => ViewClicked());

回调仅适用于RestartButton,不适用于ViewButton. 这可能是一件很小的事情,但是我对Google和Bing进行了广泛搜索,但仍然一无所知,因此,我们将不胜感激.

The callback only works for the RestartButton, and not for the ViewButton. It may well be a very small thing, but I searched Google and Bing extensively and remain clueless, so any help would be appreciated.

谢谢!

该脚本实例化预制件,并尝试通过GameObject.Find()引用预制件中的两个按钮,假设一旦调用实例化,则画布应在层次结构中处于活动状态. 我还打开了将按钮附加到侦听器以进行调试的代码部分.我不认为它完全可以吸引听众.

The script instantiates the prefab and tries to reference the two buttons in the prefab through GameObject.Find(), given that once Instantiate is called the Canvas should be active in the heirarchy. I also opened up the part of the code that attaches buttons to the listener for debugging. I dont think it attaches the listener at all.

void Start () {

    SetupScene();

    var prefab = Resources.Load("RestartOrViewPrefab");
    if (prefab == null)
    {
        Debug.LogAssertion("Prefab missing!");
        return;
    }
    restartCanvas = (GameObject)Instantiate(prefab);
    Button btn = GameObject.Find("ViewButton").GetComponent<Button>();
    btn.onClick.RemoveAllListeners();
    btn.onClick.AddListener(() => ViewToggle());
    Button btn2 = GameObject.Find("RestartButton").GetComponent<Button>();
    btn2.onClick.AddListener(() => RestartClicked());
}

private void RestartClicked()
{
    Debug.Log("RESTARTING");
    SetupScene();
}

private void ViewToggle()
{
    Debug.Log("TOGGLE");
    if (freshStart)
    {
        //Something here
        freshStart = false;
    }
    else
    {
        //Something else here
        freshStart = true;
    }
}

推荐答案

所以我花了两天时间来解决自己的问题.显然,对您刚刚实例化的预制体内的GameObject进行GameObject.Find()无效.我们总是需要使用Instantiate()方法返回的GameObject来查找预制件中的任何组件. 如果您的预制件非常大/复杂(例如您有15个按钮并且需要用一个按钮执行某些操作),那么我用来使场景工作的代码可能不是最佳的解决方案,但是它确实可以工作. :) 我替换了以下代码行:

So I took two days to solve my problem. Apparently, doing a GameObject.Find() for a GameObject within a prefab that you just instantiated doesn't work. We always need to use the GameObject that the Instantiate() method returns to find any component within the prefab. The code I used to make my scene work may not be the best solution if your prefab is very big/complex (say you have 15 buttons and need to do something with one), but it sure does work. :) I replaced the following lines of code:

Button btn = GameObject.Find("ViewButton").GetComponent<Button>();
btn.onClick.RemoveAllListeners();
btn.onClick.AddListener(() => ViewToggle());
Button btn2 = GameObject.Find("RestartButton").GetComponent<Button>();
btn2.onClick.AddListener(() => RestartClicked());

使用以下代码行:

Button[] buttons = restartCanvas.GetComponentsInChildren<Button>();

    foreach(Button but in buttons)
    {
        if(but.gameObject.name == "RestartButton")
            but.onClick.AddListener(() => RestartClicked());
        else if(but.gameObject.name == "ViewButton")
            but.onClick.AddListener(() => ViewToggle());
    }

所以我只是重用了我引用的restartCanvas.

So I just reused the restartCanvas that I got a reference to.

restartCanvas = (GameObject)Instantiate(prefab);

希望这对某人有帮助. :)

Hope this helps someone. :)

这篇关于Unity onClick.addlistener无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆