Переглянути джерело

Destroy Awake等System挪到EntitySystemSingleton中

tanghai 2 роки тому
батько
коміт
40737ad6cd

+ 38 - 30
Unity/Assets/Scripts/Core/VProcess/Module/Entity/Entity.cs

@@ -387,7 +387,7 @@ namespace ET
                 if (!this.IsCreated)
                 {
                     this.IsCreated = true;
-                    this.GetEntitySystem().Deserialize(this);
+                    EntitySystemSingleton.Instance.Deserialize(this);
                 }
             }
         }
@@ -473,6 +473,7 @@ namespace ET
             this.IsRegister = false;
             this.InstanceId = 0;
 
+            ObjectPool objectPool = ObjectPool.Instance;
             // 清理Children
             if (this.children != null)
             {
@@ -482,7 +483,7 @@ namespace ET
                 }
 
                 this.children.Clear();
-                ObjectPool.Instance.Recycle(this.children);
+                objectPool.Recycle(this.children);
                 this.children = null;
 
                 if (this.childrenDB != null)
@@ -491,7 +492,7 @@ namespace ET
                     // 创建的才需要回到池中,从db中不需要回收
                     if (this.IsNew)
                     {
-                        ObjectPool.Instance.Recycle(this.childrenDB);
+                        objectPool.Recycle(this.childrenDB);
                         this.childrenDB = null;
                     }
                 }
@@ -506,7 +507,7 @@ namespace ET
                 }
 
                 this.components.Clear();
-                ObjectPool.Instance.Recycle(this.components);
+                objectPool.Recycle(this.components);
                 this.components = null;
 
                 // 创建的才需要回到池中,从db中不需要回收
@@ -515,7 +516,7 @@ namespace ET
                     this.componentsDB.Clear();
                     if (this.IsNew)
                     {
-                        ObjectPool.Instance.Recycle(this.componentsDB);
+                        objectPool.Recycle(this.componentsDB);
                         this.componentsDB = null;
                     }
                 }
@@ -524,7 +525,7 @@ namespace ET
             // 触发Destroy事件
             if (this is IDestroy)
             {
-                this.GetEntitySystem().Destroy(this);
+                EntitySystemSingleton.Instance.Destroy(this);
             }
 
             this.iScene = null;
@@ -684,7 +685,7 @@ namespace ET
             // 如果有IGetComponent接口,则触发GetComponentSystem
             if (this is IGetComponent)
             {
-                this.GetEntitySystem().GetComponent(this, component);
+                EntitySystemSingleton.Instance.GetComponent(this, component);
             }
 
             return (K) component;
@@ -706,7 +707,7 @@ namespace ET
             // 如果有IGetComponent接口,则触发GetComponentSystem
             if (this is IGetComponent)
             {
-                this.GetEntitySystem().GetComponent(this, component);
+                EntitySystemSingleton.Instance.GetComponent(this, component);
             }
 
             return component;
@@ -743,7 +744,7 @@ namespace ET
 
             if (this is IAddComponent)
             {
-                this.GetEntitySystem().AddComponent(this, component);
+                EntitySystemSingleton.Instance.AddComponent(this, component);
             }
 
             return component;
@@ -759,11 +760,12 @@ namespace ET
             Entity component = Create(type, isFromPool);
             component.Id = this.Id;
             component.ComponentParent = this;
-            this.GetEntitySystem().Awake(component);
+            EntitySystemSingleton entitySystemSingleton = EntitySystemSingleton.Instance;
+            entitySystemSingleton.Awake(component);
 
             if (this is IAddComponent)
             {
-                this.GetEntitySystem().AddComponent(this, component);
+                entitySystemSingleton.AddComponent(this, component);
             }
 
             return component;
@@ -780,11 +782,12 @@ namespace ET
             Entity component = Create(type, isFromPool);
             component.Id = id;
             component.ComponentParent = this;
