Explorar el Código

Entity Domain跟Parent再次调整
1. Domain不允许自己设置了,设置了Parent后,Domain跟Parent设置成一样
2. EntityFactory放到了Entity中
3. Entity的创建必须设置Parent,这样强制保证所有创建出来的Entity都在Game.Scene这个书上,统一管理生命周期
4. 设置了Parent的对象要释放必须调用Dispose,父亲Dispose了,那么他的孩子孙子都会Dispose
5. Parent不允许设置为null,但是可以修改,比如两个人交易道具,道具的parent可以改变

注意: 对象反序列化出来是没有Parent的,反序列化的对象本来就不应该管理它的生命周期,所以它没有parent是非常合理的。
要执行反序列化System必须把它设置parent,把它挂到Game.Scene树上去。为啥要这样设计呢?举个例子,BuffComponent对象从数据库中取出来,反序列化可能会注册每个Buff的过期Timer,而Timer是非托管的,而Timer又可能闭包引用了Buff,从而导致泄漏,如果这个Buff没有在Game.Scene树上,那Buff究竟是要Dispose还是靠GC回收呢,这样就搞不清楚了?而现在我们强制反序列化必须设置Parent,那么Buff就成为了Game.Scene的孙子,它释放必须Dispose,这样就保证不会泄漏了。即使没有Dispose,我们也能很快在EventSystem.allEntities中打印出来发现这个对象没有Dispose

tanghai hace 4 años
padre
commit
1c90514252

+ 1 - 1
Robot/Hotfix/Module/RobotCase/RobotCaseComponentSystem.cs

@@ -31,7 +31,7 @@ namespace ET
         public static async ETTask<RobotCase> New(this RobotCaseComponent self)
         {
             await ETTask.CompletedTask;
-            RobotCase robotCase = EntityFactory.CreateWithParent<RobotCase>(self);
+            RobotCase robotCase = Entity.Create<RobotCase>(self);
             return robotCase;
         }
     }

+ 4 - 2
Server/Hotfix/Demo/C2G_LoginGateHandler.cs

@@ -17,8 +17,10 @@ namespace ET
 				reply();
 				return;
 			}
-			Player player = EntityFactory.Create<Player, string>(Game.Scene, account);
-			scene.GetComponent<PlayerComponent>().Add(player);
+
+			PlayerComponent playerComponent = scene.GetComponent<PlayerComponent>();
+			Player player = Entity.Create<Player, string>(playerComponent, account);
+			playerComponent.Add(player);
 			session.AddComponent<SessionPlayerComponent>().Player = player;
 			session.AddComponent<MailBoxComponent, MailboxType>(MailboxType.GateSession);
 

+ 3 - 2
Server/Hotfix/Demo/G2M_CreateUnitHandler.cs

