Pārlūkot izejas kodu

完善了ObjectPool的可视化

tanghai 7 gadi atpakaļ
vecāks
revīzija
92b65e08c6

+ 1 - 0
Server/App/Program.cs

@@ -35,6 +35,7 @@ namespace App
 
 				Log.Info($"server start........................ {startConfig.AppId} {startConfig.AppType}");
 
+				Game.Scene.AddComponent<TimerComponent>();
 				Game.Scene.AddComponent<OpcodeTypeComponent>();
 				Game.Scene.AddComponent<MessageDispatcherComponent>();
 

+ 1 - 2
Server/Model/Entity/Game.cs

@@ -12,8 +12,7 @@
 				{
 					return scene;
 				}
-				scene = new Scene() { Name = "ClientModel" };
-				scene.AddComponent<TimerComponent>();
+				scene = new Scene();
 				return scene;
 			}
 		}

+ 6 - 6
Unity/Assets/Hotfix/Base/Object/Component.cs

@@ -14,6 +14,8 @@ namespace ETHotfix
 		public long InstanceId { get; protected set; }
 		
 #if !SERVER
+		public static GameObject Global { get; } = GameObject.Find("/Global");
+		
 		[BsonIgnore]
 		public GameObject GameObject { get; protected set; }
 #endif
@@ -69,16 +71,13 @@ namespace ETHotfix
 #if !SERVER
 				if (this.parent == null)
 				{
-					this.GameObject.transform.SetParent(GameObject.Find("/Global").transform, false);
+					this.GameObject.transform.SetParent(Global.transform, false);
 					return;
 				}
 
-				if (this.GameObject != null)
+				if (this.GameObject != null && this.parent.GameObject != null)
 				{
-					if (this.parent.GameObject != null)
-					{
-						this.GameObject.transform.SetParent(this.parent.GameObject.transform, false);
-					}
+					this.GameObject.transform.SetParent(this.parent.GameObject.transform, false);
 				}
 #endif
 			}
@@ -107,6 +106,7 @@ namespace ETHotfix
 				this.GameObject = new GameObject();
 				this.GameObject.name = this.GetType().Name;
 				this.GameObject.layer = LayerNames.GetLayerInt(LayerNames.HIDDEN);
+				this.GameObject.transform.SetParent(Global.transform, false);
 				this.GameObject.AddComponent<ComponentView>().Component = this;
 			}
 #endif

+ 92 - 41
Unity/Assets/Hotfix/Base/Object/ObjectPool.cs

@@ -3,61 +3,112 @@ using System.Collections.Generic;
 
 namespace ETHotfix
 {
-	public class ObjectPool: Component
+	public class ComponentQueue: Component
 	{
-		private readonly Dictionary<Type, Queue<Component>> dictionary = new Dictionary<Type, Queue<Component>>();
+		public string TypeName { get; }
+		
+		private readonly Queue<Component> queue = new Queue<Component>();
 
-		public Component Fetch(Type type)
+		public ComponentQueue(string typeName)
 		{
-			Queue<Component> queue;
-			if (!this.dictionary.TryGetValue(type, out queue))
-			{
-				queue = new Queue<Component>();
-				this.dictionary.Add(type, queue);
-			}
-			Component obj;
-			if (queue.Count > 0)
-			{
-				obj = queue.Dequeue();
-			}
-			else
-			{
-				obj = (Component)Activator.CreateInstance(type);	
-			}
-			obj.IsFromPool = true;
-			return obj;
+			this.TypeName = typeName;
 		}
 
-		public T Fetch<T>() where T: Component
+		public void Enqueue(Component component)
 		{
-			T t = (T) this.Fetch(typeof(T));
-			return t;
+			component.Parent = this;
+			this.queue.Enqueue(component);
 		}
-        
-		public void Recycle(Component obj)
+
+		public Component Dequeue()
+		{
+			return this.queue.Dequeue();
+		}
+
+		public Component Peek()
 		{
-			obj.Parent = this;
-			Type type = obj.GetType();
-			Queue<Component> queue;
-			if (!this.dictionary.TryGetValue(type, out queue))
+			return this.queue.Peek();
+		}
+
+		public int Count
+		{
+			get
 			{
-				queue = new Queue<Component>();
-				this.dictionary.Add(type, queue);
+				return this.queue.Count;
 			}
-			queue.Enqueue(obj);
 		}
-		
-		public void Clear()
+
+		public override void Dispose()
 		{
-			foreach (var kv in this.dictionary)
+			if (this.IsDisposed)
 			{
-				foreach (Component component in kv.Value)
-				{
-					component.IsFromPool = false;
-					component.Dispose();
-				}
+				return;
+			}
+			base.Dispose();
+
+			while (this.queue.Count > 0)
+			{
+				Component component = this.queue.Dequeue();
+				component.IsFromPool = false;
+				component.Dispose();
 			}
-			this.dictionary.Clear();
 		}
 	}
+	
+    public class ObjectPool: Component
+    {
+	    public string Name { get; set; }
+	    
+        private readonly Dictionary<Type, ComponentQueue> dictionary = new Dictionary<Type, ComponentQueue>();
+
+        public Component Fetch(Type type)
+        {
+	        Component obj;
+            if (!this.dictionary.TryGetValue(type, out ComponentQueue queue))
+            {
+	            obj = (Component)Activator.CreateInstance(type);
+            }
+	        else if (queue.Count == 0)
+            {
+	            obj = (Component)Activator.CreateInstance(type);
+            }
+            else
+            {
+	            obj = queue.Dequeue();
+            }
+	        
+	        obj.IsFromPool = true;
+            return obj;
+        }
+
+        public T Fetch<T>() where T: Component
+		{
+            T t = (T) this.Fetch(typeof(T));
+			return t;
+		}
+        
+        public void Recycle(Component obj)
+        {
+	        obj.Parent = this;
+            Type type = obj.GetType();
+	        ComponentQueue queue;
+            if (!this.dictionary.TryGetValue(type, out queue))
+            {
+                queue = new ComponentQueue(type.Name);
+	            queue.Parent = this;
+				this.dictionary.Add(type, queue);
+            }
+            queue.Enqueue(obj);
+        }
+
+	    public void Clear()
+	    {
+		    foreach (var kv in this.dictionary)
+		    {
+			    kv.Value.IsFromPool = false;
+			    kv.Value.Dispose();
+		    }
+		    this.dictionary.Clear();
+	    }
+    }
 }

