/// <summary> /// 新手引导获得动态创建Item 格式为:PetItem[0].Use(PetItem的Item上挂有UIBase脚本, [0] 第几个Item,Use:拖到PetItem上的GameObject) /// </summary> /// <param name="itemName"></param> /// <returns></returns> public GameObject GetGuideDynamicCreateItem(string itemName) { string firstName = ""; string[] strArr = itemName.Split('.'); //Debug.Log("itemName :" + itemName); string childName = ""; GameObject obj = null; if (strArr.Length > 0) { UIBase uIBase = null; firstName = strArr[0]; int index = int.Parse(firstName.SplitExtend("[", "]")[0]); int tempIndex0 = firstName.IndexOf("["); firstName = firstName.Replace(firstName.Substring(tempIndex0), ""); //Debug.Log("UIBase : Index :" + index + " firstName :" + firstName + " m_ChildList:"+ m_ChildList.Count); int tempIndex = 0; for (int i = 0; i < m_ChildList.Count; i++) { UIBase cItem = m_ChildList[i]; //Debug.Log("Item:" + cItem.name); if (cItem.name == firstName) { if (index == tempIndex) { uIBase = cItem; obj = uIBase.gameObject; break; } tempIndex++; } } if (strArr.Length > 1) { childName = strArr[1]; if (childName.Contains("[")) { childName = itemName.Replace(firstName + ".", ""); obj = uIBase.GetGuideDynamicCreateItem(childName); } else { obj = uIBase.GetGameObject(childName); } } } if (obj == null) { Debug.LogError("GetGuideDynamicCreateItem error :UIEventKey " + UIEventKey + "itemName " + itemName); } return(obj); }
/// <summary> /// 新手引导获得动态创建Item 格式为:PetItem[0].Use(PetItem的Item上挂有UIBase脚本, [0] 该名字的第几个Item,Use:拖到PetItem上的GameObject),当index小于0,则表示动态创建的列表List.Count-index(如-1:表示List.Count-1) /// </summary> /// <param name="itemName"></param> /// <returns></returns> public GameObject GetGuideDynamicCreateItem(string itemName) { if (string.IsNullOrEmpty(itemName)) { Debug.LogError("GetGuideDynamicCreateItem itemName is Null!! "); return(null); } string firstName = ""; string[] strArr = itemName.Split('.'); Debug.Log("GetGuideDynamicCreateItem.itemName :" + itemName); string childName = ""; GameObject obj = null; if (strArr.Length > 0) { UIBase uIBase = null; firstName = strArr[0]; //寻找继承UIbase的变量 if (!firstName.Contains("[")) { //FieldInfo fieldInfo = GetType().GetField(firstName, BindingFlags.Public|BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); // UIBase data= (UIBase)fieldInfo.GetValue(this); UIBase data = GetGameObject(firstName).GetComponent <UIBase>(); itemName = itemName.Replace(firstName + ".", ""); return(data.GetGuideDynamicCreateItem(itemName)); } else { int index = int.Parse(firstName.SplitExtend("[", "]")[0]); if (index < 0) { index = m_ChildList.Count + index; } int tempIndex0 = firstName.IndexOf("["); firstName = firstName.Replace(firstName.Substring(tempIndex0), ""); Debug.Log("UIBase : Index :" + index + " firstName :" + firstName + " m_ChildList:" + m_ChildList.Count); int tempIndex = 0; for (int i = 0; i < m_ChildList.Count; i++) { UIBase cItem = m_ChildList[i]; Debug.Log("Item:" + cItem); if (cItem.name == firstName) { if (index == tempIndex) { uIBase = cItem; obj = uIBase.gameObject; break; } tempIndex++; } } if (strArr.Length > 1) { childName = strArr[1]; Debug.Log("childName:" + childName); if (childName.Contains("[")) { childName = itemName.Replace(strArr[0] + ".", ""); Debug.Log("childName:" + childName); obj = uIBase.GetGuideDynamicCreateItem(childName); } else { string afterNames = itemName.Replace(strArr[0] + ".", ""); strArr = afterNames.Split('.'); Debug.Log("afterNames :" + afterNames + " UIBase:" + GetType().Name); for (int i = 0; i < strArr.Length; i++) { string findName = strArr[i]; obj = uIBase.GetGameObject(findName); if (i < strArr[i].Length - 1) { uIBase = obj.GetComponent <UIBase>(); } } } } } } if (obj == null) { Debug.LogError("GetGuideDynamicCreateItem error :UIEventKey " + UIEventKey + "itemName " + itemName); } return(obj); }