@@ -8,7 +8,8 @@ namespace ET
 	{
 		protected override async ETTask Run(Scene scene, G2M_CreateUnit request, M2G_CreateUnit response, Action reply)
 		{
-			Unit unit = EntityFactory.CreateWithId<Unit, int>(scene, IdGenerater.Instance.GenerateId(), 1001);
+			UnitComponent unitComponent = scene.GetComponent<UnitComponent>();
+			Unit unit = Entity.CreateWithId<Unit, int>(unitComponent, IdGenerater.Instance.GenerateId(), 1001);
 			unit.AddComponent<MoveComponent>();
 			unit.Position = new Vector3(-10, 0, -10);
 			
@@ -18,7 +19,7 @@ namespace ET
 			unit.AddComponent<MailBoxComponent>();
 			await unit.AddLocation();
 			unit.AddComponent<UnitGateComponent, long>(request.GateSessionId);
-			scene.GetComponent<UnitComponent>().Add(unit);
+			unitComponent.Add(unit);
 			response.UnitId = unit.Id;
 			
 			// 把自己广播给周围的人

+ 0 - 1
Server/Hotfix/Demo/Unit/UnitComponentSystem.cs

@@ -21,7 +21,6 @@ namespace ET
     {
         public static void Add(this UnitComponent self, Unit unit)
         {
-            unit.Parent = self;
             self.idUnits.Add(unit.Id, unit);
         }
 

+ 1 - 1
Server/Hotfix/Module/ActorLocation/ActorLocationSenderComponentSystem.cs

@@ -62,7 +62,7 @@ namespace ET
                 return (ActorLocationSender) actorLocationSender;
             }
 
-            actorLocationSender = EntityFactory.CreateWithParentAndId<ActorLocationSender>(self, id);
+            actorLocationSender = Entity.CreateWithId<ActorLocationSender>(self, id);
             return (ActorLocationSender) actorLocationSender;
         }
 

+ 2 - 2
Server/Hotfix/Module/Message/NetInnerComponentSystem.cs

@@ -87,7 +87,7 @@ namespace ET
         // 这个channelId是由CreateAcceptChannelId生成的
         public static void OnAccept(this NetInnerComponent self, long channelId, IPEndPoint ipEndPoint)
         {
-            Session session = EntityFactory.CreateWithParentAndId<Session, AService>(self, channelId, self.Service);
+            Session session = Entity.CreateWithId<Session, AService>(self, channelId, self.Service);
             session.RemoteAddress = ipEndPoint;
             //session.AddComponent<SessionIdleCheckerComponent, int, int, int>(NetThreadComponent.checkInteral, NetThreadComponent.recvMaxIdleTime, NetThreadComponent.sendMaxIdleTime);
         }
@@ -103,7 +103,7 @@ namespace ET
 
         private static Session CreateInner(this NetInnerComponent self, long channelId, IPEndPoint ipEndPoint)
         {
-            Session session = EntityFactory.CreateWithParentAndId<Session, AService>(self, channelId, self.Service);
+            Session session = Entity.CreateWithId<Session, AService>(self, channelId, self.Service);
 
             session.RemoteAddress = ipEndPoint;
 

+ 1 - 1
Server/Model/Demo/RecastNav/RecastPathComponent.cs

@@ -71,7 +71,7 @@ namespace ET
 
             if (RecastInterface.LoadMap(mapId, navDataPath))
             {
-                RecastPathProcessor recastPathProcessor = EntityFactory.Create<RecastPathProcessor>(this.domain);
+                RecastPathProcessor recastPathProcessor = Entity.Create<RecastPathProcessor>(this);
                 recastPathProcessor.MapId = mapId;
                 m_RecastPathProcessorDic[mapId] = recastPathProcessor;
                 Log.Debug($"加载Id为{mapId}的地图Nav数据成功!");

+ 1 - 2
Server/Model/Module/ActorLocation/LocationComponent.cs

@@ -60,8 +60,7 @@ namespace ET
         {
             CoroutineLock coroutineLock = await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key);
 
-            LockInfo lockInfo = EntityFactory.Create<LockInfo, long, CoroutineLock>(this.Domain, instanceId, coroutineLock);
-            lockInfo.Parent = this;
+            LockInfo lockInfo = Entity.Create<LockInfo, long, CoroutineLock>(this, instanceId, coroutineLock);
             this.lockInfos.Add(key, lockInfo);
 
             Log.Debug($"location lock key: {key} instanceId: {instanceId}");

+ 0 - 1
Unity/Assets/Hotfix/Demo/Unit/UnitComponent.cs

@@ -29,7 +29,6 @@ namespace ET
 		public static void Add(this UnitComponent self, Unit unit)
 		{
 			self.idUnits.Add(unit.Id, unit);
-			unit.Parent = self;
 		}
 
 		public static Unit Get(this UnitComponent self, long id)

+ 6 - 6
Unity/Assets/Hotfix/Demo/Unit/UnitFactory.cs

@@ -6,7 +6,10 @@ namespace ET
     {
         public static Unit Create(Entity domain, UnitInfo unitInfo)
         {
-	        Unit unit = EntityFactory.CreateWithId<Unit, int>(domain, unitInfo.UnitId, unitInfo.ConfigId);
+	        UnitComponent unitComponent = domain.GetComponent<UnitComponent>();
+	        Unit unit = Entity.CreateWithId<Unit, int>(unitComponent, unitInfo.UnitId, unitInfo.ConfigId);
+	        unitComponent.Add(unit);
+	        
 	        unit.Position = new Vector3(unitInfo.X, unitInfo.Y, unitInfo.Z);
 	        
 	        unit.AddComponent<MoveComponent>();
@@ -19,11 +22,8 @@ namespace ET
 	        unit.AddComponent<ObjectWait>();
 
 	        unit.AddComponent<XunLuoPathComponent>();
-
-	        UnitComponent unitComponent = domain.GetComponent<UnitComponent>();
-            unitComponent.Add(unit);
-            
-            Game.EventSystem.Publish(new EventType.AfterUnitCreate() {Unit = unit});
+	        
+	        Game.EventSystem.Publish(new EventType.AfterUnitCreate() {Unit = unit});
             return unit;
         }
     }

+ 2 - 2
Unity/Assets/Hotfix/Module/Message/NetKcpComponentSystem.cs

@@ -83,7 +83,7 @@ namespace ET
         // 这个channelId是由CreateAcceptChannelId生成的
         public static void OnAccept(this NetKcpComponent self, long channelId, IPEndPoint ipEndPoint)
         {
-            Session session = EntityFactory.CreateWithParentAndId<Session, AService>(self, channelId, self.Service);
+            Session session = Entity.CreateWithId<Session, AService>(self, channelId, self.Service);
             session.RemoteAddress = ipEndPoint;
 
             session.AddComponent<SessionAcceptTimeoutComponent>();
@@ -100,7 +100,7 @@ namespace ET
         public static Session Create(this NetKcpComponent self, IPEndPoint realIPEndPoint)
         {
             long channelId = RandomHelper.RandInt64();
-            Session session = EntityFactory.CreateWithParentAndId<Session, AService>(self, channelId, self.Service);
+            Session session = Entity.CreateWithId<Session, AService>(self, channelId, self.Service);
             session.RemoteAddress = realIPEndPoint;
             session.AddComponent<SessionIdleCheckerComponent, int>(NetThreadComponent.checkInteral);
             

+ 1 - 1
Unity/Assets/HotfixView/Demo/UI/UILoading/UILoadingEvent.cs

@@ -14,7 +14,7 @@ namespace ET
 				GameObject bundleGameObject = ((GameObject)Resources.Load("KV")).Get<GameObject>(UIType.UILoading);
 				GameObject go = UnityEngine.Object.Instantiate(bundleGameObject);
 				go.layer = LayerMask.NameToLayer(LayerNames.UI);
-				UI ui = EntityFactory.CreateWithParent<UI, string, GameObject>(uiComponent, UIType.UILoading, go);
+				UI ui = Entity.Create<UI, string, GameObject>(uiComponent, UIType.UILoading, go);
 
 				ui.AddComponent<UILoadingComponent>();
 				return ui;

+ 1 - 1
Unity/Assets/HotfixView/Demo/UI/UILobby/UILobbyEvent.cs

@@ -12,7 +12,7 @@ namespace ET
             ResourcesComponent.Instance.LoadBundle(UIType.UILobby.StringToAB());
             GameObject bundleGameObject = (GameObject) ResourcesComponent.Instance.GetAsset(UIType.UILobby.StringToAB(), UIType.UILobby);
             GameObject gameObject = UnityEngine.Object.Instantiate(bundleGameObject);
-            UI ui = EntityFactory.CreateWithParent<UI, string, GameObject>(uiComponent, UIType.UILobby, gameObject);
+            UI ui = Entity.Create<UI, string, GameObject>(uiComponent, UIType.UILobby, gameObject);
 
             ui.AddComponent<UILobbyComponent>();
             return ui;

+ 1 - 1
Unity/Assets/HotfixView/Demo/UI/UILogin/UILoginEvent.cs

@@ -12,7 +12,7 @@ namespace ET
             GameObject bundleGameObject = (GameObject) ResourcesComponent.Instance.GetAsset(UIType.UILogin.StringToAB(), UIType.UILogin);
             GameObject gameObject = UnityEngine.Object.Instantiate(bundleGameObject);
 
-            UI ui = EntityFactory.CreateWithParent<UI, string, GameObject>(uiComponent, UIType.UILogin, gameObject);
+            UI ui = Entity.Create<UI, string, GameObject>(uiComponent, UIType.UILogin, gameObject);
 
             ui.AddComponent<UILoginComponent>();
             return ui;

+ 4 - 4
Unity/Assets/Model/Core/Entity/TimerComponent.cs

@@ -176,7 +176,7 @@ namespace ET
             }
 
             ETTask<bool> tcs = ETTask<bool>.Create(true);
-            TimerAction timer = EntityFactory.CreateWithParent<TimerAction, TimerClass, long, object>(this, TimerClass.OnceWaitTimer, 0, tcs, true);
+            TimerAction timer = Entity.Create<TimerAction, TimerClass, long, object>(this, TimerClass.OnceWaitTimer, 0, tcs, true);
             this.AddTimer(tillTime, timer);
             long timerId = timer.Id;
 
@@ -216,7 +216,7 @@ namespace ET
 
             ETTask<bool> tcs = ETTask<bool>.Create(true);
             
-            TimerAction timer = EntityFactory.CreateWithParent<TimerAction, TimerClass, long, object>(this, TimerClass.OnceWaitTimer, 0, tcs, true);
+            TimerAction timer = Entity.Create<TimerAction, TimerClass, long, object>(this, TimerClass.OnceWaitTimer, 0, tcs, true);
             this.AddTimer(tillTime, timer);
             long timerId = timer.Id;
 
@@ -262,7 +262,7 @@ namespace ET
 			}
 #endif
             long tillTime = TimeHelper.ServerNow() + time;
-            TimerAction timer = EntityFactory.CreateWithParent<TimerAction, TimerClass, long, object>(this, TimerClass.RepeatedTimer, time, action, true);
+            TimerAction timer = Entity.Create<TimerAction, TimerClass, long, object>(this, TimerClass.RepeatedTimer, time, action, true);
             this.AddTimer(tillTime, timer);
             return timer.Id;
         }
@@ -300,7 +300,7 @@ namespace ET
             {
                 Log.Error($"new once time too small: {tillTime}");
             }
-            TimerAction timer = EntityFactory.CreateWithParent<TimerAction, TimerClass, long, object>(this, TimerClass.OnceTimer, 0, action, true);
+            TimerAction timer = Entity.Create<TimerAction, TimerClass, long, object>(this, TimerClass.OnceTimer, 0, action, true);
             this.AddTimer(tillTime, timer);
             return timer.Id;
         }

+ 36 - 41
Unity/Assets/Model/Core/Object/Entity.cs

@@ -129,6 +129,7 @@ namespace ET
         [BsonIgnore]
         protected Entity parent;
 
+        // 可以改变parent,但是不能设置为null
         [IgnoreDataMember]
         [BsonIgnore]
         public Entity Parent
@@ -136,37 +137,37 @@ namespace ET
             get => this.parent;
             set
             {
+                if (value == null)
+                {
+                    throw new Exception($"cant set parent null: {this.GetType().Name}");
+                }
+                
                 if (value == this)
                 {
                     throw new Exception($"cant set parent self: {this.GetType().Name}");
                 }
 
+                // 严格限制parent必须要有domain,也就是说parent必须在数据树上面
+                if (value.Domain == null)
+                {
+                    throw new Exception($"cant set parent because parent domain is null: {this.GetType().Name} {value.GetType().Name}");
+                }
+
                 if (this.parent != null) // 之前有parent
                 {
                     // parent相同,不设置
-                    if (value != null && this.parent.InstanceId == value.InstanceId)
+                    if (this.parent == value)
                     {
                         Log.Error($"重复设置了Parent: {this.GetType().Name} parent: {this.parent.GetType().Name}");
                         return;
                     }
-
                     this.parent.RemoveChild(this);
                 }
                 
-                
-                if (value == null)
-                {
-                    this.parent = null;
-                    this.IsComponent = false;
-                    this.Domain = null;
-                }
-                else
-                {
-                    this.parent = value;
-                    this.parent.AddChild(this);
-                    this.IsComponent = false;
-                    this.Domain = this.parent.domain;
-                }
+                this.parent = value;
+                this.parent.AddChild(this);
+                this.IsComponent = false;
+                this.Domain = this.parent.domain;
             }
         }
 