-            this.GetEntitySystem().Awake(component);
+            EntitySystemSingleton entitySystemSingleton = EntitySystemSingleton.Instance;
+            entitySystemSingleton.Awake(component);
 
             if (this is IAddComponent)
             {
-                this.GetEntitySystem().AddComponent(this, component);
+                entitySystemSingleton.AddComponent(this, component);
             }
 
             return component as K;
@@ -801,11 +804,12 @@ namespace ET
             Entity component = Create(type, isFromPool);
             component.Id = id;
             component.ComponentParent = this;
-            this.GetEntitySystem().Awake(component, p1);
+            EntitySystemSingleton entitySystemSingleton = EntitySystemSingleton.Instance;
+            entitySystemSingleton.Awake(component, p1);
 
             if (this is IAddComponent)
             {
-                this.GetEntitySystem().AddComponent(this, component);
+                entitySystemSingleton.AddComponent(this, component);
             }
 
             return component as K;
@@ -822,11 +826,12 @@ namespace ET
             Entity component = Create(type, isFromPool);
             component.Id = id;
             component.ComponentParent = this;
-            this.GetEntitySystem().Awake(component, p1, p2);
+            EntitySystemSingleton entitySystemSingleton = EntitySystemSingleton.Instance;
+            entitySystemSingleton.Awake(component, p1, p2);
 
             if (this is IAddComponent)
             {
-                this.GetEntitySystem().AddComponent(this, component);
+                entitySystemSingleton.AddComponent(this, component);
             }
 
             return component as K;
@@ -843,11 +848,12 @@ namespace ET
             Entity component = Create(type, isFromPool);
             component.Id = id;
             component.ComponentParent = this;
-            this.GetEntitySystem().Awake(component, p1, p2, p3);
+            EntitySystemSingleton entitySystemSingleton = EntitySystemSingleton.Instance;
+            entitySystemSingleton.Awake(component, p1, p2, p3);
 
             if (this is IAddComponent)
             {
-                this.GetEntitySystem().AddComponent(this, component);
+                entitySystemSingleton.AddComponent(this, component);
             }
 
             return component as K;
@@ -886,7 +892,7 @@ namespace ET
             component.Id = this.GetIdGenerater().GenerateId();
             component.Parent = this;
 
-            this.GetEntitySystem().Awake(component);
+            EntitySystemSingleton.Instance.Awake(component);
             return component;
         }
 
@@ -897,7 +903,7 @@ namespace ET
             component.Id = this.GetIdGenerater().GenerateId();
             component.Parent = this;
 
-            this.GetEntitySystem().Awake(component, a);
+            EntitySystemSingleton.Instance.Awake(component, a);
             return component;
         }
 
@@ -908,7 +914,7 @@ namespace ET
             component.Id = this.GetIdGenerater().GenerateId();
             component.Parent = this;
 
-            this.GetEntitySystem().Awake(component, a, b);
+            EntitySystemSingleton.Instance.Awake(component, a, b);
             return component;
         }
 
@@ -919,7 +925,7 @@ namespace ET
             component.Id = this.GetIdGenerater().GenerateId();
             component.Parent = this;
 
-            this.GetEntitySystem().Awake(component, a, b, c);
+            EntitySystemSingleton.Instance.Awake(component, a, b, c);
             return component;
         }
 
@@ -929,7 +935,7 @@ namespace ET
             T component = Entity.Create(type, isFromPool) as T;
             component.Id = id;
             component.Parent = this;
-            this.GetEntitySystem().Awake(component);
+            EntitySystemSingleton.Instance.Awake(component);
             return component;
         }
 
@@ -940,7 +946,7 @@ namespace ET
             component.Id = id;
             component.Parent = this;
 
-            this.GetEntitySystem().Awake(component, a);
+            EntitySystemSingleton.Instance.Awake(component, a);
             return component;
         }
 
@@ -951,7 +957,7 @@ namespace ET
             component.Id = id;
             component.Parent = this;
 
