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