| | 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 | |
|
| 1171 | 38 | | private readonly LinkedList<PoolableObject> unusedObjects = new LinkedList<PoolableObject>(); |
| 1171 | 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 | |
|
| 1171 | 52 | | public Pool(string name, int maxPrewarmCount) |
| | 53 | | { |
| | 54 | | #if UNITY_EDITOR |
| 1171 | 55 | | Application.quitting += OnIsQuitting; |
| | 56 | | #endif |
| 1171 | 57 | | if (PoolManager.USE_POOL_CONTAINERS) |
| 1171 | 58 | | container = new GameObject("Pool - " + name); |
| | 59 | |
|
| 1171 | 60 | | this.maxPrewarmCount = maxPrewarmCount; |
| 1171 | 61 | | } |
| | 62 | |
|
| | 63 | | public void ForcePrewarm() |
| | 64 | | { |
| 803 | 65 | | if (maxPrewarmCount <= objectsCount) |
| 803 | 66 | | return; |
| | 67 | |
|
| 0 | 68 | | int objectsToInstantiate = Mathf.Max(0, maxPrewarmCount - objectsCount); |
| 0 | 69 | | for (int i = 0; i < objectsToInstantiate; i++) |
| | 70 | | { |
| 0 | 71 | | Instantiate(); |
| | 72 | | } |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public PoolableObject Get() |
| | 76 | | { |
| | 77 | | // These extra instantiations during initialization are to populate pools that will be used a lot later |
| 827 | 78 | | if (PoolManager.i.initializing && !isInitialized) |
| | 79 | | { |
| 0 | 80 | | isInitialized = true; |
| 0 | 81 | | int count = usedObjectsCount; |
| | 82 | |
|
| 0 | 83 | | for (int i = unusedObjectsCount; i < Mathf.Min(count * PREWARM_ACTIVE_MULTIPLIER, maxPrewarmCount); i++) |
| | 84 | | { |
| 0 | 85 | | Instantiate(); |
| | 86 | | } |
| | 87 | |
|
| 0 | 88 | | Instantiate(); |
| 0 | 89 | | } |
| 827 | 90 | | else if (unusedObjects.Count == 0) |
| | 91 | | { |
| 785 | 92 | | Instantiate(); |
| | 93 | | } |
| | 94 | |
|
| 827 | 95 | | PoolableObject poolable = Extract(); |
| | 96 | |
|
| 827 | 97 | | EnablePoolableObject(poolable); |
| 827 | 98 | | poolable.OnPoolGet(); |
| 827 | 99 | | return poolable; |
| | 100 | | } |
| | 101 | |
|
| | 102 | | private PoolableObject Extract() |
| | 103 | | { |
| 827 | 104 | | PoolableObject po = null; |
| 827 | 105 | | po = unusedObjects.First.Value; |
| 827 | 106 | | unusedObjects.RemoveFirst(); |
| 827 | 107 | | po.node = usedObjects.AddFirst(po); |
| | 108 | |
|
| | 109 | | #if UNITY_EDITOR |
| 827 | 110 | | RefreshName(); |
| | 111 | | #endif |
| 827 | 112 | | return po; |
| | 113 | | } |
| | 114 | |
|
| | 115 | | public PoolableObject Instantiate() |
| | 116 | | { |
| 785 | 117 | | var gameObject = InstantiateAsOriginal(); |
| 785 | 118 | | return SetupPoolableObject(gameObject); |
| | 119 | | } |
| | 120 | |
|
| | 121 | | public GameObject InstantiateAsOriginal() |
| | 122 | | { |
| 807 | 123 | | Assert.IsTrue(original != null, $"Original should never be null here ({id})"); |
| | 124 | |
|
| 807 | 125 | | GameObject gameObject = null; |
| | 126 | |
|
| 807 | 127 | | if (instantiator != null) |
| 130 | 128 | | gameObject = instantiator.Instantiate(original); |
| | 129 | | else |
| 677 | 130 | | gameObject = GameObject.Instantiate(original); |
| | 131 | |
|
| 807 | 132 | | gameObject.SetActive(true); |
| | 133 | |
|
| 807 | 134 | | return gameObject; |
| | 135 | | } |
| | 136 | |
|
| | 137 | | private PoolableObject SetupPoolableObject(GameObject gameObject, bool active = false) |
| | 138 | | { |
| 786 | 139 | | if (PoolManager.i.poolables.ContainsKey(gameObject)) |
| 0 | 140 | | return PoolManager.i.GetPoolable(gameObject); |
| | 141 | |
|
| 786 | 142 | | PoolableObject poolable = new PoolableObject(this, gameObject); |
| 786 | 143 | | PoolManager.i.poolables.Add(gameObject, poolable); |
| 786 | 144 | | PoolManager.i.poolableValues.Add(poolable); |
| | 145 | |
|
| 786 | 146 | | if (!active) |
| | 147 | | { |
| 785 | 148 | | DisablePoolableObject(poolable); |
| 785 | 149 | | poolable.node = unusedObjects.AddFirst(poolable); |
| 785 | 150 | | } |
| | 151 | | else |
| | 152 | | { |
| 1 | 153 | | EnablePoolableObject(poolable); |
| 1 | 154 | | poolable.node = usedObjects.AddFirst(poolable); |
| | 155 | | } |
| | 156 | |
|
| | 157 | | #if UNITY_EDITOR |
| 786 | 158 | | RefreshName(); |
| | 159 | | #endif |
| 786 | 160 | | return poolable; |
| | 161 | | } |
| | 162 | |
|
| | 163 | | public void Release(PoolableObject poolable) |
| | 164 | | { |
| | 165 | | #if UNITY_EDITOR |
| 828 | 166 | | if (isQuitting) |
| 0 | 167 | | return; |
| | 168 | | #endif |
| | 169 | |
|
| 828 | 170 | | if (poolable == null || !PoolManager.i.HasPoolable(poolable)) |
| 0 | 171 | | return; |
| | 172 | |
|
| 828 | 173 | | DisablePoolableObject(poolable); |
| | 174 | |
|
| 828 | 175 | | poolable.node.List.Remove(poolable.node); |
| 828 | 176 | | poolable.node = unusedObjects.AddFirst(poolable); |
| | 177 | |
|
| | 178 | | #if UNITY_EDITOR |
| 828 | 179 | | RefreshName(); |
| | 180 | | #endif |
| 828 | 181 | | } |
| | 182 | |
|
| | 183 | | public void ReleaseAll() |
| | 184 | | { |
| 1478 | 185 | | while (usedObjects.Count > 0) |
| | 186 | | { |
| 159 | 187 | | usedObjects.First.Value.Release(); |
| | 188 | | } |
| 1319 | 189 | | } |
| | 190 | |
|
| | 191 | | /// <summary> |
| | 192 | | /// This will add a gameObject that is not on any pool to this pool. |
| | 193 | | /// </summary> |
| | 194 | | /// <param name="gameObject"></param> |
| | 195 | | public void AddToPool(GameObject gameObject, bool addActive = true) |
| | 196 | | { |
| 1 | 197 | | if (instantiator != null && !instantiator.IsValid(gameObject)) |
| | 198 | | { |
| 0 | 199 | | Debug.LogError($"ERROR: Trying to add invalid gameObject to pool! -- {gameObject.name}", gameObject); |
| 0 | 200 | | return; |
| | 201 | | } |
| | 202 | |
|
| 1 | 203 | | PoolableObject obj = PoolManager.i.GetPoolable(gameObject); |
| | 204 | |
|
| 1 | 205 | | if (obj != null) |
| | 206 | | { |
| 0 | 207 | | Debug.LogError($"ERROR: gameObject is already being tracked by a pool! -- {gameObject.name}", gameObject |
| 0 | 208 | | return; |
| | 209 | | } |
| | 210 | |
|
| 1 | 211 | | SetupPoolableObject(gameObject, addActive); |
| 1 | 212 | | } |
| | 213 | |
|
| | 214 | | public void RemoveFromPool(PoolableObject poolable) |
| | 215 | | { |
| 2 | 216 | | if (poolable.node != null) |
| | 217 | | { |
| 2 | 218 | | if (poolable.node.List != null) |
| 0 | 219 | | poolable.node.List.Remove(poolable); |
| | 220 | |
|
| 2 | 221 | | poolable.node = null; |
| | 222 | | } |
| | 223 | |
|
| 2 | 224 | | PoolManager.i.poolables.Remove(poolable.gameObject); |
| 2 | 225 | | PoolManager.i.poolableValues.Remove(poolable); |
| | 226 | | #if UNITY_EDITOR |
| 2 | 227 | | RefreshName(); |
| | 228 | | #endif |
| 2 | 229 | | } |
| | 230 | |
|
| | 231 | | public void Cleanup() |
| | 232 | | { |
| 1171 | 233 | | ReleaseAll(); |
| | 234 | |
|
| 1957 | 235 | | while (unusedObjects.Count > 0) |
| | 236 | | { |
| 786 | 237 | | PoolManager.i.poolables.Remove(unusedObjects.First.Value.gameObject); |
| 786 | 238 | | PoolManager.i.poolableValues.Remove(unusedObjects.First.Value); |
| 786 | 239 | | unusedObjects.RemoveFirst(); |
| | 240 | | } |
| | 241 | |
|
| 1171 | 242 | | while (usedObjects.Count > 0) |
| | 243 | | { |
| 0 | 244 | | PoolManager.i.poolables.Remove(usedObjects.First.Value.gameObject); |
| 0 | 245 | | PoolManager.i.poolableValues.Remove(usedObjects.First.Value); |
| 0 | 246 | | usedObjects.RemoveFirst(); |
| | 247 | | } |
| | 248 | |
|
| 1171 | 249 | | unusedObjects.Clear(); |
| 1171 | 250 | | usedObjects.Clear(); |
| | 251 | |
|
| 1171 | 252 | | Object.Destroy(this.original); |
| | 253 | |
|
| 1171 | 254 | | if (PoolManager.USE_POOL_CONTAINERS) |
| 1171 | 255 | | Object.Destroy(this.container); |
| | 256 | |
|
| 1171 | 257 | | OnCleanup?.Invoke(this); |
| 121 | 258 | | } |
| | 259 | |
|
| | 260 | | public void EnablePoolableObject(PoolableObject poolable) |
| | 261 | | { |
| 828 | 262 | | GameObject go = poolable.gameObject; |
| | 263 | |
|
| 828 | 264 | | if (go == null) |
| 0 | 265 | | return; |
| | 266 | |
|
| 828 | 267 | | if (!go.activeSelf) |
| 827 | 268 | | go.SetActive(true); |
| | 269 | |
|
| 828 | 270 | | go.transform.ResetLocalTRS(); |
| | 271 | |
|
| 828 | 272 | | lastGetTime = Time.unscaledTime; |
| 828 | 273 | | } |
| | 274 | |
|
| | 275 | | public void DisablePoolableObject(PoolableObject poolable) |
| | 276 | | { |
| | 277 | | #if UNITY_EDITOR |
| 1613 | 278 | | if (isQuitting) |
| 0 | 279 | | return; |
| | 280 | | #endif |
| 1613 | 281 | | GameObject go = poolable.gameObject; |
| | 282 | |
|
| 1613 | 283 | | if (go == null) |
| 36 | 284 | | return; |
| | 285 | |
|
| 1577 | 286 | | if (go.activeSelf) |
| 1133 | 287 | | go.SetActive(false); |
| | 288 | |
|
| 1577 | 289 | | if (PoolManager.USE_POOL_CONTAINERS) |
| | 290 | | { |
| 1577 | 291 | | if (container != null) |
| | 292 | | { |
| 1577 | 293 | | go.transform.SetParent(container.transform); |
| | 294 | | } |
| 1577 | 295 | | } |
| | 296 | | else |
| | 297 | | { |
| 0 | 298 | | go.transform.SetParent(null); |
| | 299 | | } |
| 0 | 300 | | } |
| | 301 | |
|
| | 302 | | #if UNITY_EDITOR |
| | 303 | | private void RefreshName() |
| | 304 | | { |
| 2443 | 305 | | if (this.container != null) |
| 2443 | 306 | | this.container.name = $"in: {unusedObjectsCount} out: {usedObjectsCount} id: {id} persistent: {persisten |
| 2443 | 307 | | } |
| | 308 | | #endif |
| | 309 | | public static bool FindPoolInGameObject(GameObject gameObject, out Pool pool) |
| | 310 | | { |
| 1 | 311 | | pool = null; |
| | 312 | |
|
| 1 | 313 | | if (PoolManager.i.poolables.TryGetValue(gameObject, out PoolableObject poolable)) |
| | 314 | | { |
| 0 | 315 | | pool = poolable.pool; |
| 0 | 316 | | return true; |
| | 317 | | } |
| | 318 | |
|
| 1 | 319 | | return false; |
| | 320 | | } |
| | 321 | |
|
| 12 | 322 | | public bool IsValid() { return original != null; } |
| | 323 | |
|
| | 324 | | #if UNITY_EDITOR |
| | 325 | | // In production it will always be false |
| | 326 | | private bool isQuitting = false; |
| | 327 | |
|
| | 328 | | // We need to check if application is quitting in editor |
| | 329 | | // to prevent the pool from releasing objects that are |
| | 330 | | // being destroyed |
| | 331 | | void OnIsQuitting() |
| | 332 | | { |
| 0 | 333 | | Application.quitting -= OnIsQuitting; |
| 0 | 334 | | isQuitting = true; |
| 0 | 335 | | } |
| | 336 | | #endif |
| | 337 | | } |
| | 338 | | }; |