-            this.GetEntitySystem().Awake(component, a, b);
+            EntitySystemSingleton.Instance.Awake(component, a, b);
             return component;
         }
 
@@ -962,17 +968,18 @@ namespace ET
             component.Id = id;
             component.Parent = this;
 
-            this.GetEntitySystem().Awake(component, a, b, c);
+            EntitySystemSingleton.Instance.Awake(component, a, b, c);
             return component;
         }
 
         public override void BeginInit()
         {
-            this.GetEntitySystem().Serialize(this);
+            EntitySystemSingleton.Instance.Serialize(this);
             
             this.componentsDB?.Clear();
             if (this.components != null && this.components.Count != 0)
             {
+                ObjectPool objectPool = ObjectPool.Instance;
                 foreach (Entity entity in this.components.Values)
                 {
                     if (entity is not ISerializeToEntity)
@@ -980,7 +987,7 @@ namespace ET
                         continue;
                     }
 
-                    this.componentsDB ??= ObjectPool.Instance.Fetch<List<Entity>>();
+                    this.componentsDB ??= objectPool.Fetch<List<Entity>>();
                     this.componentsDB.Add(entity);
 
                     entity.BeginInit();
@@ -990,6 +997,7 @@ namespace ET
             this.childrenDB?.Clear();
             if (this.children != null && this.children.Count != 0)
             {
+                ObjectPool objectPool = ObjectPool.Instance;
                 foreach (Entity entity in this.children.Values)
                 {
                     if (entity is not ISerializeToEntity)
@@ -997,7 +1005,7 @@ namespace ET
                         continue;
                     }
 
-                    this.childrenDB ??= ObjectPool.Instance.Fetch<List<Entity>>();
+                    this.childrenDB ??= objectPool.Fetch<List<Entity>>();
                     this.childrenDB.Add(entity);
 
                     entity.BeginInit();

+ 266 - 0
Unity/Assets/Scripts/Core/VProcess/Module/Entity/EntitySystemSingleton.cs

@@ -27,5 +27,271 @@ namespace ET
                 }
             }
         }
