//o(1) public GameObject NextAvailableObject(Vector3 position, Quaternion rotation) { PooledObjectScript po = null; if (availableObjStack.Count > 0) { po = availableObjStack.Pop(); } else if (fixedSize == false) { //increment size var, this is for info purpose only poolSize++; /////////////////////////////////////////////////////////////////////////////////////////////////////////Debug.Log(string.Format("Growing pool {0}. New size: {1}",poolName,poolSize)); //create new object po = NewObjectInstance(); } else { //Debug.LogWarning("No object available & cannot grow pool: " + poolName); } GameObject result = null; if (po != null) { po.isPooled = false; result = po.gameObject; result.SetActive(true); result.transform.position = position; result.transform.rotation = rotation; } return(result); }
public void ReturnObjectToPool(GameObject go) { //custom for (int i = 0; i < go.transform.childCount; i++) { GameObject child = go.transform.GetChild(i).gameObject; if (child.GetComponent <PooledObjectScript>() != null) { ReturnObjectToPool(child.gameObject); i--; } } //custom PooledObjectScript po = go.GetComponent <PooledObjectScript>(); if (po == null) { //Debug.LogWarning("Specified object is not a pooled instance: " + go.name); } else { if (poolDictionary.ContainsKey(po.poolName)) { Pool pool = poolDictionary[po.poolName]; pool.ReturnObjectToPool(po); } else { //Debug.LogWarning("No pool available with name: " + po.poolName); } } }
//o(1) private void AddObjectToPool(PooledObjectScript po) { po.gameObject.SetActive(false); po.gameObject.transform.parent = null; //custom availableObjStack.Push(po); po.isPooled = true; }
private PooledObjectScript NewObjectInstance() { GameObject go = (GameObject)GameObject.Instantiate(poolObjectPrefab); PooledObjectScript po = go.GetComponent <PooledObjectScript>(); if (po == null) { po = go.AddComponent <PooledObjectScript>(); } //set name po.poolName = poolName; return(po); }
//o(1) public void ReturnObjectToPool(PooledObjectScript po) { if (poolName.Equals(po.poolName)) { if (po.isPooled) { //Debug.LogWarning(po.gameObject.name + " is already in pool. Why are you trying to return it again? Check usage."); } else { AddObjectToPool(po); } } else { //Debug.LogError(string.Format("Trying to add object to incorrect pool {0} {1}", po.poolName, poolName)); } }