/// <summary> /// Returns an available object from the pool /// OR null in case the pool does not have any object available & can grow size is false. /// </summary> /// <param name="poolName"></param> /// <returns></returns> public GameObject GetObjectFromPool(string poolName, bool autoActive = true, int autoCreate = 0) { GameObject result = null; if (!poolDict.ContainsKey(poolName) && autoCreate > 0) { InitPool(poolName, autoCreate, PoolInflationType.INCREMENT); } if (poolDict.ContainsKey(poolName)) { UIObjectPool pool = poolDict[poolName]; result = pool.NextAvailableObject(autoActive); //scenario when no available object is found in pool #if UNITY_EDITOR if (result == null) { Debug.LogWarning("[ResourceManager]:No object available in " + poolName); } #endif } #if UNITY_EDITOR else { Debug.LogError("[ResourceManager]:Invalid pool name specified: " + poolName); } #endif return(result); }
/// <summary> /// Return obj to the pool /// </summary> /// <param name="go"></param> public void ReturnObjectToPool(GameObject go) { PoolObject po = go.GetComponent <PoolObject>(); if (po == null) { #if UNITY_EDITOR Debug.LogWarning("Specified object is not a pooled instance: " + go.name); #endif } else { UIObjectPool pool = null; if (poolDict.TryGetValue(po.poolName, out pool)) { pool.ReturnObjectToPool(po); } #if UNITY_EDITOR else { Debug.LogWarning("No pool available with name: " + po.poolName); } #endif } }
public void InitPool(string poolName, int size, PoolInflationType type = PoolInflationType.DOUBLE) { if (poolDict.ContainsKey(poolName)) { return; } else { GameObject pb = Resources.Load <GameObject>(poolName); if (pb == null) { Debug.LogError("[ResourceManager] Invalide prefab name for pooling :" + poolName); return; } poolDict[poolName] = new UIObjectPool(poolName, pb, gameObject, size, type); } }