+        
+        public void Serialize(Entity component)
+        {
+            if (component is not ISerialize)
+            {
+                return;
+            }
+            
+            List<object> iSerializeSystems = EntitySystemSingleton.Instance.TypeSystems.GetSystems(component.GetType(), typeof (ISerializeSystem));
+            if (iSerializeSystems == null)
+            {
+                return;
+            }
+
+            foreach (ISerializeSystem serializeSystem in iSerializeSystems)
+            {
+                if (serializeSystem == null)
+                {
+                    continue;
+                }
+
+                try
+                {
+                    serializeSystem.Run(component);
+                }
+                catch (Exception e)
+                {
+                    Log.Error(e);
+                }
+            }
+        }
+        
+        public void Deserialize(Entity component)
+        {
+            if (component is not IDeserialize)
+            {
+                return;
+            }
+            
+            List<object> iDeserializeSystems = EntitySystemSingleton.Instance.TypeSystems.GetSystems(component.GetType(), typeof (IDeserializeSystem));
+            if (iDeserializeSystems == null)
+            {
+                return;
+            }
+
+            foreach (IDeserializeSystem deserializeSystem in iDeserializeSystems)
+            {
+                if (deserializeSystem == null)
+                {
+                    continue;
+                }
+
+                try
+                {
+                    deserializeSystem.Run(component);
+                }
+                catch (Exception e)
+                {
+                    Log.Error(e);
+                }
+            }
+        }
+        
+        // GetComponentSystem
+        public void GetComponent(Entity entity, Entity component)
+        {
+            List<object> iGetSystem = EntitySystemSingleton.Instance.TypeSystems.GetSystems(entity.GetType(), typeof (IGetComponentSystem));
+            if (iGetSystem == null)
+            {
+                return;
+            }
+
+            foreach (IGetComponentSystem getSystem in iGetSystem)
+            {
+                if (getSystem == null)
+                {
+                    continue;
+                }
+
+                try
+                {
+                    getSystem.Run(entity, component);
+                }
+                catch (Exception e)
+                {
+                    Log.Error(e);
+                }
+            }
+        }
+        
+        // AddComponentSystem
+        public void AddComponent(Entity entity, Entity component)
+        {
+            List<object> iAddSystem = EntitySystemSingleton.Instance.TypeSystems.GetSystems(entity.GetType(), typeof (IAddComponentSystem));
+            if (iAddSystem == null)
+            {
+                return;
+            }
+
+            foreach (IAddComponentSystem addComponentSystem in iAddSystem)
+            {
+                if (addComponentSystem == null)
+                {
+                    continue;
+                }
+
+                try
+                {
+                    addComponentSystem.Run(entity, component);
+                }
+                catch (Exception e)
+                {
+                    Log.Error(e);
+                }
+            }
+        }
+
+        public void Awake(Entity component)
+        {
+            List<object> iAwakeSystems = EntitySystemSingleton.Instance.TypeSystems.GetSystems(component.GetType(), typeof (IAwakeSystem));
+            if (iAwakeSystems == null)
+            {
+                return;
+            }
+
+            foreach (IAwakeSystem aAwakeSystem in iAwakeSystems)
+            {
+                if (aAwakeSystem == null)
+                {
+                    continue;
+                }
+
+                try
+                {
+                    aAwakeSystem.Run(component);
+                }
+                catch (Exception e)
+                {
+                    Log.Error(e);
+                }
+            }
+        }
+
+        public void Awake<P1>(Entity component, P1 p1)
+        {
+            if (component is not IAwake<P1>)
+            {
+                return;
+            }
+            
+            List<object> iAwakeSystems = EntitySystemSingleton.Instance.TypeSystems.GetSystems(component.GetType(), typeof (IAwakeSystem<P1>));
+            if (iAwakeSystems == null)
+            {
+                return;
+            }
+
+            foreach (IAwakeSystem<P1> aAwakeSystem in iAwakeSystems)
+            {
+                if (aAwakeSystem == null)
+                {
+                    continue;
+                }
+
+                try
+                {
+                    aAwakeSystem.Run(component, p1);
+                }
+                catch (Exception e)
+                {
+                    Log.Error(e);
+                }
+            }
+        }
+
+        public void Awake<P1, P2>(Entity component, P1 p1, P2 p2)
+        {
+            if (component is not IAwake<P1, P2>)
+            {
+                return;
+            }
+            
+            List<object> iAwakeSystems = EntitySystemSingleton.Instance.TypeSystems.GetSystems(component.GetType(), typeof (IAwakeSystem<P1, P2>));
+            if (iAwakeSystems == null)
+            {
+                return;
+            }
+
+            foreach (IAwakeSystem<P1, P2> aAwakeSystem in iAwakeSystems)
+            {
+                if (aAwakeSystem == null)
+                {
+                    continue;
+                }
+
+                try
+                {
+                    aAwakeSystem.Run(component, p1, p2);
+                }
+                catch (Exception e)
+                {
+                    Log.Error(e);
+                }
+            }
+        }
+
+        public void Awake<P1, P2, P3>(Entity component, P1 p1, P2 p2, P3 p3)
+        {
+            if (component is not IAwake<P1, P2, P3>)
+            {
+                return;
+            }
+            
+            List<object> iAwakeSystems = EntitySystemSingleton.Instance.TypeSystems.GetSystems(component.GetType(), typeof (IAwakeSystem<P1, P2, P3>));
+            if (iAwakeSystems == null)
+            {
+                return;
+            }
+
+            foreach (IAwakeSystem<P1, P2, P3> aAwakeSystem in iAwakeSystems)
+            {
+                if (aAwakeSystem == null)
+                {
+                    continue;
+                }
+
+                try
+                {
+                    aAwakeSystem.Run(component, p1, p2, p3);
+                }
+                catch (Exception e)
+                {
+                    Log.Error(e);
+                }
+            }
+        }
+
+        public void Destroy(Entity component)
+        {
+            if (component is not IDestroy)
+            {
+                return;
+            }
+            
+            List<object> iDestroySystems = EntitySystemSingleton.Instance.TypeSystems.GetSystems(component.GetType(), typeof (IDestroySystem));
+            if (iDestroySystems == null)
+            {
+                return;
+            }
+
+            foreach (IDestroySystem iDestroySystem in iDestroySystems)
+            {
+                if (iDestroySystem == null)
+                {
+                    continue;
+                }
+
+                try
+                {
+                    iDestroySystem.Run(component);
+                }
+                catch (Exception e)
+                {
+                    Log.Error(e);
+                }
+            }
+        }
     }
 }

