Explorar el Código

修复 Entity Tree Window 刷新机制不正确导致的显示问题 (#396)

L hace 3 años
padre
commit
4b56e842d5

+ 1 - 2
Codes/Model/Share/Module/Unit/Unit.cs

@@ -5,7 +5,7 @@ using UnityEngine;
 namespace ET
 {
     [ChildOf(typeof(UnitComponent))]
-    [DebuggerDisplay("DebuggerDisplay,nq")]
+    [DebuggerDisplay("ViewGoName,nq")]
     public class Unit: Entity, IAwake<int>
     {
         public int ConfigId { get; set; } //配置表id
@@ -51,7 +51,6 @@ namespace ET
             }
         }
 
-        private string DebuggerDisplay => this.Config.Name;
         
 #if ENABLE_CODES
         protected override string ViewGoName

+ 1 - 1
Unity/Assets/Scripts/Core/Entity/Scene.cs

@@ -3,7 +3,7 @@
 namespace ET
 {
     [EnableMethod]
-    [DebuggerDisplay("DebuggerDisplay,nq")]
+    [DebuggerDisplay("ViewGoName,nq")]
     public sealed class Scene: Entity
     {
         public int Zone

+ 26 - 11
Unity/Assets/Scripts/Editor/ComponentViewEditor/Tree/EntityTreeView.cs

@@ -6,19 +6,28 @@ using UnityEngine;
 
 namespace ET
 {
-    public class EntityTreeView : TreeView
+    public class EntityTreeView: TreeView
     {
         private EntityTreeViewItem root;
         private int                id;
 
-        public Dictionary<int, Entity> all = new();
+        private readonly Dictionary<int, Entity> all             = new();
+        private readonly Dictionary<Entity, int> entityHistoryID = new();
 
-        public EntityTreeView(TreeViewState state) : base(state)
+        public EntityTreeView(TreeViewState state): base(state)
         {
             Reload();
             useScrollView = true;
         }
 
+        public void Refresh()
+        {
+            this.root = BuildRoot() as EntityTreeViewItem;
+            BuildRows(this.root);
+
+            this.Repaint();
+        }
+
         protected override TreeViewItem BuildRoot()
         {
             this.id = 0;
@@ -31,7 +40,6 @@ namespace ET
             return this.root;
         }
 
-
         private EntityTreeViewItem PreOrder(Entity root)
         {
             if(root is null)
@@ -39,11 +47,17 @@ namespace ET
                 return null;
             }
 
-            this.id++;
+            if(!this.entityHistoryID.TryGetValue(root, out var itemID))
+            {
+                this.id++;
+                itemID = this.id;
 
-            var item = new EntityTreeViewItem(root, this.id);
+                this.entityHistoryID[root] = itemID;
+            }
+
+            EntityTreeViewItem item = new(root, itemID);
 
-            all[this.id] = root;
+            this.all[itemID] = root;
 
             if(root.Components.Count > 0)
             {
@@ -64,7 +78,6 @@ namespace ET
             return item;
         }
 
-
         /// <summary>
         /// 处理右键内容
         /// </summary>
@@ -81,14 +94,13 @@ namespace ET
             EntityContextMenu.Show(EntityTreeWindow.VIEW_MONO.Component);
         }
 
-
         /// <summary>
         /// 处理左键内容
         /// </summary>
         /// <param name="id"></param>
         protected override void SingleClickedItem(int id)
         {
-            all.TryGetValue(id, out var entity);
+            this.all.TryGetValue(id, out Entity entity);
 
             if(entity is null)
             {
@@ -96,7 +108,10 @@ namespace ET
             }
 
             EntityTreeWindow.VIEW_MONO.Component = entity;
-            Selection.activeObject            = EntityTreeWindow.VIEW_MONO;
+            Selection.activeObject               = null;
+
+            // 刷新 Inspector 显示
+            EditorApplication.delayCall += () => { Selection.activeObject = EntityTreeWindow.VIEW_MONO; };
         }
     }
 }

+ 11 - 8
Unity/Assets/Scripts/Editor/ComponentViewEditor/Tree/EntityTreeViewItem.cs

@@ -1,3 +1,5 @@
+#if ENABLE_CODES
+
 using System;
 using System.Runtime.CompilerServices;
 using Microsoft.CSharp.RuntimeBinder;
@@ -5,14 +7,14 @@ using UnityEditor.IMGUI.Controls;
 
 namespace ET
 {
-    public class EntityTreeViewItem : TreeViewItem
+    public class EntityTreeViewItem: TreeViewItem
     {
-        private Entity entity;
+        public Entity entity;
 
         public EntityTreeViewItem(Entity entity, int id)
         {
             this.entity = entity;
-            base.id = id;
+            base.id     = id;
         }
 
         public override string displayName
@@ -28,18 +30,18 @@ namespace ET
 
                 string debugger_name = ReadDebuggerDisplay(entity);
 
-                _displayName = string.IsNullOrEmpty(debugger_name) ? name : $"{name}: ({debugger_name})";
+                _displayName = string.IsNullOrEmpty(debugger_name)? name : $"{name}{debugger_name}";
 
                 return _displayName;
             }
         }
 
         private string _displayName;
-        
+
         // https://stackoverflow.com/a/13650728/37055
         private static object ReadProperty(object target, string propertyName)
         {
-            var args = new[] {CSharpArgumentInfo.Create(0, null)};
+            var args = new[] { CSharpArgumentInfo.Create(0, null) };
             var binder = Binder.GetMember(
                 0,
                 propertyName,
@@ -50,7 +52,7 @@ namespace ET
             return site.Target(site, target);
         }
 
-        private static string ReadDebuggerDisplay(object target, string propertyName = "DebuggerDisplay")
+        private static string ReadDebuggerDisplay(object target, string propertyName = "ViewGoName")
         {
             string debuggerDisplay = string.Empty;
             try
@@ -67,4 +69,5 @@ namespace ET
             return debuggerDisplay;
         }
     }
-}
+}
+#endif

+ 6 - 1
Unity/Assets/Scripts/Editor/ComponentViewEditor/Tree/EntityTreeWindow.cs

@@ -1,4 +1,5 @@
 #if ENABLE_CODES
+using System;
 using UnityEditor;
 using UnityEditor.IMGUI.Controls;
 using UnityEngine;
@@ -56,6 +57,11 @@ namespace ET
             VIEW_MONO = null;
         }
 
+        private void OnInspectorUpdate()
+        {
+            this.treeView?.Refresh();
+        }
+
         private void OnGUI()
         {
             this.treeView.searchString = this.searchField.OnGUI(
@@ -77,7 +83,6 @@ namespace ET
                 )
             );
 
-            this.treeView.Reload();
 
             GUILayout.BeginArea(
                 new Rect(