using System.Collections.Generic; using System.Linq; using System.Text; using UnityEditor; using UnityEngine; namespace TyphoonUI { /// /// 拓展UIObject /// public static class UIObjectExtension { /// /// 获取有效节点 /// public static HashSet GetValidateNodesHashList(this UIObject obj) { var hash = new HashSet(); foreach (var element in obj.Nodes) { if (element != null) { hash.Add(element); } } return hash; } /// /// 元素是否被导出 /// public static bool Contains(this UIObject obj, Object check) { return obj.Nodes.Contains(check); } /// /// 设置导出元素 /// public static void SetNodes(this UIObject obj, List nodes) { obj.Nodes = nodes; } /// /// 保存 /// public static void Save(this UIObject obj) { EditorUtility.SetDirty(obj); AssetDatabase.SaveAssets(); } /// /// 添加导出元素 /// public static void Add(this UIObject obj, Object element, bool apply = true) { var nodes = GetValidateNodesHashList(obj); nodes.Add(element); var list = nodes.ToList(); obj.Nodes = list; if (apply) { obj.Apply(); } } /// /// 应用 /// public static void Apply(this UIObject obj) { var nodes = GetValidateNodesHashList(obj); //按层级排序 var list = nodes.ToList(); list.Sort((a, b) => { var goA = a is GameObject ? a as GameObject : (a as Component).gameObject; var goB = b is GameObject ? b as GameObject : (b as Component).gameObject; var code = goA.GetHierarchySiblingIndices().CompareTo(goB.GetHierarchySiblingIndices()); if (code == 0) { var nameA = a.GetType().FullName; var nameB = b.GetType().FullName; return nameA.CompareTo(nameB); } return code; }); obj.Nodes = list; TyphoonUIEditorApplication.InvokeUIObjectChanged(obj); EditorUtility.SetDirty(obj); } /// /// 修改 /// public static void Modify(this UIObject obj, List status) { if (status != null) { bool changed = false; var hash = obj.GetValidateNodesHashList(); foreach (var element in status) { if (element.Exported) { changed |= hash.Add(element.Node); } else { changed |= hash.Remove(element.Node); } } if (changed) { obj.Nodes = hash.ToList(); obj.Apply(); } } } public static string ExportCode(this UIObject obj, bool refreshProj = true) { return CodeTool.ExportCode(obj, refreshProj); } /// /// 获取相关联的实例ID /// public static HashSet GetRelatedInstanceID(this UIObject obj) { var hash = new HashSet(); var nodes = obj.Nodes; foreach (var node in nodes) { if (node == null) { continue; } var go = node as GameObject; if (go == null) { go = (node as Component)?.gameObject; } hash.Add(go.GetInstanceID()); } return hash; } /// /// 是否有空节点 /// public static bool HasNullNode(this UIObject obj) { foreach (var element in obj.Nodes) { if (element == null) { return true; } } return false; } public static string GetLinkUIObjectViewFullClass(UIObject uiObject) { var sb = new StringBuilder(); var original = PrefabUtility.GetCorrespondingObjectFromOriginalSource(uiObject); sb.AppendLine($"#{uiObject}"); if (original != null) { //无原始预制物 sb.AppendLine($"[{original.gameObject.GetHierarchyPath()}]"); } var fullClass = new ViewCodeGenerator(original).GetFullClassName(); sb.AppendLine($"class name:{fullClass}"); Debug.Log(sb.ToString()); return fullClass; } } }