+ 1 - 267
Unity/Assets/Scripts/Core/VProcess/Module/EntitySystem/EntitySystem.cs

@@ -15,7 +15,7 @@ namespace ET
             }
             this.Load();
         }
-
+        
         public void Load()
         {
             Queue<EntityRef<Entity>> queue = this.queues[InstanceQueueIndex.Load];
@@ -78,272 +78,6 @@ namespace ET
                 this.queues[i].Enqueue(component);
             }
         }
-        
-        public void Serialize(Entity component)
-        {
-            if (component is not ISerialize)
-            {
-                return;
-            }
-            
-            List<object> iSerializeSystems = EntitySystemSingleton.Instance.TypeSystems.GetSystems(component.GetType(), typeof (ISerializeSystem));
-            if (iSerializeSystems == null)
-            {
-                return;
-            }
-
-            foreach (ISerializeSystem serializeSystem in iSerializeSystems)
-            {
-                if (serializeSystem == null)
-                {
-                    continue;
-                }
-
-                try
-                {
-                    serializeSystem.Run(component);
-                }
-                catch (Exception e)
-                {
-                    Log.Error(e);
-                }
-            }
-        }
-        
-        public void Deserialize(Entity component)
-        {
-            if (component is not IDeserialize)
-            {
-                return;
-            }
-            
-            List<object> iDeserializeSystems = EntitySystemSingleton.Instance.TypeSystems.GetSystems(component.GetType(), typeof (IDeserializeSystem));
-            if (iDeserializeSystems == null)
-            {
-                return;
-            }
-
-            foreach (IDeserializeSystem deserializeSystem in iDeserializeSystems)
-            {
-                if (deserializeSystem == null)
-                {
-                    continue;
-                }
-
-                try
-                {
-                    deserializeSystem.Run(component);
-                }
-                catch (Exception e)
-                {
-                    Log.Error(e);
-                }
-            }
-        }
-        
-        // GetComponentSystem
-        public void GetComponent(Entity entity, Entity component)
-        {
-            List<object> iGetSystem = EntitySystemSingleton.Instance.TypeSystems.GetSystems(entity.GetType(), typeof (IGetComponentSystem));
-            if (iGetSystem == null)
-            {
-                return;
-            }
-
-            foreach (IGetComponentSystem getSystem in iGetSystem)
-            {
-                if (getSystem == null)
-                {
-                    continue;
-                }
-
-                try
-                {
-                    getSystem.Run(entity, component);
-                }
-                catch (Exception e)
-                {
-                    Log.Error(e);
-                }
-            }
-        }
-        
-        // AddComponentSystem
-        public void AddComponent(Entity entity, Entity component)
-        {
-            List<object> iAddSystem = EntitySystemSingleton.Instance.TypeSystems.GetSystems(entity.GetType(), typeof (IAddComponentSystem));
-            if (iAddSystem == null)
-            {
-                return;
-            }
-
-            foreach (IAddComponentSystem addComponentSystem in iAddSystem)
-            {
-                if (addComponentSystem == null)
-                {
-                    continue;
-                }
-
-                try
-                {
-                    addComponentSystem.Run(entity, component);
-                }
-                catch (Exception e)
-                {
-                    Log.Error(e);
-                }
-            }
-        }
-
-        public void Awake(Entity component)
-        {
-            List<object> iAwakeSystems = EntitySystemSingleton.Instance.TypeSystems.GetSystems(component.GetType(), typeof (IAwakeSystem));
-            if (iAwakeSystems == null)
-            {
-                return;
-            }
-
-            foreach (IAwakeSystem aAwakeSystem in iAwakeSystems)
-            {
-                if (aAwakeSystem == null)
-                {
-                    continue;
-                }
-
-                try
-                {
-                    aAwakeSystem.Run(component);
-                }
-                catch (Exception e)
-                {
-                    Log.Error(e);
-                }
-            }
-        }
-
-        public void Awake<P1>(Entity component, P1 p1)
-        {
-            if (component is not IAwake<P1>)
-            {
-                return;
-            }
-            
-            List<object> iAwakeSystems = EntitySystemSingleton.Instance.TypeSystems.GetSystems(component.GetType(), typeof (IAwakeSystem<P1>));
-            if (iAwakeSystems == null)
-            {
-                return;
-            }
-
-            foreach (IAwakeSystem<P1> aAwakeSystem in iAwakeSystems)
-            {
-                if (aAwakeSystem == null)
-                {
-                    continue;
-                }
-
-                try
-                {
-                    aAwakeSystem.Run(component, p1);
-                }
-                catch (Exception e)
-                {
-                    Log.Error(e);
-                }
-            }
-        }
-
-        public void Awake<P1, P2>(Entity component, P1 p1, P2 p2)
-        {
-            if (component is not IAwake<P1, P2>)
-            {
-                return;
-            }
-            
-            List<object> iAwakeSystems = EntitySystemSingleton.Instance.TypeSystems.GetSystems(component.GetType(), typeof (IAwakeSystem<P1, P2>));
-            if (iAwakeSystems == null)
-            {
-                return;
-            }
-
-            foreach (IAwakeSystem<P1, P2> aAwakeSystem in iAwakeSystems)
-            {
-                if (aAwakeSystem == null)
-                {
-                    continue;
-                }
-
-                try
-                {
-                    aAwakeSystem.Run(component, p1, p2);
-                }
-                catch (Exception e)
-                {
-                    Log.Error(e);
-                }
-            }
-        }
-
-        public void Awake<P1, P2, P3>(Entity component, P1 p1, P2 p2, P3 p3)
-        {
-            if (component is not IAwake<P1, P2, P3>)
-            {
-                return;
-            }
-            
-            List<object> iAwakeSystems = EntitySystemSingleton.Instance.TypeSystems.GetSystems(component.GetType(), typeof (IAwakeSystem<P1, P2, P3>));
-            if (iAwakeSystems == null)
-            {
-                return;
-            }
-
-            foreach (IAwakeSystem<P1, P2, P3> aAwakeSystem in iAwakeSystems)
-            {
-                if (aAwakeSystem == null)
-                {
-                    continue;
-                }
-
-                try
-                {
-                    aAwakeSystem.Run(component, p1, p2, p3);
-                }
-                catch (Exception e)
-                {
-                    Log.Error(e);
-                }
-            }
-        }
-
-        public void Destroy(Entity component)
-        {
-            if (component is not IDestroy)
-            {
-                return;
-            }
-            
-            List<object> iDestroySystems = EntitySystemSingleton.Instance.TypeSystems.GetSystems(component.GetType(), typeof (IDestroySystem));
-            if (iDestroySystems == null)
-            {
-                return;
-            }
-
-            foreach (IDestroySystem iDestroySystem in iDestroySystems)
-            {
-                if (iDestroySystem == null)
-                {
-                    continue;
-                }
-
-                try
-                {
-                    iDestroySystem.Run(component);
-                }
-                catch (Exception e)
-                {
-                    Log.Error(e);
-                }
-            }
-        }
 
         public void Update()
         {