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