| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using UnityEngine.Assertions; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public interface IPooledObjectInstantiator |
| | 10 | | { |
| | 11 | | bool IsValid(GameObject original); |
| | 12 | | GameObject Instantiate(GameObject gameObject); |
| | 13 | | } |
| | 14 | |
|
| | 15 | | public class Pool : ICleanable |
| | 16 | | { |
| | 17 | | public delegate void OnReleaseAllDlg(Pool pool); |
| | 18 | |
|
| | 19 | | public const int PREWARM_ACTIVE_MULTIPLIER = 2; |
| | 20 | | public object id; |
| | 21 | | public GameObject original; |
| | 22 | | public GameObject container; |
| | 23 | |
|
| | 24 | | public bool persistent = false; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// If this is set to true, all Unity components in the poolable gameObject implementing ILifecycleHandler |
| | 28 | | /// will be registered and called when necessary. |
| | 29 | | /// |
| | 30 | | /// The interface call responsibility lies in the PoolableObject class. |
| | 31 | | /// </summary> |
| | 32 | | public bool useLifecycleHandlers = false; |
| | 33 | |
|
| | 34 | | public System.Action<Pool> OnCleanup; |
| | 35 | |
|
| | 36 | | public IPooledObjectInstantiator instantiator; |
| | 37 | |
|
| 1796 | 38 | | private readonly LinkedList<PoolableObject> unusedObjects = new LinkedList<PoolableObject>(); |
| 1796 | 39 | | private readonly LinkedList<PoolableObject> usedObjects = new LinkedList<PoolableObject>(); |
| | 40 | |
|
| | 41 | | private int maxPrewarmCount = 0; |
| | 42 | | private bool isInitialized; |
| | 43 | |
|
| 0 | 44 | | public float lastGetTime { get; private set; } |
| | 45 | |
|
| 0 | 46 | | public int objectsCount => unusedObjectsCount + usedObjectsCount; |
| | 47 | |
|
| 0 | 48 | | public int unusedObjectsCount { get { return unusedObjects.Count; } } |
| | 49 | |
|
| 0 | 50 | | public int usedObjectsCount { get { return usedObjects.Count; } } |
| | 51 | |
|
| 1796 | 52 | | public Pool(string name, int maxPrewarmCount) |
| | 53 | | { |
| 1796 | 54 | | if (PoolManager.USE_POOL_CONTAINERS) |
| 1796 | 55 | | container = new GameObject("Pool - " + name); |
| | 56 | |
|
| 1796 | 57 | | this.maxPrewarmCount = maxPrewarmCount; |
| 1796 | 58 | | } |
| | 59 | |
|
| | 60 | | public void ForcePrewarm() |
| | 61 | | { |
| 2161 | 62 | | if (maxPrewarmCount <= objectsCount) |
| 2161 | 63 | | return; |
| | 64 | |
|
| 0 | 65 | | int objectsToInstantiate = Mathf.Max(0, maxPrewarmCount - objectsCount); |
| 0 | 66 | | for (int i = 0; i < objectsToInstantiate; i++) |
| | 67 | | { |
| 0 | 68 | | Instantiate(); |
| | 69 | | } |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | public PoolableObject Get() |
| | 73 | | { |
| | 74 | | // These extra instantiations during initialization are to populate pools that will be used a lot later |
| 852 | 75 | | if (PoolManager.i.initializing && !isInitialized) |
| | 76 | | { |
| 0 | 77 | | isInitialized = true; |
| 0 | 78 | | int count = usedObjectsCount; |
| | 79 | |
|
| 0 | 80 | | for (int i = unusedObjectsCount; i < Mathf.Min(count * PREWARM_ACTIVE_MULTIPLIER, maxPrewarmCount); i++) |
| | 81 | | { |
| 0 | 82 | | Instantiate(); |
| | 83 | | } |
| | 84 | |
|
| 0 | 85 | | Instantiate(); |
| 0 | 86 | | } |
| 852 | 87 | | else if (unusedObjects.Count == 0) |
| | 88 | | { |
| 805 | 89 | | Instantiate(); |
| | 90 | | } |
| | 91 | |
|
| 852 | 92 | | PoolableObject poolable = Extract(); |
| | 93 | |
|
| 852 | 94 | | EnablePoolableObject(poolable); |
| 852 | 95 | | poolable.OnPoolGet(); |
| 852 | 96 | | return poolable; |
| | 97 | | } |
| | 98 | |
|
| | 99 | | private PoolableObject Extract() |
| | 100 | | { |
| 852 | 101 | | PoolableObject po = null; |
| 852 | 102 | | po = unusedObjects.First.Value; |
| 852 | 103 | | unusedObjects.RemoveFirst(); |
| 852 | 104 | | po.node = usedObjects.AddFirst(po); |
| | 105 | |
|
| | 106 | | #if UNITY_EDITOR |
| 852 | 107 | | RefreshName(); |
| | 108 | | #endif |
| 852 | 109 | | return po; |
| | 110 | | } |
| | 111 | |
|
| | 112 | | public PoolableObject Instantiate() |
| | 113 | | { |
| 805 | 114 | | var gameObject = InstantiateAsOriginal(); |
| 805 | 115 | | return SetupPoolableObject(gameObject); |
| | 116 | | } |
| | 117 | |
|
| | 118 | | public GameObject InstantiateAsOriginal() |
| | 119 | | { |
| 827 | 120 | | Assert.IsTrue(original != null, $"Original should never be null here ({id})"); |
| | 121 | |
|
| 827 | 122 | | GameObject gameObject = null; |
| | 123 | |
|
| 827 | 124 | | if (instantiator != null) |
| 122 | 125 | | gameObject = instantiator.Instantiate(original); |
| | 126 | | else |
| 705 | 127 | | gameObject = GameObject.Instantiate(original); |
| | 128 | |
|
| 827 | 129 | | gameObject.SetActive(true); |
| | 130 | |
|
| 827 | 131 | | return gameObject; |
| | 132 | | } |
| | 133 | |
|
| | 134 | | private PoolableObject SetupPoolableObject(GameObject gameObject, bool active = false) |
| | 135 | | { |
| 806 | 136 | | if (PoolManager.i.poolables.ContainsKey(gameObject)) |
| 0 | 137 | | return PoolManager.i.GetPoolable(gameObject); |
| | 138 | |
|
| 806 | 139 | | PoolableObject poolable = new PoolableObject(this, gameObject); |
| 806 | 140 | | PoolManager.i.poolables.Add(gameObject, poolable); |
| 806 | 141 | | PoolManager.i.poolableValues.Add(poolable); |
| | 142 | |
|
| 806 | 143 | | if (!active) |
| | 144 | | { |
| 805 | 145 | | DisablePoolableObject(poolable); |
| 805 | 146 | | poolable.node = unusedObjects.AddFirst(poolable); |
| 805 | 147 | | } |
| | 148 | | else |
| | 149 | | { |
| 1 | 150 | | EnablePoolableObject(poolable); |
| 1 | 151 | | poolable.node = usedObjects.AddFirst(poolable); |
| | 152 | | } |
| | 153 | |
|
| | 154 | | #if UNITY_EDITOR |
| 806 | 155 | | RefreshName(); |
| | 156 | | #endif |
| 806 | 157 | | return poolable; |
| | 158 | | } |
| | 159 | |
|
| | 160 | | public void Release(PoolableObject poolable) |
| | 161 | | { |
| 853 | 162 | | if (poolable == null || !PoolManager.i.HasPoolable(poolable)) |
| 0 | 163 | | return; |
| | 164 | |
|
| 853 | 165 | | DisablePoolableObject(poolable); |
| | 166 | |
|
| 853 | 167 | | poolable.node.List.Remove(poolable.node); |
| 853 | 168 | | poolable.node = unusedObjects.AddFirst(poolable); |
| | 169 | |
|
| | 170 | | #if UNITY_EDITOR |
| 853 | 171 | | RefreshName(); |
| | 172 | | #endif |
| 853 | 173 | | } |
| | 174 | |
|
| | 175 | | public void ReleaseAll() |
| | 176 | | { |
| 2604 | 177 | | while (usedObjects.Count > 0) |
| | 178 | | { |
| 655 | 179 | | usedObjects.First.Value.Release(); |
| | 180 | | } |
| 1949 | 181 | | } |
| | 182 | |
|
| | 183 | | /// <summary> |
| | 184 | | /// This will add a gameObject that is not on any pool to this pool. |
| | 185 | | /// </summary> |
| | 186 | | /// <param name="gameObject"></param> |
| | 187 | | public void AddToPool(GameObject gameObject, bool addActive = true) |
| | 188 | | { |
| 1 | 189 | | if (instantiator != null && !instantiator.IsValid(gameObject)) |
| | 190 | | { |
| 0 | 191 | | Debug.LogError($"ERROR: Trying to add invalid gameObject to pool! -- {gameObject.name}", gameObject); |
| 0 | 192 | | return; |
| | 193 | | } |
| | 194 | |
|
| 1 | 195 | | PoolableObject obj = PoolManager.i.GetPoolable(gameObject); |
| | 196 | |
|
| 1 | 197 | | if (obj != null) |
| | 198 | | { |
| 0 | 199 | | Debug.LogError($"ERROR: gameObject is already being tracked by a pool! -- {gameObject.name}", gameObject |
| 0 | 200 | | return; |
| | 201 | | } |
| | 202 | |
|
| 1 | 203 | | SetupPoolableObject(gameObject, addActive); |
| 1 | 204 | | } |
| | 205 | |
|
| | 206 | | public void RemoveFromPool(PoolableObject poolable) |
| | 207 | | { |
| 2 | 208 | | if (poolable.node != null) |
| | 209 | | { |
| 2 | 210 | | if (poolable.node.List != null) |
| 0 | 211 | | poolable.node.List.Remove(poolable); |
| | 212 | |
|
| 2 | 213 | | poolable.node = null; |
| | 214 | | } |
| | 215 | |
|
| 2 | 216 | | PoolManager.i.poolables.Remove(poolable.gameObject); |
| 2 | 217 | | PoolManager.i.poolableValues.Remove(poolable); |
| | 218 | | #if UNITY_EDITOR |
| 2 | 219 | | RefreshName(); |
| | 220 | | #endif |
| 2 | 221 | | } |
| | 222 | |
|
| | 223 | | public void Cleanup() |
| | 224 | | { |
| 1796 | 225 | | ReleaseAll(); |
| | 226 | |
|
| 2602 | 227 | | while (unusedObjects.Count > 0) |
| | 228 | | { |
| 806 | 229 | | PoolManager.i.poolables.Remove(unusedObjects.First.Value.gameObject); |
| 806 | 230 | | PoolManager.i.poolableValues.Remove(unusedObjects.First.Value); |
| 806 | 231 | | unusedObjects.RemoveFirst(); |
| | 232 | | } |
| | 233 | |
|
| 1796 | 234 | | while (usedObjects.Count > 0) |
| | 235 | | { |
| 0 | 236 | | PoolManager.i.poolables.Remove(usedObjects.First.Value.gameObject); |
| 0 | 237 | | PoolManager.i.poolableValues.Remove(usedObjects.First.Value); |
| 0 | 238 | | usedObjects.RemoveFirst(); |
| | 239 | | } |
| | 240 | |
|
| 1796 | 241 | | unusedObjects.Clear(); |
| 1796 | 242 | | usedObjects.Clear(); |
| | 243 | |
|
| 1796 | 244 | | Object.Destroy(this.original); |
| | 245 | |
|
| 1796 | 246 | | if (PoolManager.USE_POOL_CONTAINERS) |
| 1796 | 247 | | Object.Destroy(this.container); |
| | 248 | |
|
| 1796 | 249 | | OnCleanup?.Invoke(this); |
| 109 | 250 | | } |
| | 251 | |
|
| | 252 | | public void EnablePoolableObject(PoolableObject poolable) |
| | 253 | | { |
| 853 | 254 | | GameObject go = poolable.gameObject; |
| | 255 | |
|
| 853 | 256 | | if (go == null) |
| 0 | 257 | | return; |
| | 258 | |
|
| 853 | 259 | | if (!go.activeSelf) |
| 852 | 260 | | go.SetActive(true); |
| | 261 | |
|
| 853 | 262 | | go.transform.ResetLocalTRS(); |
| | 263 | |
|
| 853 | 264 | | lastGetTime = Time.unscaledTime; |
| 853 | 265 | | } |
| | 266 | |
|
| | 267 | | public void DisablePoolableObject(PoolableObject poolable) |
| | 268 | | { |
| | 269 | | #if UNITY_STANDALONE || UNITY_EDITOR |
| 1658 | 270 | | if (DataStore.i.common.isApplicationQuitting.Get()) |
| 0 | 271 | | return; |
| | 272 | | #endif |
| 1658 | 273 | | GameObject go = poolable.gameObject; |
| | 274 | |
|
| 1658 | 275 | | if (go == null) |
| 56 | 276 | | return; |
| | 277 | |
|
| 1602 | 278 | | if (go.activeSelf) |
| 1600 | 279 | | go.SetActive(false); |
| | 280 | |
|
| 1602 | 281 | | if (PoolManager.USE_POOL_CONTAINERS) |
| | 282 | | { |
| 1602 | 283 | | if (container != null) |
| | 284 | | { |
| 1602 | 285 | | go.transform.SetParent(container.transform); |
| | 286 | | } |
| 1602 | 287 | | } |
| | 288 | | else |
| | 289 | | { |
| 0 | 290 | | go.transform.SetParent(null); |
| | 291 | | } |
| 0 | 292 | | } |
| | 293 | |
|
| | 294 | | #if UNITY_EDITOR |
| | 295 | | private void RefreshName() |
| | 296 | | { |
| 2513 | 297 | | if (this.container != null) |
| 2513 | 298 | | this.container.name = $"in: {unusedObjectsCount} out: {usedObjectsCount} id: {id} persistent: {persisten |
| 2513 | 299 | | } |
| | 300 | | #endif |
| | 301 | | public static bool FindPoolInGameObject(GameObject gameObject, out Pool pool) |
| | 302 | | { |
| 1 | 303 | | pool = null; |
| | 304 | |
|
| 1 | 305 | | if (PoolManager.i.poolables.TryGetValue(gameObject, out PoolableObject poolable)) |
| | 306 | | { |
| 0 | 307 | | pool = poolable.pool; |
| 0 | 308 | | return true; |
| | 309 | | } |
| | 310 | |
|
| 1 | 311 | | return false; |
| | 312 | | } |
| | 313 | |
|
| 12 | 314 | | public bool IsValid() { return original != null; } |
| | 315 | | } |
| | 316 | | }; |