使用 C# 统一显示隐藏按钮

show hide button in unity using c#

我的屏幕上有 15 个按钮。 在 onClick 事件中,我正在获取按钮并使用

隐藏该按钮
EventSystem.current.currentSelectedGameObject.SetActive(false);

最后我不得不再次显示按钮,所以我使用

for (int i = 0; i < 15; i++)
{
    tag1 = "Button" + (i + 1);
    GameObject.FindGameObjectWithTag(tag1).SetActive(true);
    Debug.Log("done");
}

循环给出错误,因为它无法找到已隐藏的对象或 setActive(false) 从其他参考资料 (unity forum) 我看到了相同的解决方案,但我不知道为什么它在我的情况下不起作用。

由于您想通过标签查找对象,而使用方法FindGameObjectWithTag 无法找到隐藏的元素。你可以使用这个方法:

var allObjects = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[]; // this will grab all GameObjects from the current scene!
foreach(GameObejct obj in allObjects) {
    if(new Regex(@"^Button(?'number'\d{1,2})").IsMatch(obj.Tag)) {
        obj.SetActive(true);
        Debug.Log("done");
    }
}