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

修复Entity类空异常报错 (#455)

* 修复Entity类空异常报错
susices 2 лет назад
Родитель
Сommit
f6e35057dc
1 измененных файлов с 21 добавлено и 15 удалено
  1. 21 15
      Unity/Assets/Scripts/Core/Module/Entity/Entity.cs

+ 21 - 15
Unity/Assets/Scripts/Core/Module/Entity/Entity.cs

@@ -912,17 +912,20 @@ namespace ET
         public override void BeginInit()
         {
             this.componentsDB?.Clear();
-            foreach (Entity entity in this.components.Values)
+            if (this.components!=null && this.components.Count!=0)
             {
-                if (entity is not ISerializeToEntity)
+                foreach (Entity entity in this.components.Values)
                 {
-                    continue;
-                }
+                    if (entity is not ISerializeToEntity)
+                    {
+                        continue;
+                    }
 
-                this.componentsDB ??= ObjectPool.Instance.Fetch<List<Entity>>();
-                this.componentsDB.Add(entity);
+                    this.componentsDB ??= ObjectPool.Instance.Fetch<List<Entity>>();
+                    this.componentsDB.Add(entity);
                 
-                entity.BeginInit();
+                    entity.BeginInit();
+                }
             }
 
             if (this.componentsDB != null && this.componentsDB.Count == 0)
@@ -932,18 +935,21 @@ namespace ET
             }
             
             this.childrenDB?.Clear();
-            foreach (Entity entity in this.children.Values)
+            if (this.children!=null && this.children.Count!=0)
             {
-                if (entity is not ISerializeToEntity)
+                foreach (Entity entity in this.children.Values)
                 {
-                    continue;
-                }
-                this.childrenDB ??= ObjectPool.Instance.Fetch<List<Entity>>();
-                this.childrenDB.Add(entity);
+                    if (entity is not ISerializeToEntity)
+                    {
+                        continue;
+                    }
+                    this.childrenDB ??= ObjectPool.Instance.Fetch<List<Entity>>();
+                    this.childrenDB.Add(entity);
                 
-                entity.BeginInit();
+                    entity.BeginInit();
+                }
             }
-            
+
             if (this.childrenDB != null && this.childrenDB.Count == 0)
             {
                 ObjectPool.Instance.Recycle(this.childrenDB);