+ 2 - 4
Unity/Assets/Hotfix/Entity/Game.cs

@@ -24,8 +24,7 @@ namespace ETHotfix
 				{
 					return scene;
 				}
-				scene = new Scene() { Name = "ClientHotfix" };
-				scene.GameObject.transform.SetParent(scene.GameObject.transform.Find("/Global"));
+				scene = new Scene() { Name = "ClientH" };
 				return scene;
 			}
 		}
@@ -40,8 +39,7 @@ namespace ETHotfix
 				{
 					return objectPool;
 				}
-				objectPool = new ObjectPool();
-				objectPool.GameObject.transform.SetParent(GameObject.Find("/Global").transform);
+				objectPool = new ObjectPool() { Name = "ClientH" };
 				return objectPool;
 			}
 		}

+ 1 - 1
Unity/Assets/Hotfix/Module/UI/UIComponent.cs

@@ -9,7 +9,7 @@ namespace ETHotfix
 	{
 		public override void Awake(UIComponent self)
 		{
-			self.Camera = GameObject.Find("/Global/UICamera");
+			self.Camera = Component.Global.transform.Find("UICamera").gameObject;
 		}
 	}
 	

+ 7 - 12
Unity/Assets/Model/Base/Object/Component.cs

@@ -14,6 +14,8 @@ namespace ETModel
 		public long InstanceId { get; protected set; }
 		
 #if !SERVER
+		public static GameObject Global { get; } = GameObject.Find("/Global");
+		
 		[BsonIgnore]
 		public GameObject GameObject { get; protected set; }
 #endif
@@ -69,23 +71,16 @@ namespace ETModel
 #if !SERVER
 				if (this.parent == null)
 				{
-					this.GameObject.transform.SetParent(GameObject.Find("/Global").transform, false);
+					this.GameObject.transform.SetParent(Global.transform, false);
 					return;
 				}
 
-				if (this.GameObject == null)
+				if (this.GameObject != null && this.parent.GameObject != null)
 				{
-					return;
+					this.GameObject.transform.SetParent(this.parent.GameObject.transform, false);
 				}
-
-				if (this.parent.GameObject == null)
-				{
-					return;
-				}
-
-				this.GameObject.transform.SetParent(this.parent.GameObject.transform, false);
 #endif
-			} 
+			}
 		}
 
 		public T GetParent<T>() where T : Component
@@ -111,6 +106,7 @@ namespace ETModel
 				this.GameObject = new GameObject();
 				this.GameObject.name = this.GetType().Name;
 				this.GameObject.layer = LayerNames.GetLayerInt(LayerNames.HIDDEN);
+				this.GameObject.transform.SetParent(Global.transform, false);
 				this.GameObject.AddComponent<ComponentView>().Component = this;
 			}
 #endif
