using System.Collections.Generic; using UnityEngine; namespace Core.Utilities { /// /// Managers a dictionary of pools, getting and returning /// public class PoolManager : Singleton { /// /// List of poolables that will be used to initialize corresponding pools /// public List poolables; /// /// Dictionary of pools, key is the prefab /// protected Dictionary> m_Pools; /// /// Gets a poolable component from the corresponding pool /// /// /// public Poolable GetPoolable(Poolable poolablePrefab) { if (!m_Pools.ContainsKey(poolablePrefab)) { m_Pools.Add(poolablePrefab, new AutoComponentPrefabPool(poolablePrefab, Initialize, null, poolablePrefab.initialPoolCapacity)); } AutoComponentPrefabPool pool = m_Pools[poolablePrefab]; Poolable spawnedInstance = pool.Get(); spawnedInstance.pool = pool; return spawnedInstance; } /// /// Returns the poolable component to its component pool /// /// public void ReturnPoolable(Poolable poolable) { poolable.pool.Return(poolable); } /// /// Initializes the dicionary of pools /// protected void Start() { m_Pools = new Dictionary>(); foreach (var poolable in poolables) { if (poolable == null) { continue; } m_Pools.Add(poolable, new AutoComponentPrefabPool(poolable, Initialize, null, poolable.initialPoolCapacity)); } } void Initialize(Component poolable) { poolable.transform.SetParent(transform, false); } } }