Просмотр исходного кода

修改对象池机制,如果Entity没有使用对象池,那么挂载组件也不使用对象池

tanghai 7 лет назад
Родитель
Сommit
a883cffe0c

+ 5 - 5
Unity/Assets/Hotfix/Base/Object/Entity.cs

@@ -69,7 +69,7 @@ namespace ETHotfix
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}");
 			}
 
-			Component component = ComponentFactory.CreateWithParent(type, this);
+			Component component = ComponentFactory.CreateWithParent(type, this, this.IsFromPool);
 
 			this.componentDict.Add(type, component);
 			return component;
@@ -83,7 +83,7 @@ namespace ETHotfix
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
-			K component = ComponentFactory.CreateWithParent<K>(this);
+			K component = ComponentFactory.CreateWithParent<K>(this, this.IsFromPool);
 
 			this.componentDict.Add(type, component);
 			return component;
@@ -97,7 +97,7 @@ namespace ETHotfix
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
-			K component = ComponentFactory.CreateWithParent<K, P1>(this, p1);
+			K component = ComponentFactory.CreateWithParent<K, P1>(this, p1, this.IsFromPool);
 			
 			this.componentDict.Add(type, component);
 			return component;
@@ -111,7 +111,7 @@ namespace ETHotfix
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
-			K component = ComponentFactory.CreateWithParent<K, P1, P2>(this, p1, p2);
+			K component = ComponentFactory.CreateWithParent<K, P1, P2>(this, p1, p2, this.IsFromPool);
 			
 			this.componentDict.Add(type, component);
 			return component;
@@ -125,7 +125,7 @@ namespace ETHotfix
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
-			K component = ComponentFactory.CreateWithParent<K, P1, P2, P3>(this, p1, p2, p3);
+			K component = ComponentFactory.CreateWithParent<K, P1, P2, P3>(this, p1, p2, p3, this.IsFromPool);
 			
 			this.componentDict.Add(type, component);
 			return component;

+ 2 - 1
Unity/Assets/Hotfix/Base/Object/ObjectPool.cs

@@ -3,7 +3,7 @@ using System.Collections.Generic;
 
 namespace ETHotfix
 {
-	public class ObjectPool
+	public class ObjectPool: Component
 	{
 		private readonly Dictionary<Type, Queue<Component>> dictionary = new Dictionary<Type, Queue<Component>>();
 
@@ -36,6 +36,7 @@ namespace ETHotfix
         
 		public void Recycle(Component obj)
 		{
+			obj.Parent = this;
 			Type type = obj.GetType();
 			Queue<Component> queue;
 			if (!this.dictionary.TryGetValue(type, out queue))

+ 21 - 12
Unity/Assets/Hotfix/Entity/Game.cs

@@ -1,7 +1,19 @@
-namespace ETHotfix
+using UnityEngine;
+
+namespace ETHotfix
 {
 	public static class Game
 	{
+		private static EventSystem eventSystem;
+
+		public static EventSystem EventSystem
+		{
+			get
+			{
+				return eventSystem ?? (eventSystem = new EventSystem());
+			}
+		}
+		
 		private static Scene scene;
 
 		public static Scene Scene
@@ -18,23 +30,19 @@
 			}
 		}
 
-		private static EventSystem eventSystem;
-
-		public static EventSystem EventSystem
-		{
-			get
-			{
-				return eventSystem ?? (eventSystem = new EventSystem());
-			}
-		}
-
 		private static ObjectPool objectPool;
 
 		public static ObjectPool ObjectPool
 		{
 			get
 			{
-				return objectPool ?? (objectPool = new ObjectPool());
+				if (objectPool != null)
+				{
+					return objectPool;
+				}
+				objectPool = new ObjectPool();
+				objectPool.GameObject.transform.SetParent(GameObject.Find("/Global").transform);
+				return objectPool;
 			}
 		}
 
@@ -43,6 +51,7 @@
 			scene.Dispose();
 			scene = null;
 			eventSystem = null;
+			objectPool.Dispose();
 			objectPool = null;
 		}
 	}

+ 5 - 5
Unity/Assets/Model/Base/Object/Entity.cs

@@ -69,7 +69,7 @@ namespace ETModel
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}");
 			}
 
-			Component component = ComponentFactory.CreateWithParent(type, this);
+			Component component = ComponentFactory.CreateWithParent(type, this, this.IsFromPool);
 
 			this.componentDict.Add(type, component);
 			return component;
@@ -83,7 +83,7 @@ namespace ETModel
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
-			K component = ComponentFactory.CreateWithParent<K>(this);
+			K component = ComponentFactory.CreateWithParent<K>(this, this.IsFromPool);
 
 			this.componentDict.Add(type, component);
 			return component;
