美文网首页
四。Unity枚举套枚举:模拟经营资源列表的实现方法

四。Unity枚举套枚举:模拟经营资源列表的实现方法

作者: 熬夜Plus | 来源:发表于2019-10-25 21:41 被阅读0次

    需求:使用一个枚举表示数据的种类,再使用一堆其他枚举,表示每个数据种类对应的,真正的种类

    如下图:大枚举套小枚举,形成一个模拟经营游戏中的,资源总览列表,类似缺氧中的列表

    每个大类中有好多个小类,用中文枚举定义

    方法一:switch硬写,type.name == "人口"  然后返回一个object。或者不用switch,直接硬写,这样太麻烦了

    方法二:建立字典,遍历字典,创建按钮button,再遍历子枚举,创建文本text

    初始字典如下:将具体的枚举装箱为object


    private Dictionary<DetailShowType, List<Text>> textList_DetailShowType_Dic = new Dictionary<DetailShowType, List<Text>>();

    public Dictionary<DetailShowType, object> DetailShowType_SomeEnum_Dic = new Dictionary<DetailShowType, object>()

        {

            { DetailShowType.人口 , HumanType._null },

            { DetailShowType.食物 , FoodType._null },

        };


    然后是函数:

    //遍历枚举,创建按钮,并赋响应函数,并读数据,进行初始化

        private void InitShowTypeButton()

        {

            //生成左侧按钮

            for(int i = 0; i < (int)DetailShowType._null; i++)

            {

                DetailShowType detailShowType = (DetailShowType)i;

                //创建按钮

                GameObject objectToCreate = Instantiate(Prefab_Btn_ShowDetail, ShowDetail_Content);

                objectToCreate.name = "btn_" + detailShowType.ToString();

                //赋予点击函数

                Button btnToCreate = objectToCreate.GetComponent<Button>();

                btnToCreate.onClick.AddListener(() => ShowTypeOfDetail(detailShowType));

                Text textToCreate = SearchObject.FindChild(objectToCreate.transform, "Text").GetComponent<Text>();

                textToCreate.text = detailShowType.ToString();

                //创建字符

                InitDetailShowTypeText(detailShowType);

                //触发一次点击函数,将其隐藏

                ShowTypeOfDetail(detailShowType);

            }

        }


    //由上函数调用,根据左侧按钮枚举,生成指定数量的文本

        private void InitDetailShowTypeText(DetailShowType detailShowType)

        {

            if (!DetailShowType_SomeEnum_Dic.ContainsKey(detailShowType))

            {

                Debug.LogWarning("初始化字典中不存在" + detailShowType + "相关的具体枚举");

                return;

            }

            List<Text> textsForTheDetail = new List<Text>();

            for (int i = 0; i < (int)DetailShowType_SomeEnum_Dic[detailShowType]; i++)

            {

                System.Type type = DetailShowType_SomeEnum_Dic[detailShowType].GetType();

                //拿到指定的类型的枚举中,指定序号的值(函数返回数组,再进行读取)

                string theEnumNow = type.GetEnumNames()[i];

                //创建按钮

                GameObject objectToCreate = Instantiate(Prefab_Text_ShowDetail, ShowDetail_Content);

                objectToCreate.name = "text_" + detailShowType.ToString() + theEnumNow;

                Text textToCreate = objectToCreate.GetComponent<Text>();

                textToCreate.text = theEnumNow + ":";

                int index = i;

                index_Text_ForItsEnum_Dic.Add(textToCreate, index);

                textsForTheDetail.Add(textToCreate);

            }

            textList_DetailShowType_Dic.Add(detailShowType, textsForTheDetail);

        }


    总结:核心代码为         

    System.Type type = DetailShowType_SomeEnum_Dic[detailShowType].GetType();

    //拿到指定的类型的枚举中,指定序号的值(函数返回数组,再进行读取)

    string theEnumNow = type.GetEnumNames()[i];

    这一段,GetEnumNames是C#的system自带的函数,以数组的形式返回一个枚举的所有可能值。

    然后在for循环中得到其序号,从而得到具体的枚举。


    备注:枚举遍历方法:

    将枚举定义如下,然后用for循环,将_null这一项强制转化为int,开始遍历


    public enum DetailShowType

    {

        人口,

        食物,

        工具,

        药材,

        文化,

        装饰,

        娱乐,

        信仰,

        _null,

    }


    public enum HumanType

    {

        婴儿,

        少年,

        成年,

        老年,

        _null,

    }

    相关文章

      网友评论

          本文标题:四。Unity枚举套枚举:模拟经营资源列表的实现方法

          本文链接:https://www.haomeiwen.com/subject/ievevctx.html