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