Bladeren bron

EventSystem中管理了所有的Entity,这块放到Root中更加合适

tanghai 3 jaren geleden
bovenliggende
commit
9af143b595

+ 2 - 2
Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Session/NetInnerComponentOnReadEvent.cs

@@ -35,7 +35,7 @@ namespace ET.Server
                             replySession.Send(realActorId, response);
                         }
                         
-                        Entity entity = EventSystem.Instance.Get(realActorId);
+                        Entity entity = Root.Instance.Get(realActorId);
                         if (entity == null)
                         {
                             IActorResponse response = ActorHelper.CreateResponse(iActorRequest, ErrorCore.ERR_NotFoundActor);
@@ -78,7 +78,7 @@ namespace ET.Server
                     }
                     case IActorMessage iActorMessage:
                     {
-                        Entity entity = EventSystem.Instance.Get(realActorId);
+                        Entity entity = Root.Instance.Get(realActorId);
                         if (entity == null)
                         {
                             Log.Error($"not found actor: {scene.Name} {realActorId} {message}");

+ 10 - 1
Unity/Assets/Scripts/Core/Module/Entity/Entity.cs

@@ -72,8 +72,17 @@ namespace ET
                     this.status &= ~EntityStatus.IsRegister;
                 }
 
-                EventSystem.Instance.RegisterSystem(this, value);
                 
+                if (!value)
+                {
+                    Root.Instance.Remove(this.InstanceId);
+                }
+                else
+                {
+                    Root.Instance.Add(this);
+                    EventSystem.Instance.RegisterSystem(this);
+                }
+
 #if ENABLE_VIEW && UNITY_EDITOR
                 if (value)
                 {

+ 88 - 4
Unity/Assets/Scripts/Core/Module/Entity/Root.cs

@@ -1,11 +1,19 @@
-namespace ET
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace ET
 {
     // 管理根部的Scene
-    public class Root: Singleton<Root>
+    public class Root: Singleton<Root>, ISingletonAwake
     {
-        public Scene Scene { get; }
+        // 管理所有的Entity
+        private readonly Dictionary<long, Entity> allEntities = new();
+        
+        public Scene Scene { get; private set; }
 
-        public Root()
+        public void Awake()
         {
             this.Scene = EntitySceneFactory.CreateScene(0, SceneType.Process, "Process");
         }
@@ -14,5 +22,81 @@
         {
             this.Scene.Dispose();
         }
+
+        public void Add(Entity entity)
+        {
+            this.allEntities.Add(entity.InstanceId, entity);
+        }
+        
+        public void Remove(long instanceId)
+        {
+            this.allEntities.Remove(instanceId);
+        }
+
+        public Entity Get(long instanceId)
+        {
+            Entity component = null;
+            this.allEntities.TryGetValue(instanceId, out component);
+            return component;
+        }
+        
+        public override string ToString()
+        {
+            StringBuilder sb = new();
+            HashSet<Type> noParent = new HashSet<Type>();
+            Dictionary<Type, int> typeCount = new Dictionary<Type, int>();
+
+            HashSet<Type> noDomain = new HashSet<Type>();
+
+            foreach (var kv in this.allEntities)
+            {
+                Type type = kv.Value.GetType();
+                if (kv.Value.Parent == null)
+                {
+                    noParent.Add(type);
+                }
+
+                if (kv.Value.Domain == null)
+                {
+                    noDomain.Add(type);
+                }
+
+                if (typeCount.ContainsKey(type))
+                {
+                    typeCount[type]++;
+                }
+                else
+                {
+                    typeCount[type] = 1;
+                }
+            }
+
+            sb.AppendLine("not set parent type: ");
+            foreach (Type type in noParent)
+            {
+                sb.AppendLine($"\t{type.Name}");
+            }
+
+            sb.AppendLine("not set domain type: ");
+            foreach (Type type in noDomain)
+            {
+                sb.AppendLine($"\t{type.Name}");
+            }
+
+            IOrderedEnumerable<KeyValuePair<Type, int>> orderByDescending = typeCount.OrderByDescending(s => s.Value);
+
+            sb.AppendLine("Entity Count: ");
+            foreach (var kv in orderByDescending)
+            {
+                if (kv.Value == 1)
+                {
+                    continue;
+                }
+
+                sb.AppendLine($"\t{kv.Key.Name}: {kv.Value}");
+            }
+
+            return sb.ToString();
+        }
     }
 }

+ 7 - 93
Unity/Assets/Scripts/Core/Module/EventSystem/EventSystem.cs

@@ -69,8 +69,6 @@ namespace ET
             }
         }
         
-        private readonly Dictionary<long, Entity> allEntities = new();
-
         private readonly Dictionary<string, Type> allTypes = new();
 
         private readonly UnOrderMultiMapSet<Type, Type> types = new();
@@ -212,16 +210,8 @@ namespace ET
             return this.allTypes[typeName];
         }
 
