using System.Collections; using System.Collections.Generic; using UnityEngine; namespace AssetBundleMaster.ObjectPool { using AssetBundleMaster.GameUtilities; using AssetBundleMaster.Extention; /// /// UnityObjectPool Manager /// public sealed class UnityObjectPoolManager : SingletonComponent { public const string DefaultPoolName = "[DefaultPool]"; // cached pools, with diferent pool name private Dictionary _pools = new Dictionary(); #region Main Funcs /// /// Get / Create Target Pool /// /// /// /// /// public T GetObjectPool(string poolName = null, bool createIfNoExists = true) where T : UnityObjectPool { if(string.IsNullOrEmpty(poolName)) { poolName = DefaultPoolName; } var objectPool = _pools.TryGetValue(poolName) as T; // the type may not the same if(objectPool == false && createIfNoExists) { var poolObject = new GameObject(poolName); poolObject.transform.SetParent(this.transform); objectPool = poolObject.AddComponent(); objectPool.PoolName = poolName; _pools[poolName] = objectPool; } return objectPool; } /// /// Delete Target Pool /// /// public void DestroyPool(string poolName) { var pool = GetObjectPool(poolName, false); if(pool) { pool.DoDestroy(); _pools.Remove(poolName); } } #endregion } }