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