public void DestroyChildTemp() { List <GameObject> list = GetAsComponentObj(gameObject, "UITemplateChild"); foreach (GameObject cGo in list) { UITemplateChild temChild = cGo.GetComponent <UITemplateChild>(); if (temChild.m_PartGUID == m_GUID) { DestroyImmediate(temChild); } } DestroyImmediate(gameObject.GetComponent <UITemplate>()); }
static private void CreatPrefab(GameObject prefab) { if (!prefab.GetComponent <UITemplate>()) { prefab.AddComponent <UITemplate>().InitGUID(TEMPLATE_PREFAB_PATH); } int prefabGuid = prefab.GetComponent <UITemplate>().m_GUID; List <GameObject> childList = new List <GameObject>(); new EditTools().GetChild(prefab, childList); foreach (GameObject childGo in childList) { UITemplate tem = childGo.GetComponent <UITemplate>(); UITemplateChild temc = childGo.GetComponent <UITemplateChild>(); if (tem || temc) { continue; } childGo.AddComponent <UITemplateChild>().InitGUID(prefabGuid); } PrefabUtility.CreatePrefab(TEMPLATE_PREFAB_PATH + "/" + prefab.name + ".prefab", prefab); Refresh(); }
/// <summary> /// 将目标label的值复制到目标label /// 并且由子模板的身份变成父模板 /// </summary> /// <param name="sourceGo">要被复制属性的目标uilabel对象</param> public static void CopyTemplateLabelValue(GameObject sourceGo) { UILabel sourceLab = sourceGo.GetComponent <UILabel>(); UITemplate sourceTem = sourceGo.GetComponent <UITemplate>(); if (!sourceLab) { return; } UnityEngine.Object[] selectionAsset = Selection.GetFiltered(typeof(GameObject), SelectionMode.TopLevel); //获取当前鼠标选中的所有对象 foreach (GameObject sGo in selectionAsset) { UILabel lab = sGo.GetComponent <UILabel>(); if (lab) { CopyComponent(sourceLab, lab); UITemplate tem = sGo.GetComponent <UITemplate>(); UITemplateChild temc = sGo.GetComponent <UITemplateChild>(); if (tem) { } else if (temc) { DestroyImmediate(temc); tem = sGo.AddComponent <UITemplate>(); //由子模板的身份变成父模板 } else { tem = sGo.AddComponent <UITemplate>(); } EditorUtility.CopySerialized(sourceTem, tem); EditorUtility.SetDirty(lab); } } }
static private void ApplyPrefab(GameObject prefab, Object targetPrefab, bool replace) { if (EditorUtility.DisplayDialog("注意!", "是否进行递归查找批量替换模板?", "ok", "cancel")) { GameObject replacePrefab; //int count = 0; if (replace) { PrefabUtility.ReplacePrefab(prefab, targetPrefab, ReplacePrefabOptions.ConnectToPrefab); Refresh(); replacePrefab = targetPrefab as GameObject; //count = prefab.GetComponentsInChildren<UITemplate>().Length; } else { replacePrefab = AssetDatabase.LoadAssetAtPath(AssetDatabase.GetAssetPath(targetPrefab), typeof(GameObject)) as GameObject;; //GameObject checkPrefab = PrefabUtility.InstantiatePrefab(replacePrefab) as GameObject; //count = checkPrefab.GetComponentsInChildren<UITemplate>().Length; //DestroyImmediate(checkPrefab); } //if (count != 1) //{ // EditorUtility.DisplayDialog("注意!", "无法批量替换,因为模板不支持嵌套。", "ok"); // return; //} UITemplate template = replacePrefab.GetComponent <UITemplate>(); if (template != null) { List <GameObject> references; if (TrySearchPrefab(template.m_GUID, out references)) { GameObject checkPrefab = PrefabUtility.InstantiatePrefab(replacePrefab) as GameObject; for (int i = 0; i < references.Count; i++) { GameObject reference = references[i]; GameObject go = PrefabUtility.InstantiatePrefab(reference) as GameObject; UITemplate[] instanceTemplates = go.GetComponentsInChildren <UITemplate>(); for (int j = 0; j < instanceTemplates.Length; j++) { UITemplate instance = instanceTemplates[j]; if (instance.m_GUID == template.m_GUID) { EditTools.CopyComponents(template.gameObject, instance.gameObject); UITemplateChild[] instanceTemplatesChild = go.GetComponentsInChildren <UITemplateChild>(); UITemplateChild[] templateTemplatesChild = checkPrefab.gameObject.GetComponentsInChildren <UITemplateChild>(); for (int x = 0; x < templateTemplatesChild.Length; x++) { UITemplateChild templateChild = templateTemplatesChild[x]; for (int y = 0; y < instanceTemplatesChild.Length; y++) { UITemplateChild instanceChild = instanceTemplatesChild[y]; if (instanceChild.m_GUID == templateChild.m_GUID) { EditTools.CopyComponents(templateChild.gameObject, instanceChild.gameObject); } } } } PrefabUtility.ReplacePrefab(go, PrefabUtility.GetPrefabParent(go), ReplacePrefabOptions.ConnectToPrefab); } DestroyImmediate(go); } DestroyImmediate(checkPrefab); } } ClearHierarchy(); Refresh(); } }