Преглед на файлове

Entity Parent跟Domain做重大调整:
1. Entity IsRegister只跟parent一致,并且Entity的所有孩子都跟Entity保持一致
2. Entity Parent可以重新设置为null,设置为null则自己跟孩子都不会注册到EventSystem中,这样不用担心回收问题,因为没有注册就可以靠gc回收
3. 设置完Domain并不会设置Parent,也就不会注册。
4. 这样一来很清晰了,只有作为Scene的孩子或者孙子,entity才会注册到EventSystem中,光设置Domain是不行的

tanghai преди 4 години
родител
ревизия
8b6056023f
променени са 2 файла, в които са добавени 42 реда и са изтрити 44 реда
  1. 4 1
      Server/Hotfix/Demo/MessageHelper.cs
  2. 38 43
      Unity/Assets/Model/Core/Object/Entity.cs

+ 4 - 1
Server/Hotfix/Demo/MessageHelper.cs

@@ -8,7 +8,10 @@ namespace ET
         {
         {
             var units = unit.Domain.GetComponent<UnitComponent>().GetAll();
             var units = unit.Domain.GetComponent<UnitComponent>().GetAll();
 
 
-            if (units == null) return;
+            if (units == null)
+            {
+                return;
+            }
 
 
             foreach (Unit u in units)
             foreach (Unit u in units)
             {
             {

+ 38 - 43
Unity/Assets/Model/Core/Object/Entity.cs

@@ -136,35 +136,36 @@ namespace ET
             get => this.parent;
             get => this.parent;
             set
             set
             {
             {
-                if (value == null)
+                if (value == this)
                 {
                 {
-                    throw new Exception($"cant set parent null: {this.GetType().Name}");
+                    throw new Exception($"cant set parent self: {this.GetType().Name}");
                 }
                 }
 
 
                 if (this.parent != null) // 之前有parent
                 if (this.parent != null) // 之前有parent
                 {
                 {
                     // parent相同,不设置
                     // parent相同,不设置
-                    if (this.parent.InstanceId == value.InstanceId)
+                    if (value != null && this.parent.InstanceId == value.InstanceId)
                     {
                     {
                         Log.Error($"重复设置了Parent: {this.GetType().Name} parent: {this.parent.GetType().Name}");
                         Log.Error($"重复设置了Parent: {this.GetType().Name} parent: {this.parent.GetType().Name}");
                         return;
                         return;
                     }
                     }
 
 
                     this.parent.RemoveChild(this);
                     this.parent.RemoveChild(this);
-
-                    this.parent = value;
-                    this.parent.AddChild(this);
-
-                    this.Domain = this.parent.domain;
+                }
+                
+                
+                if (value == null)
+                {
+                    this.parent = null;
+                    this.IsComponent = false;
+                    this.Domain = null;
                 }
                 }
                 else
                 else
                 {
                 {
                     this.parent = value;
                     this.parent = value;
                     this.parent.AddChild(this);
                     this.parent.AddChild(this);
-
                     this.IsComponent = false;
                     this.IsComponent = false;
-
-                    AfterSetParent();
+                    this.Domain = this.parent.domain;
                 }
                 }
             }
             }
         }
         }
@@ -220,54 +221,48 @@ namespace ET
             get => this.domain;
             get => this.domain;
             set
             set
             {
             {
-                if (value == null)
+                if (this.InstanceId == 0)
                 {
                 {
-                    return;
+                    this.InstanceId = IdGenerater.Instance.GenerateInstanceId();
                 }
                 }
-
+                
                 Entity preDomain = this.domain;
                 Entity preDomain = this.domain;
                 this.domain = value;
                 this.domain = value;
-
-                //if (!(this.domain is Scene))
-                //{
-                //	throw new Exception($"domain is not scene: {this.GetType().Name}");
-                //}
-
-                if (preDomain == null)
+                
+                // 是否注册跟parent一致
+                if (this.parent != null)
                 {
                 {
-                    this.InstanceId = IdGenerater.Instance.GenerateInstanceId();
+                    this.IsRegister = this.Parent.IsRegister;
+                }
+                else
+                {
+                    this.IsRegister = false;
+                }
 
 
+                if (preDomain == null && !this.IsCreate)
+                {
                     // 反序列化出来的需要设置父子关系
                     // 反序列化出来的需要设置父子关系
-                    if (!this.IsCreate)
+                    if (this.componentsDB != null)
                     {
                     {
-                        if (this.componentsDB != null)
+                        foreach (Entity component in this.componentsDB)
                         {
                         {
-                            foreach (Entity component in this.componentsDB)
-                            {
-                                component.IsComponent = true;
-                                this.Components.Add(component.GetType(), component);
-                                component.parent = this;
-                            }
+                            component.IsComponent = true;
+                            this.Components.Add(component.GetType(), component);
+                            component.parent = this;
                         }
                         }
+                    }
 
 
-                        if (this.childrenDB != null)
+                    if (this.childrenDB != null)
+                    {
+                        foreach (Entity child in this.childrenDB)
                         {
                         {
-                            foreach (Entity child in this.childrenDB)
-                            {
-                                child.IsComponent = false;
-                                this.Children.Add(child.Id, child);
-                                child.parent = this;
-                            }
+                            child.IsComponent = false;
+                            this.Children.Add(child.Id, child);
+                            child.parent = this;
                         }
                         }
                     }
                     }
                 }
                 }
 
 
-                // 是否注册跟parent一致
-                if (this.parent != null)
-                {
-                    this.IsRegister = this.Parent.IsRegister;
-                }
-
                 // 递归设置孩子的Domain
                 // 递归设置孩子的Domain
                 if (this.children != null)
                 if (this.children != null)
                 {
                 {