//o(1) public GameObject NextAvailableObject(bool autoActive) { PoolObject po = null; if (availableObjStack.Count > 1) { po = availableObjStack.Pop(); } else { int increaseSize = 0; //increment size var, this is for info purpose only if (inflationType == PoolInflationType.INCREMENT) { increaseSize = 1; } else if (inflationType == PoolInflationType.DOUBLE) { increaseSize = availableObjStack.Count + Mathf.Max(objectsInUse, 0); } #if UNITY_EDITOR Debug.Log(string.Format("Growing pool {0}: {1} populated", poolName, increaseSize)); #endif if (increaseSize > 0) { populatePool(increaseSize); po = availableObjStack.Pop(); } } GameObject result = null; if (po != null) { objectsInUse++; po.isPooled = false; result = po.gameObject; if (autoActive) { result.SetActive(true); } } return(result); }
//o(1) public GameObject NextAvailableObject(bool autoActive) { PoolObject po = null; if (availableObjStack.Count > 1) { po = availableObjStack.Pop(); } else { int increaseSize = 0; //increment size var, this is for info purpose only if (inflationType == PoolInflationType.INCREMENT) { increaseSize = 1; } else if (inflationType == PoolInflationType.DOUBLE) { increaseSize = availableObjStack.Count + Mathf.Max(objectsInUse, 0); } if (increaseSize > 0) { populatePool(increaseSize); po = availableObjStack.Pop(); } } GameObject result = null; if (po != null) { objectsInUse++; po.isPooled = false; result = po.gameObject; if (autoActive) { result.SetActive(true); } } 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) { Debug.LogWarning("Specified object is not a pooled instance: " + go.name); } else { Pool pool = null; if (poolDict.TryGetValue(po.poolName, out pool)) { pool.ReturnObjectToPool(po); } else { Debug.LogWarning("No pool available with name: " + po.poolName); } } }
/// <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 { Pool pool = null; if (poolDict.TryGetValue(po.poolName, out pool)) { pool.ReturnObjectToPool(po); } else { Destroy(go); } } }