public static void AttachComponentDict() { GameObject[] selectedObjs = Selection.gameObjects; for (int i = 0; i < selectedObjs.Length; i++) { GameObject curObj = selectedObjs[i]; PrefabType prefabType = PrefabUtility.GetPrefabType(curObj); if (prefabType == PrefabType.Prefab || prefabType == PrefabType.ModelPrefab) { Debug.LogWarning("Please select GameObject in Hierarchy view: " + curObj.name); continue; } UIComponentDict componentDict = curObj.GetComponent <UIComponentDict>(); if (componentDict == null) { componentDict = curObj.AddComponent <UIComponentDict>(); } componentDict.m_refreshNow = true; EditorUtility.SetDirty(curObj); } }
void DoRefresh(bool recursive) { if (m_refreshNow) { m_refreshNow = false; } m_nodeList.Clear(); m_panelList.Clear(); Transform[] transes = GetComponentsInChildren <Transform>(true); foreach (Transform trans in transes) { GameObject go = trans.gameObject; //if (go == this.gameObject) continue; UIComponentDict dict = (trans.parent != null) ? FindInParents <UIComponentDict>(trans.parent) : FindInParents <UIComponentDict>(trans); //if (go.GetComponent<UIComponentDict>() == null // && dict != this) if (go != this.gameObject && dict != this) { continue; } string hierarchy = UIComponentDict.GetHierarchy(trans, CachedTrans); m_nodeList.Add(new Node() { m_name = go.name, m_hierarchy = hierarchy, m_trans = trans }); } m_nodeList.Sort((x, y) => { return(string.Compare(x.m_name, y.m_name)); }); if (recursive) { UIComponentDict[] dicts = GetComponentsInChildren <UIComponentDict>(true); foreach (UIComponentDict dict in dicts) { if (dict != this) { dict.DoRefresh(false); } } m_panelList.AddRange(GetComponentsInChildren <UIPanel>(true)); } }
public static bool InitComponents(GameObject gameObj, object target) { if (gameObj == null || target == null) { return(false); } UIComponentDict comDict = gameObj.GetComponent <UIComponentDict>(); if (comDict == null) { return(true); } bool allFieldsReady = true; Type controllerType = target.GetType(); var fields = controllerType.GetFields( System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); foreach (var field in fields) { var attrs = field.GetCustomAttributes(typeof(UIComponentAttribute), false); if (attrs == null || attrs.Length != 1) { continue; } var attr = attrs[0] as UIComponentAttribute; Transform trans = attr.KeyType == E_KeyType.Name ? comDict.GetTransByName(attr.Key) : comDict.GetTransByHierarchy(attr.Key); if (trans == null) { if (attr.IsRequired) { Debug.LogWarning(attr.Key + ":" + attr.KeyType + " not found."); allFieldsReady = false; } continue; } Type fieldType = field.FieldType; if (fieldType.Equals(typeof(Transform))) { field.SetValue(target, trans); } else if (fieldType.Equals(typeof(GameObject))) { field.SetValue(target, trans.gameObject); } else if (fieldType.IsSubclassOf(typeof(Component))) { Component comp = trans.GetComponent(fieldType); if (comp != null) { field.SetValue(target, comp); } else { Debug.LogWarning(attr.Key + ":" + attr.KeyType + " not found."); if (attr.IsRequired) { allFieldsReady = false; } } } else { Debug.LogWarning(attr.Key + ":" + attr.KeyType + " not found."); if (attr.IsRequired) { allFieldsReady = false; } } } return(allFieldsReady); }