gpt4 book ai didi

c# - 枚举指向游戏对象?

转载 作者:行者123 更新时间:2023-11-30 19:54:58 24 4
gpt4 key购买 nike

所以,我有了一个疯狂的想法,让枚举指向游戏对象。

这是我想做的:

/* These enums would hold gameobjects instead of ints */
enum exampleEnum{
AddPanel,
ListPanel
}

public class GUIManager : MonoBehaviour {
void Start()
{
EnablePanel(exampleEnum.AddPanel);
}

void EnablePanel(GameObject panel)
{
panel.setActive(true);
}
}

有什么办法可以实现吗?或者解决方法?

除了枚举之外,这可能是可行的,但我不知道它是否存在,我正在通过网络寻找这样的解决方案。

最佳答案

这将满足您的要求,适用于任何数量的枚举值或面板。

// Add this to each of your panels, the enum field can be integrated into your other behaviours as well
public class EnumPanel : MonoBehaviour
{
// configurable from the Editor, the unity way.
public ExampleEnum Type;
}

// Assign all your panles in the editor (or use FindObjectsByType<EnumPanel> in Start())
public class GUIManager : MonoBehaviour
{
// configurable from the Editor, the unity way.
public EnumPanel[] Panels;

void Start()
{
// Optionally find it at runtime, if assigning it via the editor is too much maintenance.
// Panels = FindObjectsByType<EnumPanel>();
EnablePanel(ExampleEnum.AddPanel);
}

void EnablePanel(ExampleEnum panelType)
{
foreach(var panel in Panels)
{
if(panel.Type == panelType)
EnablePanel(panel.gameObject);
}
}

void EnablePanel(GameObject panel)
{
panel.setActive(true);
}
}

关于c# - 枚举指向游戏对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39999702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com