-        public void RegisterSystem(Entity component, bool isRegister = true)
+        public void RegisterSystem(Entity component)
         {
-            if (!isRegister)
-            {
-                this.Remove(component.InstanceId);
-                return;
-            }
-
-            this.allEntities.Add(component.InstanceId, component);
-
             Type type = component.GetType();
 
             OneTypeSystems oneTypeSystems = this.typeSystems.GetOneTypeSystems(type);
@@ -239,23 +229,6 @@ namespace ET
             }
         }
 
-        public void Remove(long instanceId)
-        {
-            this.allEntities.Remove(instanceId);
-        }
-
-        public Entity Get(long instanceId)
-        {
-            Entity component = null;
-            this.allEntities.TryGetValue(instanceId, out component);
-            return component;
-        }
-
-        public bool IsRegister(long instanceId)
-        {
-            return this.allEntities.ContainsKey(instanceId);
-        }
-
         public void Deserialize(Entity component)
         {
             List<object> iDeserializeSystems = this.typeSystems.GetSystems(component.GetType(), typeof (IDeserializeSystem));
@@ -473,8 +446,8 @@ namespace ET
             while (count-- > 0)
             {
                 long instanceId = queue.Dequeue();
-                Entity component;
-                if (!this.allEntities.TryGetValue(instanceId, out component))
+                Entity component = Root.Instance.Get(instanceId);
+                if (component == null)
                 {
                     continue;
                 }
@@ -539,8 +512,8 @@ namespace ET
             while (count-- > 0)
             {
                 long instanceId = queue.Dequeue();
-                Entity component;
-                if (!this.allEntities.TryGetValue(instanceId, out component))
+                Entity component = Root.Instance.Get(instanceId);
+                if (component == null)
                 {
                     continue;
                 }
@@ -579,8 +552,8 @@ namespace ET
             while (count-- > 0)
             {
                 long instanceId = queue.Dequeue();
-                Entity component;
-                if (!this.allEntities.TryGetValue(instanceId, out component))
+                Entity component = Root.Instance.Get(instanceId);
+                if (component == null)
                 {
                     continue;
                 }
@@ -719,64 +692,5 @@ namespace ET
             
             return aInvokeHandler.Handle(args);
         }
-
-        public override string ToString()
-        {
-            StringBuilder sb = new();
-            HashSet<Type> noParent = new HashSet<Type>();
-            Dictionary<Type, int> typeCount = new Dictionary<Type, int>();
-
-            HashSet<Type> noDomain = new HashSet<Type>();
-
-            foreach (var kv in this.allEntities)
-            {
-                Type type = kv.Value.GetType();
-                if (kv.Value.Parent == null)
-                {
-                    noParent.Add(type);
-                }
-
-                if (kv.Value.Domain == null)
-                {
-                    noDomain.Add(type);
-                }
-
-                if (typeCount.ContainsKey(type))
-                {
-                    typeCount[type]++;
-                }
-                else
-                {
-                    typeCount[type] = 1;
-                }
-            }
-
-            sb.AppendLine("not set parent type: ");
-            foreach (Type type in noParent)
-            {
-                sb.AppendLine($"\t{type.Name}");
-            }
-
-            sb.AppendLine("not set domain type: ");
-            foreach (Type type in noDomain)
-            {
-                sb.AppendLine($"\t{type.Name}");
-            }
-
-            IOrderedEnumerable<KeyValuePair<Type, int>> orderByDescending = typeCount.OrderByDescending(s => s.Value);
-
-            sb.AppendLine("Entity Count: ");
-            foreach (var kv in orderByDescending)
-            {
-                if (kv.Value == 1)
-                {
-                    continue;
-                }
-
-                sb.AppendLine($"\t{kv.Key.Name}: {kv.Value}");
-            }
-
-            return sb.ToString();
-        }
     }
 }

+ 5 - 0
Unity/Assets/Scripts/Core/Singleton/Game.cs

@@ -35,6 +35,11 @@ namespace ET
             singletons.Push(singleton);
             
             singleton.Register();
+
+            if (singleton is ISingletonAwake awake)
+            {
+                awake.Awake();
+            }
             
             if (singleton is ISingletonUpdate)
             {

+ 7 - 0
Unity/Assets/Scripts/Core/Singleton/ISingletonAwake.cs

@@ -0,0 +1,7 @@
+namespace ET
+{
+    public interface ISingletonAwake
+    {
+        void Awake();
+    }
+}

+ 11 - 0
Unity/Assets/Scripts/Core/Singleton/ISingletonAwake.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 60e674069f8984b75b16d7027540547e
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 1 - 2
Unity/Assets/Scripts/Core/Singleton/Singleton.cs

@@ -40,9 +40,8 @@ namespace ET
             }
             this.isDisposed = true;
             
-            T t = instance;
+            instance.Dispose();
             instance = null;
-            t.Dispose();
         }
 
         bool ISingleton.IsDisposed()