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