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