Prechádzať zdrojové kódy

修改GetComponentSystem的实现,GetComponentSystem应该在this.components.TryGetValue之前触发
因为有可能components没有,但是执行GetComponentSystem后又有了

tanghai 2 rokov pred
rodič
commit
3b7c8662cc

+ 13 - 12
Unity/Assets/Scripts/Core/Entity/Entity.cs

@@ -652,18 +652,18 @@ namespace ET
                 return null;
             }
 
+            // 如果有IGetComponent接口,则触发GetComponentSystem
+            if (this is IGetComponent)
+            {
+                EntitySystemSingleton.Instance.GetComponent(this, typeof(K));
+            }
+            
             Entity component;
             if (!this.components.TryGetValue(typeof (K).FullName, out component))
             {
                 return default;
             }
 
-            // 如果有IGetComponent接口,则触发GetComponentSystem
-            if (this is IGetComponent)
-            {
-                EntitySystemSingleton.Instance.GetComponent(this, component);
-            }
-
             return (K) component;
         }
 
@@ -674,18 +674,19 @@ namespace ET
                 return null;
             }
 
+            // 如果有IGetComponent接口,则触发GetComponentSystem
+            // 这个要在tryget之前调用,因为有可能components没有,但是执行GetComponentSystem后又有了
+            if (this is IGetComponent)
+            {
+                EntitySystemSingleton.Instance.GetComponent(this, type);
+            }
+            
             Entity component;
             if (!this.components.TryGetValue(type.FullName, out component))
             {
                 return null;
             }
 
-            // 如果有IGetComponent接口,则触发GetComponentSystem
-            if (this is IGetComponent)
-            {
-                EntitySystemSingleton.Instance.GetComponent(this, component);
-            }
-
             return component;
         }
 

+ 2 - 2
Unity/Assets/Scripts/Core/Entity/EntitySystemSingleton.cs

@@ -92,7 +92,7 @@ namespace ET
         }
         
         // GetComponentSystem
-        public void GetComponent(Entity entity, Entity component)
+        public void GetComponent(Entity entity, Type type)
         {
             List<object> iGetSystem = this.TypeSystems.GetSystems(entity.GetType(), typeof (IGetComponentSystem));
             if (iGetSystem == null)
@@ -109,7 +109,7 @@ namespace ET
 
                 try
                 {
-                    getSystem.Run(entity, component);
+                    getSystem.Run(entity, type);
                 }
                 catch (Exception e)
                 {

+ 4 - 4
Unity/Assets/Scripts/Core/Entity/IGetComponentSystem.cs

@@ -12,15 +12,15 @@ namespace ET
 	
 	public interface IGetComponentSystem: ISystemType
 	{
-		void Run(Entity o, Entity component);
+		void Run(Entity o, Type type);
 	}
 
 	[EntitySystem]
 	public abstract class GetComponentSystem<T> : IGetComponentSystem where T: Entity, IGetComponent
 	{
-		void IGetComponentSystem.Run(Entity o, Entity component)
+		void IGetComponentSystem.Run(Entity o, Type type)
 		{
-			this.GetComponent((T)o, component);
+			this.GetComponent((T)o, type);
 		}
 
 		Type ISystemType.SystemType()
@@ -38,6 +38,6 @@ namespace ET
 			return typeof(T);
 		}
 
-		protected abstract void GetComponent(T self, Entity component);
+		protected abstract void GetComponent(T self, Type type);
 	}
 }