@@ -183,18 +184,11 @@ namespace ET
                 }
 
                 this.parent = value;
-
                 this.IsComponent = true;
-
-                AfterSetParent();
+                this.Domain = this.parent.domain;
             }
         }
 
-        private void AfterSetParent()
-        {
-            this.Domain = this.parent.domain;
-        }
-
         public T GetParent<T>() where T : Entity
         {
             return this.Parent as T;
@@ -218,29 +212,30 @@ namespace ET
         [BsonIgnore]
         public Entity Domain
         {
-            get => this.domain;
-            set
+            get
+            {
+                return this.domain;
+            }
+            private set
             {
-                if (this.InstanceId == 0)
+                if (value == null)
                 {
-                    this.InstanceId = IdGenerater.Instance.GenerateInstanceId();
+                    throw new Exception($"domain cant set null: {this.GetType().Name}");
+                }
+                
+                if (this.domain == value)
+                {
+                    return;
                 }
                 
                 Entity preDomain = this.domain;
                 this.domain = value;
                 
-                // 是否注册跟parent一致
-                if (this.parent != null)
-                {
-                    this.IsRegister = this.Parent.IsRegister;
-                }
-                else
-                {
-                    this.IsRegister = false;
-                }
-
-                if (preDomain == null && !this.IsCreate)
+                if (preDomain == null)
                 {
+                    this.InstanceId = IdGenerater.Instance.GenerateInstanceId();
+                    this.IsRegister = true;
+                    
                     // 反序列化出来的需要设置父子关系
                     if (this.componentsDB != null)
                     {
@@ -280,7 +275,7 @@ namespace ET
                     }
                 }
 
-                if (preDomain == null && !this.IsCreate)
+                if (preDomain == null)
                 {
                     EventSystem.Instance.Deserialize(this);
                 }
@@ -380,7 +375,7 @@ namespace ET
                 return;
             }
 
-            EventSystem.Instance.Remove(this.InstanceId);
+            this.IsRegister = false;
             this.InstanceId = 0;
 
             // 清理Component

+ 0 - 83
Unity/Assets/Model/Core/Object/EntityCreateComponet.cs

@@ -1,83 +0,0 @@
-using System;
-
-namespace ET
-{
-    public partial class Entity
-    {
-        public static Entity Create(Type type, bool isFromPool)
-        {
-            Entity component;
-            if (isFromPool)
-            {
-                component = (Entity)ObjectPool.Instance.Fetch(type);
-            }
-            else
-            {
-                component = (Entity)Activator.CreateInstance(type);
-            }
-            component.IsFromPool = isFromPool;
-            component.IsCreate = true;
-            component.Id = 0;
-            return component;
-        }
-		
-        private Entity CreateWithComponentParent(Type type, bool isFromPool = true)
-        {
-            Entity component = Create(type, isFromPool);
-			
-            component.Id = this.Id;
-            component.ComponentParent = this;
-			
-            EventSystem.Instance.Awake(component);
-            return component;
-        }
-
-        private T CreateWithComponentParent<T>(bool isFromPool = true) where T : Entity
-        {
-            Type type = typeof (T);
-            Entity component = Create(type, isFromPool);
-			
-            component.Id = this.Id;
-            component.ComponentParent = this;
-			
-            EventSystem.Instance.Awake(component);
-            return (T)component;
-        }
-
-        private T CreateWithComponentParent<T, A>(A a, bool isFromPool = true) where T : Entity
-        {
-            Type type = typeof (T);
-            Entity component = Create(type, isFromPool);
-			
-            component.Id = this.Id;
-            component.ComponentParent = this;
-			
-            EventSystem.Instance.Awake(component, a);
-            return (T)component;
-        }
-
-        private T CreateWithComponentParent<T, A, B>(A a, B b, bool isFromPool = true) where T : Entity
-        {
-            Type type = typeof (T);
-            Entity component = Create(type, isFromPool);
-			
-            component.Id = this.Id;
-            component.ComponentParent = this;
-			
-            EventSystem.Instance.Awake(component, a, b);
-            return (T)component;
-        }
-
-        private T CreateWithComponentParent<T, A, B, C>(A a, B b, C c, bool isFromPool = true) where T : Entity
-        {
-            Type type = typeof (T);
-            Entity component = Create(type, isFromPool);
-			
-            component.Id = this.Id;
-            component.ComponentParent = this;
-			
-            EventSystem.Instance.Awake(component, a, b, c);
-            return (T)component;
-        }
-    }
-}

+ 64 - 78
Unity/Assets/Model/Core/Object/EntityFactory.cs

@@ -2,9 +2,9 @@
 
 namespace ET
 {
-    public static class EntityFactory
+    public partial class Entity
     {
-        public static T CreateWithParent<T>(Entity parent, bool isFromPool = false) where T : Entity
+        public static T Create<T>(Entity parent, bool isFromPool = false) where T : Entity
         {
             Type type = typeof (T);
             T component = (T) Entity.Create(type, isFromPool);
@@ -15,7 +15,7 @@ namespace ET
             return component;
         }
 
-        public static T CreateWithParent<T, A>(Entity parent, A a, bool isFromPool = false) where T : Entity
+        public static T Create<T, A>(Entity parent, A a, bool isFromPool = false) where T : Entity
         {
             Type type = typeof (T);
             T component = (T) Entity.Create(type, isFromPool);
@@ -26,7 +26,7 @@ namespace ET
             return component;
         }
 
-        public static T CreateWithParent<T, A, B>(Entity parent, A a, B b, bool isFromPool = false) where T : Entity
+        public static T Create<T, A, B>(Entity parent, A a, B b, bool isFromPool = false) where T : Entity
         {
             Type type = typeof (T);
             T component = (T) Entity.Create(type, isFromPool);
@@ -37,7 +37,7 @@ namespace ET
             return component;
         }
 
-        public static T CreateWithParent<T, A, B, C>(Entity parent, A a, B b, C c, bool isFromPool = false) where T : Entity
+        public static T Create<T, A, B, C>(Entity parent, A a, B b, C c, bool isFromPool = false) where T : Entity
         {
             Type type = typeof (T);
             T component = (T) Entity.Create(type, isFromPool);
@@ -48,7 +48,7 @@ namespace ET
             return component;
         }
 
-        public static T CreateWithParent<T, A, B, C, D>(Entity parent, A a, B b, C c, D d, bool isFromPool = false) where T : Entity
+        public static T Create<T, A, B, C, D>(Entity parent, A a, B b, C c, D d, bool isFromPool = false) where T : Entity
         {
             Type type = typeof (T);
             T component = (T) Entity.Create(type, isFromPool);
@@ -59,7 +59,7 @@ namespace ET
             return component;
         }
 
-        public static T CreateWithParentAndId<T>(Entity parent, long id, bool isFromPool = false) where T : Entity
+        public static T CreateWithId<T>(Entity parent, long id, bool isFromPool = false) where T : Entity
         {
             Type type = typeof (T);
             T component = (T) Entity.Create(type, isFromPool);
@@ -70,7 +70,7 @@ namespace ET
             return component;
         }
 
-        public static T CreateWithParentAndId<T, A>(Entity parent, long id, A a, bool isFromPool = false) where T : Entity
+        public static T CreateWithId<T, A>(Entity parent, long id, A a, bool isFromPool = false) where T : Entity
         {
             Type type = typeof (T);
             T component = (T) Entity.Create(type, isFromPool);
@@ -81,7 +81,7 @@ namespace ET
             return component;
         }
 
-        public static T CreateWithParentAndId<T, A, B>(Entity parent, long id, A a, B b, bool isFromPool = false) where T : Entity
+        public static T CreateWithId<T, A, B>(Entity parent, long id, A a, B b, bool isFromPool = false) where T : Entity
         {
             Type type = typeof (T);
             T component = (T) Entity.Create(type, isFromPool);
@@ -92,7 +92,7 @@ namespace ET
             return component;
         }
 
-        public static T CreateWithParentAndId<T, A, B, C>(Entity parent, long id, A a, B b, C c, bool isFromPool = false) where T : Entity
+        public static T CreateWithId<T, A, B, C>(Entity parent, long id, A a, B b, C c, bool isFromPool = false) where T : Entity
         {
             Type type = typeof (T);
             T component = (T) Entity.Create(type, isFromPool);
@@ -103,7 +103,7 @@ namespace ET
             return component;
         }
 
-        public static T CreateWithParentAndId<T, A, B, C, D>(Entity parent, long id, A a, B b, C c, D d, bool isFromPool = false) where T : Entity
+        public static T CreateWithId<T, A, B, C, D>(Entity parent, long id, A a, B b, C c, D d, bool isFromPool = false) where T : Entity
         {
             Type type = typeof (T);
             T component = (T) Entity.Create(type, isFromPool);
@@ -113,95 +113,81 @@ namespace ET
             EventSystem.Instance.Awake(component, a, b, c, d);
             return component;
         }
-
-        public static T Create<T>(Entity domain, bool isFromPool = false) where T : Entity
-        {
-            Type type = typeof (T);
-            T component = (T) Entity.Create(type, isFromPool);
-            component.Domain = domain;
-            component.Id = IdGenerater.Instance.GenerateId();
-            EventSystem.Instance.Awake(component);
-            return component;
-        }
-
-        public static T Create<T, A>(Entity domain, A a, bool isFromPool = false) where T : Entity
-        {
-            Type type = typeof (T);
-            T component = (T) Entity.Create(type, isFromPool);
-            component.Domain = domain;
-            component.Id = IdGenerater.Instance.GenerateId();
-            EventSystem.Instance.Awake(component, a);
-            return component;
-        }
-
-        public static T Create<T, A, B>(Entity domain, A a, B b, bool isFromPool = false) where T : Entity
-        {
-            Type type = typeof (T);
-            T component = (T) Entity.Create(type, isFromPool);
-            component.Domain = domain;
-            component.Id = IdGenerater.Instance.GenerateId();
-            EventSystem.Instance.Awake(component, a, b);
-            return component;
-        }
-
-        public static T Create<T, A, B, C>(Entity domain, A a, B b, C c, bool isFromPool = false) where T : Entity
-        {
-            Type type = typeof (T);
-            T component = (T) Entity.Create(type, isFromPool);
-            component.Domain = domain;
-            component.Id = IdGenerater.Instance.GenerateId();
-            EventSystem.Instance.Awake(component, a, b, c);
-            return component;
-        }
         
-        public static T Create<T, A, B, C, D>(Entity domain, A a, B b, C c, D d, bool isFromPool = false) where T : Entity
-        {
-            Type type = typeof (T);
-            T component = (T) Entity.Create(type, isFromPool);
-            component.Domain = domain;
-            component.Id = IdGenerater.Instance.GenerateId();
-            EventSystem.Instance.Awake(component, a, b, c, d);
+        private static Entity Create(Type type, bool isFromPool)
+        {
+            Entity component;
+            if (isFromPool)
+            {
+                component = (Entity)ObjectPool.Instance.Fetch(type);
+            }
+            else
+            {
+                component = (Entity)Activator.CreateInstance(type);
+            }
+            component.IsFromPool = isFromPool;
+            component.IsCreate = true;
+            component.Id = 0;
+            return component;
+        }
+		
+        private Entity CreateWithComponentParent(Type type, bool isFromPool = true)
+        {
+            Entity component = Create(type, isFromPool);
+			
+            component.Id = this.Id;
+            component.ComponentParent = this;
+			
+            EventSystem.Instance.Awake(component);
             return component;
         }
 
-        public static T CreateWithId<T>(Entity domain, long id, bool isFromPool = false) where T : Entity
+        private T CreateWithComponentParent<T>(bool isFromPool = true) where T : Entity
         {
             Type type = typeof (T);
-            T component = (T) Entity.Create(type, isFromPool);
-            component.Domain = domain;
-            component.Id = id;
+            Entity component = Create(type, isFromPool);
+			
+            component.Id = this.Id;
+            component.ComponentParent = this;
+			
             EventSystem.Instance.Awake(component);
-            return component;
+            return (T)component;
         }
 
-        public static T CreateWithId<T, A>(Entity domain, long id, A a, bool isFromPool = false) where T : Entity
+        private T CreateWithComponentParent<T, A>(A a, bool isFromPool = true) where T : Entity
         {
             Type type = typeof (T);
-            T component = (T) Entity.Create(type, isFromPool);
-            component.Domain = domain;
-            component.Id = id;
+            Entity component = Create(type, isFromPool);
+			
+            component.Id = this.Id;
+            component.ComponentParent = this;
+			
             EventSystem.Instance.Awake(component, a);
-            return component;
+            return (T)component;
         }
 
-        public static T CreateWithId<T, A, B>(Entity domain, long id, A a, B b, bool isFromPool = false) where T : Entity
+        private T CreateWithComponentParent<T, A, B>(A a, B b, bool isFromPool = true) where T : Entity
         {
             Type type = typeof (T);
-            T component = (T) Entity.Create(type, isFromPool);
-            component.Domain = domain;
-            component.Id = id;
+            Entity component = Create(type, isFromPool);
+			
+            component.Id = this.Id;
+            component.ComponentParent = this;
+			
             EventSystem.Instance.Awake(component, a, b);
-            return component;
+            return (T)component;
         }
 
-        public static T CreateWithId<T, A, B, C>(Entity domain, long id, A a, B b, C c, bool isFromPool = false) where T : Entity
+        private T CreateWithComponentParent<T, A, B, C>(A a, B b, C c, bool isFromPool = true) where T : Entity
         {
             Type type = typeof (T);
-            T component = (T) Entity.Create(type, isFromPool);
-            component.Domain = domain;
-            component.Id = id;
+            Entity component = Create(type, isFromPool);
+			
+            component.Id = this.Id;
+            component.ComponentParent = this;
+			
             EventSystem.Instance.Awake(component, a, b, c);
-            return component;
+            return (T)component;
         }
     }
 }

+ 9 - 9
Unity/Assets/Model/Core/Object/EventSystem.cs

@@ -62,7 +62,7 @@ namespace ET
 			}
 		}
 		
-		private readonly Dictionary<long, Entity> allComponents = new Dictionary<long, Entity>();
+		private readonly Dictionary<long, Entity> allEntities = new Dictionary<long, Entity>();
 
 		private readonly Dictionary<string, Assembly> assemblies = new Dictionary<string, Assembly>();
 		
@@ -183,7 +183,7 @@ namespace ET
 				this.Remove(component.InstanceId);
 				return;
 			}
