Sfoglia il codice sorgente

EntityRef优化,instanceId变化,把entity设置为null,这样解除引用,好让runtime gc

tanghai 2 anni fa
parent
commit
2ffa00b126
1 ha cambiato i file con 4 aggiunte e 3 eliminazioni
  1. 4 3
      Unity/Assets/Scripts/Core/Entity/EntityRef.cs

+ 4 - 3
Unity/Assets/Scripts/Core/Entity/EntityRef.cs

@@ -2,10 +2,10 @@ using System;
 
 namespace ET
 {
-    public readonly struct EntityRef<T> where T: Entity
+    public struct EntityRef<T> where T: Entity
     {
         private readonly long instanceId;
-        private readonly T entity;
+        private T entity;
 
         private EntityRef(T t)
         {
@@ -29,7 +29,8 @@ namespace ET
                 }
                 if (this.entity.InstanceId != this.instanceId)
                 {
-                    return null;
+                    // 这里instanceId变化了,设置为null,解除引用,好让runtime去gc
+                    this.entity = null;
                 }
                 return this.entity;
             }