需求:使用一個枚舉表示數(shù)據(jù)的種類盔夜,再使用一堆其他枚舉,表示每個數(shù)據(jù)種類對應(yīng)的堤魁,真正的種類
如下圖:大枚舉套小枚舉喂链,形成一個模擬經(jīng)營游戲中的,資源總覽列表妥泉,類似缺氧中的列表
方法一:switch硬寫,type.name == "人口"? 然后返回一個object盲链∮剩或者不用switch,直接硬寫匈仗,這樣太麻煩了
方法二:建立字典瓢剿,遍歷字典,創(chuàng)建按鈕button悠轩,再遍歷子枚舉间狂,創(chuàng)建文本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 },
? ? };
然后是函數(shù):
//遍歷枚舉,創(chuàng)建按鈕火架,并賦響應(yīng)函數(shù)鉴象,并讀數(shù)據(jù),進行初始化
? ? private void InitShowTypeButton()
? ? {
? ? ? ? //生成左側(cè)按鈕
? ? ? ? for(int i = 0; i < (int)DetailShowType._null; i++)
? ? ? ? {
? ? ? ? ? ? DetailShowType detailShowType = (DetailShowType)i;
? ? ? ? ? ? //創(chuàng)建按鈕
? ? ? ? ? ? GameObject objectToCreate = Instantiate(Prefab_Btn_ShowDetail, ShowDetail_Content);
? ? ? ? ? ? objectToCreate.name = "btn_" + detailShowType.ToString();
? ? ? ? ? ? //賦予點擊函數(shù)
? ? ? ? ? ? Button btnToCreate = objectToCreate.GetComponent<Button>();
? ? ? ? ? ? btnToCreate.onClick.AddListener(() => ShowTypeOfDetail(detailShowType));
? ? ? ? ? ? Text textToCreate = SearchObject.FindChild(objectToCreate.transform, "Text").GetComponent<Text>();
? ? ? ? ? ? textToCreate.text = detailShowType.ToString();
? ? ? ? ? ? //創(chuàng)建字符
? ? ? ? ? ? InitDetailShowTypeText(detailShowType);
? ? ? ? ? ? //觸發(fā)一次點擊函數(shù)何鸡,將其隱藏
? ? ? ? ? ? ShowTypeOfDetail(detailShowType);
? ? ? ? }
? ? }
//由上函數(shù)調(diào)用纺弊,根據(jù)左側(cè)按鈕枚舉,生成指定數(shù)量的文本
? ? private void InitDetailShowTypeText(DetailShowType detailShowType)
? ? {
? ? ? ? if (!DetailShowType_SomeEnum_Dic.ContainsKey(detailShowType))
? ? ? ? {
? ? ? ? ? ? Debug.LogWarning("初始化字典中不存在" + detailShowType + "相關(guān)的具體枚舉");
? ? ? ? ? ? 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();
? ? ? ? ? ? //拿到指定的類型的枚舉中骡男,指定序號的值(函數(shù)返回數(shù)組淆游,再進行讀取)
? ? ? ? ? ? string theEnumNow = type.GetEnumNames()[i];
? ? ? ? ? ? //創(chuàng)建按鈕
? ? ? ? ? ? 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);
? ? }
總結(jié):核心代碼為? ? ? ? ?
System.Type type = DetailShowType_SomeEnum_Dic[detailShowType].GetType();
//拿到指定的類型的枚舉中,指定序號的值(函數(shù)返回數(shù)組隔盛,再進行讀取)
string theEnumNow = type.GetEnumNames()[i];
這一段犹菱,GetEnumNames是C#的system自帶的函數(shù),以數(shù)組的形式返回一個枚舉的所有可能值吮炕。
然后在for循環(huán)中得到其序號腊脱,從而得到具體的枚舉。
備注:枚舉遍歷方法:
將枚舉定義如下龙亲,然后用for循環(huán)陕凹,將_null這一項強制轉(zhuǎn)化為int悍抑,開始遍歷
public enum DetailShowType
{
? ? 人口,
? ? 食物,
? ? 工具,
? ? 藥材,
? ? 文化,
? ? 裝飾,
? ? 娛樂,
? ? 信仰,
? ? _null,
}
public enum HumanType
{
? ? 嬰兒,
? ? 少年,
? ? 成年,
? ? 老年,
? ? _null,
}