-			this.allComponents.Add(component.InstanceId, component);
+			this.allEntities.Add(component.InstanceId, component);
 			
 			Type type = component.GetType();
 
@@ -211,19 +211,19 @@ namespace ET
 
 		public void Remove(long instanceId)
 		{
-			this.allComponents.Remove(instanceId);
+			this.allEntities.Remove(instanceId);
 		}
 
 		public Entity Get(long instanceId)
 		{
 			Entity component = null;
-			this.allComponents.TryGetValue(instanceId, out component);
+			this.allEntities.TryGetValue(instanceId, out component);
 			return component;
 		}
 		
 		public bool IsRegister(long instanceId)
 		{
-			return this.allComponents.ContainsKey(instanceId);
+			return this.allEntities.ContainsKey(instanceId);
 		}
 		
 		public void Deserialize(Entity component)
@@ -388,7 +388,7 @@ namespace ET
 			{
 				long instanceId = this.loaders.Dequeue();
 				Entity component;
-				if (!this.allComponents.TryGetValue(instanceId, out component))
+				if (!this.allEntities.TryGetValue(instanceId, out component))
 				{
 					continue;
 				}
@@ -453,7 +453,7 @@ namespace ET
 			{
 				long instanceId = this.updates.Dequeue();
 				Entity component;
-				if (!this.allComponents.TryGetValue(instanceId, out component))
+				if (!this.allEntities.TryGetValue(instanceId, out component))
 				{
 					continue;
 				}
@@ -492,7 +492,7 @@ namespace ET
 			{
 				long instanceId = this.lateUpdates.Dequeue();
 				Entity component;
-				if (!this.allComponents.TryGetValue(instanceId, out component))
+				if (!this.allEntities.TryGetValue(instanceId, out component))
 				{
 					continue;
 				}
@@ -561,7 +561,7 @@ namespace ET
 			
 			HashSet<Type> noDomain = new HashSet<Type>();
 			
-			foreach (var kv in this.allComponents)
+			foreach (var kv in this.allEntities)
 			{
 				Type type = kv.Value.GetType();
 				if (kv.Value.Parent == null)

+ 3 - 3
Unity/Assets/Model/Module/CoroutineLock/CoroutineLockComponent.cs

@@ -10,7 +10,7 @@ namespace ET
             CoroutineLockComponent.Instance = self;
             for (int i = 0; i < self.list.Capacity; ++i)
             {
-                self.list.Add(EntityFactory.CreateWithId<CoroutineLockQueueType>(self.Domain, ++self.idGenerator));
+                self.list.Add(Entity.CreateWithId<CoroutineLockQueueType>(self, ++self.idGenerator));
             }
         }
     }
@@ -123,7 +123,7 @@ namespace ET
    
             if (!coroutineLockQueueType.TryGetValue(key, out CoroutineLockQueue queue))
             {
-                coroutineLockQueueType.Add(key, EntityFactory.CreateWithId<CoroutineLockQueue>(self.Domain, ++self.idGenerator, true));
+                coroutineLockQueueType.Add(key, Entity.CreateWithId<CoroutineLockQueue>(self, ++self.idGenerator, true));
                 return self.CreateCoroutineLock(coroutineLockType, key, time, 1);
             }
 
@@ -135,7 +135,7 @@ namespace ET
 
         public static CoroutineLock CreateCoroutineLock(this CoroutineLockComponent self, CoroutineLockType coroutineLockType, long key, int time, int count)
         {
-            CoroutineLock coroutineLock = EntityFactory.CreateWithId<CoroutineLock, CoroutineLockType, long, int>(self.Domain, ++self.idGenerator, coroutineLockType, key, count, true);
+            CoroutineLock coroutineLock = Entity.CreateWithId<CoroutineLock, CoroutineLockType, long, int>(self, ++self.idGenerator, coroutineLockType, key, count, true);
             if (time > 0)
             {
                 self.AddTimer(TimeHelper.ClientFrameTime() + time, coroutineLock);

+ 4 - 4
Unity/Assets/ModelView/Demo/Resource/ResourcesComponent.cs

@@ -401,7 +401,7 @@ namespace ET
 
                 if (realPath.Length > 0)
                 {
-                    abInfo = EntityFactory.CreateWithParent<ABInfo, string, AssetBundle>(this, assetBundleName, null);
+                    abInfo = Entity.Create<ABInfo, string, AssetBundle>(this, assetBundleName, null);
                     this.bundles[assetBundleName] = abInfo;
                     //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
                 }
@@ -442,7 +442,7 @@ namespace ET
                 }
             }
 
-            abInfo = EntityFactory.CreateWithParent<ABInfo, string, AssetBundle>(this, assetBundleName, assetBundle);
+            abInfo = Entity.Create<ABInfo, string, AssetBundle>(this, assetBundleName, assetBundle);
             this.bundles[assetBundleName] = abInfo;
 
             //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
@@ -525,7 +525,7 @@ namespace ET
 
                 if (realPath.Length > 0)
                 {
-                    abInfo = EntityFactory.CreateWithParent<ABInfo, string, AssetBundle>(this, assetBundleName, null);
+                    abInfo = Entity.Create<ABInfo, string, AssetBundle>(this, assetBundleName, null);
                     this.bundles[assetBundleName] = abInfo;
                     //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
                 }
@@ -563,7 +563,7 @@ namespace ET
                 return null;
             }
 
-            abInfo = EntityFactory.CreateWithParent<ABInfo, string, AssetBundle>(this, assetBundleName, assetBundle);
+            abInfo = Entity.Create<ABInfo, string, AssetBundle>(this, assetBundleName, assetBundle);
             this.bundles[assetBundleName] = abInfo;
             return abInfo;
             //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");

+ 1 - 1
Unity/Assets/Model/Core/Object/EntityCreateComponet.cs.meta → Unity/Assets/ModelView/Demo/Resource/ResourcesLoaderComponent.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: fa0ecfd3840da194da0529d01e974151
+guid: 0274407728a8f4020ab0703d2f6496a1
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 1 - 2
Unity/Assets/ModelView/Module/UI/UI.cs

@@ -57,7 +57,6 @@ namespace ET
 		public void Add(UI ui)
 		{
 			this.nameChildren.Add(ui.Name, ui);
-			ui.Parent = this;
 		}
 
 		public void Remove(string name)
@@ -83,7 +82,7 @@ namespace ET
 			{
 				return null;
 			}
-			child = EntityFactory.Create<UI, string, GameObject>(this.Domain, name, childGameObject);
+			child = Entity.Create<UI, string, GameObject>(this, name, childGameObject);
 			this.Add(child);
 			return child;
 		}