@@ -138,7 +134,6 @@ namespace ETModel
 			else
 			{
 #if !SERVER
-
 				if (this.GameObject != null)
 				{
 					UnityEngine.Object.Destroy(this.GameObject);

+ 70 - 19
Unity/Assets/Model/Base/Object/ObjectPool.cs

@@ -3,27 +3,80 @@ using System.Collections.Generic;
 
 namespace ETModel
 {
+	public class ComponentQueue: Component
+	{
+		public string TypeName { get; }
+		
+		private readonly Queue<Component> queue = new Queue<Component>();
+
+		public ComponentQueue(string typeName)
+		{
+			this.TypeName = typeName;
+		}
+
+		public void Enqueue(Component component)
+		{
+			component.Parent = this;
+			this.queue.Enqueue(component);
+		}
+
+		public Component Dequeue()
+		{
+			return this.queue.Dequeue();
+		}
+
+		public Component Peek()
+		{
+			return this.queue.Peek();
+		}
+
+		public int Count
+		{
+			get
+			{
+				return this.queue.Count;
+			}
+		}
+
+		public override void Dispose()
+		{
+			if (this.IsDisposed)
+			{
+				return;
+			}
+			base.Dispose();
+
+			while (this.queue.Count > 0)
+			{
+				Component component = this.queue.Dequeue();
+				component.IsFromPool = false;
+				component.Dispose();
+			}
+		}
+	}
+	
     public class ObjectPool: Component
     {
-        private readonly Dictionary<Type, Queue<Component>> dictionary = new Dictionary<Type, Queue<Component>>();
+	    public string Name { get; set; }
+	    
+        private readonly Dictionary<Type, ComponentQueue> dictionary = new Dictionary<Type, ComponentQueue>();
 
         public Component Fetch(Type type)
         {
-	        Queue<Component> queue;
-            if (!this.dictionary.TryGetValue(type, out queue))
+	        Component obj;
+            if (!this.dictionary.TryGetValue(type, out ComponentQueue queue))
             {
-                queue = new Queue<Component>();
-                this.dictionary.Add(type, queue);
+	            obj = (Component)Activator.CreateInstance(type);
             }
-	        Component obj;
-			if (queue.Count > 0)
+	        else if (queue.Count == 0)
             {
-				obj = queue.Dequeue();
+	            obj = (Component)Activator.CreateInstance(type);
             }
-			else
-			{
-				obj = (Component)Activator.CreateInstance(type);	
-			}
+            else
+            {
+	            obj = queue.Dequeue();
+            }
+	        
 	        obj.IsFromPool = true;
             return obj;
         }
@@ -38,10 +91,11 @@ namespace ETModel
         {
 	        obj.Parent = this;
             Type type = obj.GetType();
-	        Queue<Component> queue;
+	        ComponentQueue queue;
             if (!this.dictionary.TryGetValue(type, out queue))
             {
-                queue = new Queue<Component>();
+                queue = new ComponentQueue(type.Name);
+	            queue.Parent = this;
 				this.dictionary.Add(type, queue);
             }
             queue.Enqueue(obj);
@@ -51,11 +105,8 @@ namespace ETModel
 	    {
 		    foreach (var kv in this.dictionary)
 		    {
-			    foreach (Component component in kv.Value)
-			    {
-				    component.IsFromPool = false;
-				    component.Dispose();
-			    }
+			    kv.Value.IsFromPool = false;
+			    kv.Value.Dispose();
 		    }
 		    this.dictionary.Clear();
 	    }

+ 2 - 4
Unity/Assets/Model/Entity/Game.cs

@@ -24,8 +24,7 @@ namespace ETModel
 				{
 					return scene;
 				}
-				scene = new Scene();
-				scene.GameObject.transform.SetParent(GameObject.Find("/Global").transform);
+				scene = new Scene() { Name = "ClientM" };
 				return scene;
 			}
 		}
@@ -40,8 +39,7 @@ namespace ETModel
 				{
 					return objectPool;
 				}
-				objectPool = new ObjectPool();
-				objectPool.GameObject.transform.SetParent(GameObject.Find("/Global").transform);
+				objectPool = new ObjectPool() { Name = "ClientM" };
 				return objectPool;
 			}
 		}

+ 1 - 1
Unity/Assets/Model/Module/UI/UIComponent.cs

@@ -8,7 +8,7 @@ namespace ETModel
 	{
 		public override void Awake(UIComponent self)
 		{
-			self.Camera = GameObject.Find("/Global/UICamera");
+			self.Camera = Component.Global.transform.Find("UICamera").gameObject;
 		}
 	}