| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | | using System.Linq; |
| | 6 | | using DCL.Interface; |
| | 7 | | using Object = UnityEngine.Object; |
| | 8 | |
|
| | 9 | | namespace DCL |
| | 10 | | { |
| | 11 | | public class PoolManager : Singleton<PoolManager> |
| | 12 | | { |
| | 13 | | #if UNITY_EDITOR |
| 1 | 14 | | public static bool USE_POOL_CONTAINERS = true; |
| | 15 | | #else |
| | 16 | | public static bool USE_POOL_CONTAINERS = false; |
| | 17 | | #endif |
| | 18 | |
|
| | 19 | | public const int DEFAULT_PREWARM_COUNT = 100; |
| 1 | 20 | | public static bool enablePrewarm = true; |
| 617 | 21 | | public bool initializing { get; private set; } |
| | 22 | |
|
| 1 | 23 | | public Dictionary<object, Pool> pools = new Dictionary<object, Pool>(); |
| 1 | 24 | | public Dictionary<GameObject, PoolableObject> poolables = new Dictionary<GameObject, PoolableObject>(); |
| 1 | 25 | | public HashSet<PoolableObject> poolableValues = new HashSet<PoolableObject>(); |
| | 26 | |
|
| | 27 | | public event System.Action OnGet; |
| | 28 | |
|
| | 29 | | public bool HasPoolable(PoolableObject poolable) |
| | 30 | | { |
| | 31 | | //NOTE(Brian): The only poolableValues use is this. Using ContainsValue in a Dictionary is slow as hell. |
| 784 | 32 | | return poolableValues.Contains(poolable); |
| | 33 | | } |
| | 34 | |
|
| | 35 | | public PoolableObject GetPoolable(GameObject gameObject) |
| | 36 | | { |
| 64 | 37 | | if (gameObject == null) |
| 0 | 38 | | return null; |
| | 39 | |
|
| 64 | 40 | | if (poolables.ContainsKey(gameObject)) |
| | 41 | | { |
| 12 | 42 | | return poolables[gameObject]; |
| | 43 | | } |
| | 44 | |
|
| 52 | 45 | | return null; |
| | 46 | | } |
| | 47 | |
|
| | 48 | | GameObject container |
| | 49 | | { |
| | 50 | | get |
| | 51 | | { |
| 5537 | 52 | | EnsureContainer(); |
| 5537 | 53 | | return containerValue; |
| | 54 | | } |
| 0 | 55 | | set { containerValue = value; } |
| | 56 | | } |
| | 57 | |
|
| | 58 | | GameObject containerValue = null; |
| | 59 | |
|
| | 60 | | void EnsureContainer() |
| | 61 | | { |
| 5538 | 62 | | if (containerValue == null) |
| 364 | 63 | | containerValue = new GameObject("_PoolManager"); |
| 5538 | 64 | | } |
| | 65 | |
|
| | 66 | | public System.Action<CrashPayload> CrashPayloadEvent; |
| | 67 | |
|
| 1 | 68 | | public PoolManager() |
| | 69 | | { |
| 1 | 70 | | EnsureContainer(); |
| | 71 | |
|
| 1 | 72 | | initializing = !CommonScriptableObjects.rendererState.Get(); |
| 1 | 73 | | CommonScriptableObjects.rendererState.OnChange += OnRenderingStateChanged; |
| 1 | 74 | | } |
| | 75 | |
|
| 2 | 76 | | void OnRenderingStateChanged(bool renderingEnabled, bool prevState) { initializing = !renderingEnabled; } |
| | 77 | |
|
| | 78 | | public Pool AddPool(object id, GameObject original, IPooledObjectInstantiator instantiator = null, int maxPrewar |
| | 79 | | { |
| 5540 | 80 | | Pool existingPool = GetPool(id); |
| | 81 | |
|
| 5540 | 82 | | if (existingPool != null && !existingPool.IsValid()) |
| 9 | 83 | | RemovePool(id); |
| | 84 | |
|
| 5540 | 85 | | if (ContainsPool(id)) |
| | 86 | | { |
| 3 | 87 | | if (Pool.FindPoolInGameObject(original, out Pool poolAlreadyExists)) |
| | 88 | | { |
| 0 | 89 | | Debug.LogWarning("WARNING: Object is already being contained in an existing pool!. Returning it."); |
| 0 | 90 | | poolAlreadyExists.persistent = isPersistent; |
| 0 | 91 | | return poolAlreadyExists; |
| | 92 | | } |
| | 93 | |
|
| 3 | 94 | | Pool result = GetPool(id); |
| | 95 | |
|
| 3 | 96 | | result.AddToPool(original); |
| 3 | 97 | | result.persistent = isPersistent; |
| | 98 | |
|
| 3 | 99 | | return result; |
| | 100 | | } |
| | 101 | |
|
| 5537 | 102 | | if (!enablePrewarm) |
| 5537 | 103 | | maxPrewarmCount = 0; |
| | 104 | |
|
| 5537 | 105 | | Pool pool = new Pool(id.ToString(), maxPrewarmCount) |
| | 106 | | { |
| | 107 | | id = id, |
| | 108 | | original = original, |
| | 109 | | persistent = isPersistent, |
| | 110 | | }; |
| | 111 | |
|
| 5537 | 112 | | if (USE_POOL_CONTAINERS) |
| | 113 | | { |
| 5537 | 114 | | pool.container.transform.parent = container.transform; |
| 5537 | 115 | | pool.original.name = "Original_" + id; |
| 5537 | 116 | | pool.original.transform.SetParent(pool.container.transform, true); |
| | 117 | | } |
| | 118 | | else |
| | 119 | | { |
| 0 | 120 | | pool.original.transform.SetParent(null, true); |
| | 121 | | } |
| | 122 | |
|
| 5537 | 123 | | pool.original.SetActive(false); |
| | 124 | |
|
| 5537 | 125 | | pool.instantiator = instantiator; |
| | 126 | |
|
| 5537 | 127 | | pools.Add(id, pool); |
| | 128 | |
|
| 5537 | 129 | | return pool; |
| | 130 | | } |
| | 131 | |
|
| | 132 | | public Pool GetPool(object id) |
| | 133 | | { |
| 13322 | 134 | | if (id == null) |
| | 135 | | { |
| 0 | 136 | | Debug.LogError("GetPool >>> id cannot be null!"); |
| 0 | 137 | | return null; |
| | 138 | | } |
| | 139 | |
|
| 13322 | 140 | | if (pools.ContainsKey(id)) |
| 3141 | 141 | | return pools[id]; |
| | 142 | |
|
| 10181 | 143 | | return null; |
| | 144 | | } |
| | 145 | |
|
| | 146 | | public void RemovePool(object id) |
| | 147 | | { |
| 5518 | 148 | | if (id == null) |
| | 149 | | { |
| 0 | 150 | | Debug.LogError("RemovePool >>> id cannot be null!"); |
| 0 | 151 | | return; |
| | 152 | | } |
| | 153 | |
|
| 5518 | 154 | | if (pools.ContainsKey(id)) |
| | 155 | | { |
| 5518 | 156 | | pools[id].Cleanup(); |
| 5518 | 157 | | pools.Remove(id); |
| | 158 | | } |
| 5518 | 159 | | } |
| | 160 | |
|
| | 161 | | public bool ContainsPool(object id) |
| | 162 | | { |
| 6418 | 163 | | if (id == null) |
| | 164 | | { |
| 0 | 165 | | Debug.LogError("ContainsPool >>> id cannot be null!"); |
| 0 | 166 | | return false; |
| | 167 | | } |
| | 168 | |
|
| 6418 | 169 | | return pools.ContainsKey(id); |
| | 170 | | } |
| | 171 | |
|
| | 172 | | public bool Release(GameObject gameObject) |
| | 173 | | { |
| 65 | 174 | | if (gameObject == null) |
| | 175 | | { |
| | 176 | | #if UNITY_EDITOR |
| 1 | 177 | | Debug.LogWarning("Release >>> gameObject cannot be null!"); |
| | 178 | | #endif |
| 1 | 179 | | return false; |
| | 180 | | } |
| | 181 | |
|
| | 182 | |
|
| 64 | 183 | | if (poolables.TryGetValue(gameObject, out PoolableObject poolableObject)) |
| | 184 | | { |
| 54 | 185 | | poolableObject.Release(); |
| | 186 | | } |
| | 187 | | else |
| | 188 | | { |
| | 189 | | #if UNITY_EDITOR |
| 10 | 190 | | Debug.LogWarning("Release >>> poolable not found in object, destroying it instead!"); |
| | 191 | | #endif |
| 10 | 192 | | Object.Destroy(gameObject); |
| 10 | 193 | | return false; |
| | 194 | | } |
| | 195 | |
|
| 54 | 196 | | return true; |
| | 197 | | } |
| | 198 | |
|
| | 199 | | public PoolableObject Get(object id) |
| | 200 | | { |
| 468 | 201 | | if (id == null) |
| | 202 | | { |
| 0 | 203 | | Debug.LogError("Get >>> id cannot be null!"); |
| 0 | 204 | | return null; |
| | 205 | | } |
| | 206 | |
|
| | 207 | | Pool pool; |
| | 208 | |
|
| 468 | 209 | | if (pools.ContainsKey(id)) |
| | 210 | | { |
| 468 | 211 | | pool = pools[id]; |
| | 212 | | } |
| | 213 | | else |
| | 214 | | { |
| 0 | 215 | | Debug.LogError($"Pool doesn't exist for id {id}!"); |
| 0 | 216 | | return null; |
| | 217 | | } |
| | 218 | |
|
| 468 | 219 | | OnGet?.Invoke(); |
| 468 | 220 | | return pool.Get(); |
| | 221 | | } |
| | 222 | |
|
| | 223 | | public IEnumerator CleanupAsync(bool unusedOnly = true, bool nonPersistentOnly = true, bool immediate = false) |
| | 224 | | { |
| 773 | 225 | | List<object> idsToRemove = new List<object>(pools.Count); |
| | 226 | |
|
| 773 | 227 | | using (var iterator = pools.GetEnumerator()) |
| | 228 | | { |
| 6512 | 229 | | while (iterator.MoveNext()) |
| | 230 | | { |
| 5739 | 231 | | Pool pool = iterator.Current.Value; |
| 5739 | 232 | | bool unusedPool = pool.usedObjectsCount == 0; // && pool.unusedObjectsCount > 0; |
| 5739 | 233 | | bool isNonPersistent = !pool.persistent; |
| | 234 | |
|
| 11128 | 235 | | if (!unusedOnly) unusedPool = true; |
| 11128 | 236 | | if (!nonPersistentOnly) isNonPersistent = true; |
| | 237 | |
|
| 5739 | 238 | | bool shouldRemove = unusedPool && isNonPersistent; |
| | 239 | |
|
| 5739 | 240 | | if ( shouldRemove ) |
| 5394 | 241 | | idsToRemove.Add(iterator.Current.Key); |
| | 242 | | } |
| 773 | 243 | | } |
| | 244 | |
|
| 12334 | 245 | | for (int i = 0; i < idsToRemove.Count; i++) |
| | 246 | | { |
| 5394 | 247 | | RemovePool(idsToRemove[i]); |
| | 248 | |
|
| 5394 | 249 | | if ( !immediate ) |
| 4 | 250 | | yield return null; |
| | 251 | | } |
| 773 | 252 | | } |
| | 253 | |
|
| | 254 | | public void Cleanup( bool unusedOnly = false, bool nonPersistentOnly = false ) |
| | 255 | | { |
| 769 | 256 | | if (pools == null) |
| 0 | 257 | | return; |
| | 258 | |
|
| 769 | 259 | | CleanupAsync( unusedOnly: unusedOnly, nonPersistentOnly: nonPersistentOnly, immediate: true ).MoveNext(); |
| 769 | 260 | | } |
| | 261 | |
|
| | 262 | | public void Dispose() |
| | 263 | | { |
| 496 | 264 | | Cleanup(); |
| 496 | 265 | | CommonScriptableObjects.rendererState.OnChange -= OnRenderingStateChanged; |
| 496 | 266 | | } |
| | 267 | |
|
| | 268 | | public void ReleaseAllFromPool(object id) |
| | 269 | | { |
| 0 | 270 | | if (pools.ContainsKey(id)) |
| | 271 | | { |
| 0 | 272 | | pools[id].ReleaseAll(); |
| | 273 | | } |
| 0 | 274 | | } |
| | 275 | | } |
| | 276 | | } |