/// <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(UnityEngine.UI.LoopScrollPrefabSource source, bool autoActive = true, int autoCreate = 0) { GameObject result = null; if (!poolDict.ContainsKey(source.prefabName) && autoCreate > 0) { InitPool(source, PoolInflationType.INCREMENT); } if (poolDict.ContainsKey(source.prefabName)) { Pool pool = poolDict[source.prefabName]; 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 " + source.prefabName); } #endif } #if UNITY_EDITOR else { Debug.LogError("[ResourceManager]:Invalid pool name specified: " + source.prefabName); } #endif return(result); }
public void InitPool(UnityEngine.UI.LoopScrollPrefabSource source, PoolInflationType type = PoolInflationType.DOUBLE) { if (poolDict.ContainsKey(source.prefabName)) { return; } else { //GameObject pb = Resources.Load<GameObject>(poolName); GameObject pb = GameObject.Instantiate(source.itemprefab); pb.gameObject.SetActive(false); pb.gameObject.SetActive(true); if (pb == null) { Debug.LogError("[ResourceManager] Invalide prefab name for pooling :" + source.prefabName); return; } poolDict[source.prefabName] = new Pool(source.prefabName, pb, gameObject, source.poolSize, type); } }