@@ -97,7 +97,7 @@ namespace ETModel
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
-			K component = ComponentFactory.CreateWithParent<K, P1>(this, p1);
+			K component = ComponentFactory.CreateWithParent<K, P1>(this, p1, this.IsFromPool);
 			
 			this.componentDict.Add(type, component);
 			return component;
@@ -111,7 +111,7 @@ namespace ETModel
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
-			K component = ComponentFactory.CreateWithParent<K, P1, P2>(this, p1, p2);
+			K component = ComponentFactory.CreateWithParent<K, P1, P2>(this, p1, p2, this.IsFromPool);
 			
 			this.componentDict.Add(type, component);
 			return component;
@@ -125,7 +125,7 @@ namespace ETModel
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
-			K component = ComponentFactory.CreateWithParent<K, P1, P2, P3>(this, p1, p2, p3);
+			K component = ComponentFactory.CreateWithParent<K, P1, P2, P3>(this, p1, p2, p3, this.IsFromPool);
 			
 			this.componentDict.Add(type, component);
 			return component;

+ 2 - 1
Unity/Assets/Model/Base/Object/ObjectPool.cs

@@ -3,7 +3,7 @@ using System.Collections.Generic;
 
 namespace ETModel
 {
-    public class ObjectPool
+    public class ObjectPool: Component
     {
         private readonly Dictionary<Type, Queue<Component>> dictionary = new Dictionary<Type, Queue<Component>>();
 
@@ -36,6 +36,7 @@ namespace ETModel
         
         public void Recycle(Component obj)
         {
+	        obj.Parent = this;
             Type type = obj.GetType();
 	        Queue<Component> queue;
             if (!this.dictionary.TryGetValue(type, out queue))

+ 3 - 4
Unity/Assets/Model/Component/SceneChangeComponent.cs

@@ -53,14 +53,13 @@ namespace ETModel
 			{
 				return;
 			}
-			base.Dispose();
 
 			if (this.Entity.IsDisposed)
 			{
-				return;
+				this.Entity.RemoveComponent<SceneChangeComponent>();
 			}
-			
-			this.Entity.RemoveComponent<SceneChangeComponent>();
+
+			base.Dispose();
 		}
 	}
 }

+ 26 - 14
Unity/Assets/Model/Entity/Game.cs

@@ -1,7 +1,19 @@
-namespace ETModel
+using UnityEngine;
+
+namespace ETModel
 {
 	public static class Game
 	{
+		private static EventSystem eventSystem;
+
+		public static EventSystem EventSystem
+		{
+			get
+			{
+				return eventSystem ?? (eventSystem = new EventSystem());
+			}
+		}
+		
 		private static Scene scene;
 
 		public static Scene Scene
@@ -13,28 +25,24 @@
 					return scene;
 				}
 				scene = new Scene();
-				scene.GameObject.transform.SetParent(scene.GameObject.transform.Find("/Global"));
+				scene.GameObject.transform.SetParent(GameObject.Find("/Global").transform);
 				return scene;
 			}
 		}
 
-		private static EventSystem eventSystem;
-
-		public static EventSystem EventSystem
-		{
-			get
-			{
-				return eventSystem ?? (eventSystem = new EventSystem());
-			}
-		}
-
 		private static ObjectPool objectPool;
 
 		public static ObjectPool ObjectPool
 		{
 			get
 			{
-				return objectPool ?? (objectPool = new ObjectPool());
+				if (objectPool != null)
+				{
+					return objectPool;
+				}
+				objectPool = new ObjectPool();
+				objectPool.GameObject.transform.SetParent(GameObject.Find("/Global").transform);
+				return objectPool;
 			}
 		}
 
@@ -50,10 +58,14 @@
 
 		public static void Close()
 		{
-			scene.Dispose();
 			eventSystem = null;
+			
+			scene.Dispose();
 			scene = null;
+			
+			objectPool.Dispose();
 			objectPool = null;
+			
 			hotfix = null;
 		}
 	}

+ 2 - 3
Unity/Assets/Model/Module/Message/Session.cs

@@ -64,8 +64,8 @@ namespace ETModel
 				return;
 			}
 
-			long id = this.Id;
-
+			this.Network.Remove(this.Id);
+			
 			base.Dispose();
 			
 			foreach (Action<IResponse> action in this.requestCallback.Values.ToArray())
@@ -80,7 +80,6 @@ namespace ETModel
 			//}
 			
 			this.channel.Dispose();
-			this.Network.Remove(id);
 			this.requestCallback.Clear();
 		}
 

BIN
Unity/Assets/Res/Code/Hotfix.mdb.bytes