按标签禁用一组按钮的Interactable状态

时间:2015-07-17 22:02:54

标签: c# user-interface unity3d

我的游戏区域有两组按钮,我想在它们之间切换。我目前每个组在编辑器中都有不同的标记,我有另一个按钮,我打算用它作为切换来交换哪些按钮可以与之交互。这是我到目前为止所得到的,但没有运气。我在IsInteractable行上收到错误:方法IsInteractable' takes 1'参数没有重载。

我哪里错了?

    if(keySet == true)
    {
        // turn ON Interactable for Alpha buttons
        for(int i = 0; i > GameSetup.alphaKeys.Length; i++)
        {
            GameObject alphaButton = GameObject.FindGameObjectWithTag("AlphaKey");
            alphaButton.GetComponent<UnityEngine.UI.Button>().IsInteractable(true);
        }

        // turn OFF Interactable for Shape buttons
        for(int i = 0; i > GameSetup.symbolKeys.Length; i++)
        {
            GameObject alphaButton = GameObject.FindGameObjectWithTag("SymbolKey");
            alphaButton.GetComponent<UnityEngine.UI.Button>().IsInteractable(false);
        }

    }
    else
    {
        // Do the reverse if false
    }

1 个答案:

答案 0 :(得分:1)

知道了!我为每组按钮添加了一个Canvas Group组件,这使得控制我需要访问的所有属性变得非常容易。

public void HandleActiveKeySet (bool keySet)
{
    if(keySet == true)
    {
        GameObject alphaGroup = GameObject.FindGameObjectWithTag("AlphaGroup");
        alphaGroup.GetComponent<CanvasGroup>().interactable = true;
        alphaGroup.GetComponent<CanvasGroup>().blocksRaycasts = true;

        GameObject symbolGroup = GameObject.FindGameObjectWithTag("SymbolGroup");
        symbolGroup.GetComponent<CanvasGroup>().interactable = false;
        symbolGroup.GetComponent<CanvasGroup>().blocksRaycasts = false;
    }
    else
    {
        GameObject alphaGroup = GameObject.FindGameObjectWithTag("AlphaGroup");
        alphaGroup.GetComponent<CanvasGroup>().interactable = false;
        alphaGroup.GetComponent<CanvasGroup>().blocksRaycasts = false;

        GameObject symbolGroup = GameObject.FindGameObjectWithTag("SymbolGroup");
        symbolGroup.GetComponent<CanvasGroup>().interactable = true;
        symbolGroup.GetComponent<CanvasGroup>().blocksRaycasts = true;
    }
}