【问题标题】:Enum pointing to gameobjects?枚举指向游戏对象?
【发布时间】:2017-02-21 07:41:28
【问题描述】:

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

这是我想做的:

/* 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);
    }
}

有什么办法可以使这个工作吗?还是解决方法?

使用枚举以外的其他方法可能会实现这一点,但我不知道是否存在,我正在网上寻找这样的解决方案。

【问题讨论】:

  • 使用开关盒。
  • 不幸的是,我不想要一个巨大的开关来装一堆箱子。
  • 即使我有 500 个面板要激活,我也希望它能够正常工作并保持清洁。
  • 根据经验,这样做不会有太多收获,而且生成的代码可能很脆弱。
  • @Amy 所以有可能吗?它怎么会脆弱?对于我打算做的事情,这个想法似乎是最灵活的方式。

标签: c# unity3d enums gameobject


【解决方案1】:

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

// 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);
    }
}

【讨论】:

  • @Alox 这就是您要找的东西吗?如果是这样,请标记答案,以便将其从未回答部分中删除。
  • 抱歉,我正在开会,看起来像我要找的东西,我会试一试以确定,但只需查看代码,它看起来就是一个很好的答案。
  • 请注意,如果您使用数组以外的任何东西(例如列表),请不要使用foreach 来迭代面板,使用for 它可以节省垃圾收集(IEnumerator 为 40 个字节),从而为您提供稍高的 FPS,并且只花费 1 行额外的代码。为了清楚起见,我没有在上面的代码示例中添加它。
  • 只需要解决 OP 的答案,即如何将 Enums 绑定到对象,而不是管理器范围背后的技术细节。我们不知道他项目的技术规范,所以你怎么判断他的经理是否应该执行逻辑,请不要在答案中添加离题讨论。
【解决方案2】:

我不知道为什么来自@Paradox Forge 的答案是错误的,但也许这会对你有所帮助。

System.Collections.Generic.Dictionary

我没有太多时间来解释字典类,但这就是你可以使用它的方式。 这会消耗一些性能,但具有非常好的可读性

    public class GUIManager : MonoBehaviour {
        public enum exampleEnum{
            AddPanel,
            ListPanel
        }

        //For readability you can also add "using System.Collections.Generic;" on the top of your script
        private System.Collections.Generic.Dictionary<exampleEnum,GameObject> exampleDictionary = new System.Collections.Generic.Dictionary<exampleEnum, GameObject>();

        private GameObject SomeGameObject;
        private GameObject SomeOtherGameObject;

        void Start()
        {
            //You have to add all the enums and objects you want to use inside your GUIManager.
            exampleDictionary.Add (exampleEnum.AddPanel, SomeGameObject); //Add panel will be linked to SomeGameObject
            exampleDictionary.Add (exampleEnum.ListPanel, SomeOtherGameObject); //List Panel will be linked to SomeOtherGameObject

            EnablePanel(exampleEnum.AddPanel);
        }

        void EnablePanel(exampleEnum examplePanel)
        {
            if (!exampleDictionary.ContainsKey (examplePanel)) //If the given panel does not exist inside the dictionary
                return; //Leave the method

            GameObject panelToEnable = exampleDictionary [examplePanel]; //Will return the GameObject linked to given panel
            panelToEnable.SetActive(true); //Enable the gameobject
        }
    }

如果您想了解有关 Dictionary 类的更多信息,请访问:Dictionary

【讨论】:

  • 谢谢!我只听说过字典,但我不知道如何使用它们,这也是我问题的一个可能答案,但不幸的是我不能有两个答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-02
  • 1970-01-01
  • 2015-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
相关资源
最近更新 更多