| | 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 | |
|
| 5204 | 41 | | private readonly LinkedList<PoolableObject> unusedObjects = new LinkedList<PoolableObject>(); |
| 5204 | 42 | | private readonly LinkedList<PoolableObject> usedObjects = new LinkedList<PoolableObject>(); |
| | 43 | |
|
| | 44 | | private readonly int maxPrewarmCount; |
| | 45 | |
|
| | 46 | | private bool isInitialized; |
| | 47 | |
|
| 1571 | 48 | | public float lastGetTime { get; private set; } |
| | 49 | |
|
| 4834 | 50 | | public int objectsCount => unusedObjectsCount + usedObjectsCount; |
| | 51 | |
|
| 9895 | 52 | | public int unusedObjectsCount => unusedObjects.Count; |
| | 53 | |
|
| 15254 | 54 | | public int usedObjectsCount => usedObjects.Count; |
| | 55 | |
|
| 5204 | 56 | | public Pool(string name, int maxPrewarmCount) |
| | 57 | | { |
| 5204 | 58 | | if (PoolManager.USE_POOL_CONTAINERS) |
| | 59 | | { |
| 5204 | 60 | | container = new GameObject("Pool - " + name); |
| 5204 | 61 | | container.transform.position = EnvironmentSettings.MORDOR; |
| | 62 | | } |
| | 63 | |
|
| 5204 | 64 | | this.maxPrewarmCount = maxPrewarmCount; |
| 5204 | 65 | | } |
| | 66 | |
|
| | 67 | | public void ForcePrewarm() |
| | 68 | | { |
| 4833 | 69 | | if (maxPrewarmCount <= objectsCount) |
| 4833 | 70 | | return; |
| | 71 | |
|
| 0 | 72 | | int objectsToInstantiate = Mathf.Max(0, maxPrewarmCount - objectsCount); |
| | 73 | |
|
| 0 | 74 | | for (int i = 0; i < objectsToInstantiate; i++) { Instantiate(); } |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// This will return an instance of the poolable object |
| | 79 | | /// </summary> |
| | 80 | | /// <returns></returns> |
| | 81 | | public PoolableObject Get() |
| | 82 | | { |
| | 83 | | // These extra instantiations during initialization are to populate pools that will be used a lot later |
| 1568 | 84 | | if (PoolManager.i.initializing && !isInitialized) |
| | 85 | | { |
| 461 | 86 | | isInitialized = true; |
| | 87 | |
|
| 922 | 88 | | for (int i = unusedObjectsCount; i < Mathf.Min(usedObjectsCount * PREWARM_ACTIVE_MULTIPLIER, maxPrewarmC |
| 0 | 89 | | Instantiate(); |
| | 90 | |
|
| 461 | 91 | | Instantiate(); |
| | 92 | | } |
| 2117 | 93 | | else if (unusedObjects.Count == 0) { Instantiate(); } |
| | 94 | |
|
| 1568 | 95 | | PoolableObject poolable = Extract(); |
| | 96 | |
|
| 1568 | 97 | | EnablePoolableObject(poolable); |
| 1568 | 98 | | poolable.OnPoolGet(); |
| | 99 | |
|
| 1568 | 100 | | return poolable; |
| | 101 | | } |
| | 102 | |
|
| | 103 | | public T Get<T>() where T: MonoBehaviour => |
| 62 | 104 | | this.Get().gameObject.GetComponent<T>(); |
| | 105 | |
|
| | 106 | | private PoolableObject Extract() |
| | 107 | | { |
| 1568 | 108 | | PoolableObject po = null; |
| 1568 | 109 | | po = unusedObjects.First.Value; |
| 1568 | 110 | | unusedObjects.RemoveFirst(); |
| 1568 | 111 | | po.node = usedObjects.AddFirst(po); |
| | 112 | |
|
| | 113 | | #if UNITY_EDITOR |
| 1568 | 114 | | RefreshName(); |
| | 115 | | #endif |
| 1568 | 116 | | return po; |
| | 117 | | } |
| | 118 | |
|
| | 119 | | public async UniTask PrewarmAsync(int prewarmCount, CancellationToken cancellationToken) |
| | 120 | | { |
| 4 | 121 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 122 | |
|
| 4 | 123 | | if (unusedObjects.Count >= prewarmCount) |
| 4 | 124 | | return; |
| | 125 | |
|
| 0 | 126 | | for (int i = 0; i < prewarmCount; i++) |
| | 127 | | { |
| 0 | 128 | | Instantiate(); |
| 0 | 129 | | await UniTask.NextFrame(cancellationToken); |
| | 130 | | } |
| 4 | 131 | | } |
| | 132 | |
|
| | 133 | | public PoolableObject Instantiate() |
| | 134 | | { |
| 1471 | 135 | | var gameObject = InstantiateAsOriginal(); |
| | 136 | |
|
| 1471 | 137 | | return SetupPoolableObject(gameObject); |
| | 138 | | } |
| | 139 | |
|
| | 140 | | public GameObject InstantiateAsOriginal() |
| | 141 | | { |
| 1493 | 142 | | Assert.IsTrue(original != null, $"Original should never be null here ({id})"); |
| | 143 | |
|
| 1493 | 144 | | GameObject gameObject = null; |
| | 145 | |
|
| 1493 | 146 | | if (instantiator != null) |
| 121 | 147 | | gameObject = instantiator.Instantiate(original); |
| | 148 | | else |
| 1372 | 149 | | gameObject = GameObject.Instantiate(original); |
| | 150 | |
|
| 1493 | 151 | | gameObject.SetActive(true); |
| | 152 | |
|
| 1493 | 153 | | return gameObject; |
| | 154 | | } |
| | 155 | |
|
| | 156 | | private PoolableObject SetupPoolableObject(GameObject gameObject, bool active = false) |
| | 157 | | { |
| 1474 | 158 | | if (PoolManager.i.poolables.ContainsKey(gameObject)) |
| 0 | 159 | | return PoolManager.i.GetPoolable(gameObject); |
| | 160 | |
|
| 1474 | 161 | | PoolableObject poolable = new PoolableObject(this, gameObject); |
| 1474 | 162 | | PoolManager.i.poolables.Add(gameObject, poolable); |
| 1474 | 163 | | PoolManager.i.poolableValues.Add(poolable); |
| | 164 | |
|
| 1474 | 165 | | if (!active) |
| | 166 | | { |
| 1471 | 167 | | DisablePoolableObject(poolable); |
| 1471 | 168 | | poolable.node = unusedObjects.AddFirst(poolable); |
| | 169 | | } |
| | 170 | | else |
| | 171 | | { |
| 3 | 172 | | EnablePoolableObject(poolable); |
| 3 | 173 | | poolable.node = usedObjects.AddFirst(poolable); |
| | 174 | | } |
| | 175 | |
|
| | 176 | | #if UNITY_EDITOR |
| 1474 | 177 | | RefreshName(); |
| | 178 | | #endif |
| 1474 | 179 | | return poolable; |
| | 180 | | } |
| | 181 | |
|
| | 182 | | public void Release(PoolableObject poolable) |
| | 183 | | { |
| 1735 | 184 | | if (poolable == null || !PoolManager.i.HasPoolable(poolable)) |
| 184 | 185 | | return; |
| | 186 | |
|
| 1551 | 187 | | DisablePoolableObject(poolable); |
| | 188 | |
|
| 1551 | 189 | | poolable.node.List.Remove(poolable.node); |
| 1551 | 190 | | poolable.node = unusedObjects.AddFirst(poolable); |
| | 191 | |
|
| | 192 | | #if UNITY_EDITOR |
| 1551 | 193 | | RefreshName(); |
| | 194 | | #endif |
| 1551 | 195 | | } |
| | 196 | |
|
| | 197 | | public void ReleaseAll() |
| | 198 | | { |
| 8175 | 199 | | while (usedObjects.Count > 0) { usedObjects.First.Value.Release(); } |
| 5463 | 200 | | } |
| | 201 | |
|
| | 202 | | /// <summary> |
| | 203 | | /// This will add a gameObject that is not on any pool to this pool. |
| | 204 | | /// </summary> |
| | 205 | | /// <param name="gameObject"></param> |
| | 206 | | public void AddToPool(GameObject gameObject, bool addActive = true) |
| | 207 | | { |
| 3 | 208 | | if (instantiator != null && !instantiator.IsValid(gameObject)) |
| | 209 | | { |
| 0 | 210 | | Debug.LogError($"ERROR: Trying to add invalid gameObject to pool! -- {gameObject.name}", gameObject); |
| | 211 | |
|
| 0 | 212 | | return; |
| | 213 | | } |
| | 214 | |
|
| 3 | 215 | | PoolableObject obj = PoolManager.i.GetPoolable(gameObject); |
| | 216 | |
|
| 3 | 217 | | if (obj != null) |
| | 218 | | { |
| 0 | 219 | | Debug.LogError($"ERROR: gameObject is already being tracked by a pool! -- {gameObject.name}", gameObject |
| | 220 | |
|
| 0 | 221 | | return; |
| | 222 | | } |
| | 223 | |
|
| 3 | 224 | | SetupPoolableObject(gameObject, addActive); |
| 3 | 225 | | } |
| | 226 | |
|
| | 227 | | public void RemoveFromPool(PoolableObject poolable) |
| | 228 | | { |
| 5 | 229 | | if (poolable.node != null) |
| | 230 | | { |
| 5 | 231 | | if (poolable.node.List != null) |
| 0 | 232 | | poolable.node.List.Remove(poolable); |
| | 233 | |
|
| 5 | 234 | | poolable.node = null; |
| | 235 | | } |
| | 236 | |
|
| 5 | 237 | | PoolManager.i.poolables.Remove(poolable.gameObject); |
| 5 | 238 | | PoolManager.i.poolableValues.Remove(poolable); |
| | 239 | | #if UNITY_EDITOR |
| 5 | 240 | | RefreshName(); |
| | 241 | | #endif |
| 5 | 242 | | } |
| | 243 | |
|
| | 244 | | public void Cleanup() |
| | 245 | | { |
| 5186 | 246 | | ReleaseAll(); |
| | 247 | |
|
| 6631 | 248 | | while (unusedObjects.Count > 0) |
| | 249 | | { |
| 1445 | 250 | | PoolManager.i.poolables.Remove(unusedObjects.First.Value.gameObject); |
| 1445 | 251 | | PoolManager.i.poolableValues.Remove(unusedObjects.First.Value); |
| 1445 | 252 | | unusedObjects.RemoveFirst(); |
| | 253 | | } |
| | 254 | |
|
| 5186 | 255 | | while (usedObjects.Count > 0) |
| | 256 | | { |
| 0 | 257 | | PoolManager.i.poolables.Remove(usedObjects.First.Value.gameObject); |
| 0 | 258 | | PoolManager.i.poolableValues.Remove(usedObjects.First.Value); |
| 0 | 259 | | usedObjects.RemoveFirst(); |
| | 260 | | } |
| | 261 | |
|
| 5186 | 262 | | unusedObjects.Clear(); |
| 5186 | 263 | | usedObjects.Clear(); |
| | 264 | |
|
| 5186 | 265 | | Utils.SafeDestroy(this.original); |
| | 266 | |
|
| 5186 | 267 | | Utils.SafeDestroy(this.container);; |
| | 268 | |
|
| 5186 | 269 | | OnCleanup?.Invoke(this); |
| 114 | 270 | | } |
| | 271 | |
|
| | 272 | | public void EnablePoolableObject(PoolableObject poolable) |
| | 273 | | { |
| 1571 | 274 | | GameObject go = poolable.gameObject; |
| | 275 | |
|
| 1571 | 276 | | if (go == null) |
| 0 | 277 | | return; |
| | 278 | |
|
| 1571 | 279 | | if (!go.activeSelf) |
| 1568 | 280 | | go.SetActive(true); |
| | 281 | |
|
| 1571 | 282 | | go.transform.ResetLocalTRS(); |
| | 283 | |
|
| 1571 | 284 | | lastGetTime = Time.unscaledTime; |
| 1571 | 285 | | } |
| | 286 | |
|
| | 287 | | public void DisablePoolableObject(PoolableObject poolable) |
| | 288 | | { |
| | 289 | | #if UNITY_STANDALONE || UNITY_EDITOR |
| 3022 | 290 | | if (DataStore.i.common.isApplicationQuitting.Get()) |
| 0 | 291 | | return; |
| | 292 | | #endif |
| 3022 | 293 | | GameObject go = poolable.gameObject; |
| | 294 | |
|
| 3022 | 295 | | if (go == null) |
| 29 | 296 | | return; |
| | 297 | |
|
| 2993 | 298 | | if (go.activeSelf) |
| 2987 | 299 | | go.SetActive(false); |
| | 300 | |
|
| 2993 | 301 | | if (PoolManager.USE_POOL_CONTAINERS) |
| | 302 | | { |
| 5986 | 303 | | if (container != null) { go.transform.SetParent(container.transform); } |
| | 304 | | } |
| 0 | 305 | | else { go.transform.SetParent(null); } |
| 0 | 306 | | } |
| | 307 | |
|
| | 308 | | #if UNITY_EDITOR |
| | 309 | | private void RefreshName() |
| | 310 | | { |
| 4598 | 311 | | if (this.container != null) |
| 4598 | 312 | | this.container.name = $"in: {unusedObjectsCount} out: {usedObjectsCount} id: {id} persistent: {persisten |
| 4598 | 313 | | } |
| | 314 | | #endif |
| | 315 | | public static bool FindPoolInGameObject(GameObject gameObject, out Pool pool) |
| | 316 | | { |
| 3 | 317 | | pool = null; |
| | 318 | |
|
| 3 | 319 | | if (PoolManager.i.poolables.TryGetValue(gameObject, out PoolableObject poolable)) |
| | 320 | | { |
| 0 | 321 | | pool = poolable.pool; |
| | 322 | |
|
| 0 | 323 | | return true; |
| | 324 | | } |
| | 325 | |
|
| 3 | 326 | | return false; |
| | 327 | | } |
| | 328 | |
|
| | 329 | | public bool IsValid() |
| | 330 | | { |
| 15 | 331 | | return original != null; |
| | 332 | | } |
| | 333 | | } |
| | 334 | | }; |