| | 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; |
| 0 | 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. |
| 853 | 32 | | return poolableValues.Contains(poolable); |
| | 33 | | } |
| | 34 | |
|
| | 35 | | public PoolableObject GetPoolable(GameObject gameObject) |
| | 36 | | { |
| 63 | 37 | | if (gameObject == null) |
| 1 | 38 | | return null; |
| | 39 | |
|
| 62 | 40 | | if (poolables.ContainsKey(gameObject)) |
| | 41 | | { |
| 24 | 42 | | return poolables[gameObject]; |
| | 43 | | } |
| | 44 | |
|
| 38 | 45 | | return null; |
| | 46 | | } |
| | 47 | |
|
| | 48 | | GameObject container |
| | 49 | | { |
| | 50 | | get |
| | 51 | | { |
| 1796 | 52 | | EnsureContainer(); |
| 1796 | 53 | | return containerValue; |
| | 54 | | } |
| 0 | 55 | | set { containerValue = value; } |
| | 56 | | } |
| | 57 | |
|
| | 58 | | GameObject containerValue = null; |
| | 59 | |
|
| | 60 | | void EnsureContainer() |
| | 61 | | { |
| 1797 | 62 | | if (containerValue == null) |
| 635 | 63 | | containerValue = new GameObject("_PoolManager"); |
| 1797 | 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 | |
|
| 0 | 76 | | void OnRenderingStateChanged(bool renderingEnabled, bool prevState) { initializing = !renderingEnabled; } |
| | 77 | |
|
| | 78 | | public Pool AddPool(object id, GameObject original, IPooledObjectInstantiator instantiator = null, int maxPrewar |
| | 79 | | { |
| 1797 | 80 | | Pool existingPool = GetPool(id); |
| | 81 | |
|
| 1797 | 82 | | if (existingPool != null && !existingPool.IsValid()) |
| 11 | 83 | | RemovePool(id); |
| | 84 | |
|
| 1797 | 85 | | if (ContainsPool(id)) |
| | 86 | | { |
| 1 | 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 | |
|
| 1 | 94 | | Pool result = GetPool(id); |
| | 95 | |
|
| 1 | 96 | | result.AddToPool(original); |
| 1 | 97 | | result.persistent = isPersistent; |
| | 98 | |
|
| 1 | 99 | | return result; |
| | 100 | | } |
| | 101 | |
|
| 1796 | 102 | | if (!enablePrewarm) |
| 1796 | 103 | | maxPrewarmCount = 0; |
| | 104 | |
|
| 1796 | 105 | | Pool pool = new Pool(id.ToString(), maxPrewarmCount); |
| | 106 | |
|
| 1796 | 107 | | pool.id = id; |
| 1796 | 108 | | pool.original = original; |
| 1796 | 109 | | pool.persistent = isPersistent; |
| | 110 | |
|
| 1796 | 111 | | if (USE_POOL_CONTAINERS) |
| | 112 | | { |
| 1796 | 113 | | pool.container.transform.parent = container.transform; |
| 1796 | 114 | | pool.original.name = "Original"; |
| 1796 | 115 | | pool.original.transform.SetParent(pool.container.transform, true); |
| 1796 | 116 | | } |
| | 117 | | else |
| | 118 | | { |
| 0 | 119 | | pool.original.transform.SetParent(null, true); |
| | 120 | | } |
| | 121 | |
|
| 1796 | 122 | | pool.original.SetActive(false); |
| | 123 | |
|
| 1796 | 124 | | pool.instantiator = instantiator; |
| | 125 | |
|
| 1796 | 126 | | pools.Add(id, pool); |
| | 127 | |
|
| 1796 | 128 | | return pool; |
| | 129 | | } |
| | 130 | |
|
| | 131 | | public Pool GetPool(object id) |
| | 132 | | { |
| 4998 | 133 | | if (id == null) |
| | 134 | | { |
| 0 | 135 | | Debug.LogError("GetPool >>> id cannot be null!"); |
| 0 | 136 | | return null; |
| | 137 | | } |
| | 138 | |
|
| 4998 | 139 | | if (pools.ContainsKey(id)) |
| 2464 | 140 | | return pools[id]; |
| | 141 | |
|
| 2534 | 142 | | return null; |
| | 143 | | } |
| | 144 | |
|
| | 145 | | public void RemovePool(object id) |
| | 146 | | { |
| 1796 | 147 | | if (id == null) |
| | 148 | | { |
| 0 | 149 | | Debug.LogError("RemovePool >>> id cannot be null!"); |
| 0 | 150 | | return; |
| | 151 | | } |
| | 152 | |
|
| 1796 | 153 | | if (pools.ContainsKey(id)) |
| | 154 | | { |
| 1796 | 155 | | pools[id].Cleanup(); |
| 1796 | 156 | | pools.Remove(id); |
| | 157 | | } |
| 1796 | 158 | | } |
| | 159 | |
|
| | 160 | | public bool ContainsPool(object id) |
| | 161 | | { |
| 3188 | 162 | | if (id == null) |
| | 163 | | { |
| 0 | 164 | | Debug.LogError("ContainsPool >>> id cannot be null!"); |
| 0 | 165 | | return false; |
| | 166 | | } |
| | 167 | |
|
| 3188 | 168 | | return pools.ContainsKey(id); |
| | 169 | | } |
| | 170 | |
|
| | 171 | | public bool Release(GameObject gameObject) |
| | 172 | | { |
| 82 | 173 | | if (gameObject == null) |
| | 174 | | { |
| | 175 | | #if UNITY_EDITOR |
| 0 | 176 | | Debug.LogWarning("Release >>> gameObject cannot be null!"); |
| | 177 | | #endif |
| 0 | 178 | | return false; |
| | 179 | | } |
| | 180 | |
|
| | 181 | |
|
| 82 | 182 | | if (poolables.TryGetValue(gameObject, out PoolableObject poolableObject)) |
| | 183 | | { |
| 62 | 184 | | poolableObject.Release(); |
| 62 | 185 | | } |
| | 186 | | else |
| | 187 | | { |
| | 188 | | #if UNITY_EDITOR |
| 20 | 189 | | Debug.LogWarning("Release >>> poolable not found in object, destroying it instead!"); |
| | 190 | | #endif |
| 20 | 191 | | Object.Destroy(gameObject); |
| 20 | 192 | | return false; |
| | 193 | | } |
| | 194 | |
|
| 62 | 195 | | return true; |
| | 196 | | } |
| | 197 | |
|
| | 198 | | public PoolableObject Get(object id) |
| | 199 | | { |
| 785 | 200 | | if (id == null) |
| | 201 | | { |
| 0 | 202 | | Debug.LogError("Get >>> id cannot be null!"); |
| 0 | 203 | | return null; |
| | 204 | | } |
| | 205 | |
|
| | 206 | | Pool pool; |
| | 207 | |
|
| 785 | 208 | | if (pools.ContainsKey(id)) |
| | 209 | | { |
| 785 | 210 | | pool = pools[id]; |
| 785 | 211 | | } |
| | 212 | | else |
| | 213 | | { |
| 0 | 214 | | Debug.LogError($"Pool doesn't exist for id {id}!"); |
| 0 | 215 | | return null; |
| | 216 | | } |
| | 217 | |
|
| 785 | 218 | | OnGet?.Invoke(); |
| 785 | 219 | | return pool.Get(); |
| | 220 | | } |
| | 221 | |
|
| | 222 | | public IEnumerator CleanupAsync(bool unusedOnly = true, bool nonPersistentOnly = true, bool immediate = false) |
| | 223 | | { |
| 806 | 224 | | List<object> idsToRemove = new List<object>(pools.Count); |
| | 225 | |
|
| 806 | 226 | | using (var iterator = pools.GetEnumerator()) |
| | 227 | | { |
| 2579 | 228 | | while (iterator.MoveNext()) |
| | 229 | | { |
| 1773 | 230 | | Pool pool = iterator.Current.Value; |
| 1773 | 231 | | bool unusedPool = pool.usedObjectsCount == 0; // && pool.unusedObjectsCount > 0; |
| 1773 | 232 | | bool isNonPersistent = !pool.persistent; |
| | 233 | |
|
| 3495 | 234 | | if (!unusedOnly) unusedPool = true; |
| 3495 | 235 | | if (!nonPersistentOnly) isNonPersistent = true; |
| | 236 | |
|
| 1773 | 237 | | bool shouldRemove = unusedPool && isNonPersistent; |
| | 238 | |
|
| 1773 | 239 | | if ( shouldRemove ) |
| 1727 | 240 | | idsToRemove.Add(iterator.Current.Key); |
| | 241 | | } |
| 806 | 242 | | } |
| | 243 | |
|
| 5066 | 244 | | for (int i = 0; i < idsToRemove.Count; i++) |
| | 245 | | { |
| 1727 | 246 | | RemovePool(idsToRemove[i]); |
| | 247 | |
|
| 1727 | 248 | | if ( !immediate ) |
| 12 | 249 | | yield return null; |
| | 250 | | } |
| 806 | 251 | | } |
| | 252 | |
|
| | 253 | | public void Cleanup( bool unusedOnly = false, bool nonPersistentOnly = false ) |
| | 254 | | { |
| 802 | 255 | | if (pools == null) |
| 0 | 256 | | return; |
| | 257 | |
|
| 802 | 258 | | CleanupAsync( unusedOnly: unusedOnly, nonPersistentOnly: nonPersistentOnly, immediate: true ).MoveNext(); |
| 802 | 259 | | } |
| | 260 | |
|
| | 261 | | public void Dispose() |
| | 262 | | { |
| 778 | 263 | | Cleanup(); |
| 778 | 264 | | CommonScriptableObjects.rendererState.OnChange -= OnRenderingStateChanged; |
| 778 | 265 | | } |
| | 266 | |
|
| | 267 | | public void ReleaseAllFromPool(object id) |
| | 268 | | { |
| 1 | 269 | | if (pools.ContainsKey(id)) |
| | 270 | | { |
| 1 | 271 | | pools[id].ReleaseAll(); |
| | 272 | | } |
| 1 | 273 | | } |
| | 274 | | } |
| | 275 | | } |