//Returns an object to pool public void ReturnToPool(IPoolableObject objParam) { IPoolableObject objResult = null; //look if it's in the live list, end eventually remove it for (int iLoop = 0; iLoop < m_Size; iLoop++) { if (objParam == m_aObjLiveList[iLoop]) { objResult = m_aObjLiveList[iLoop]; m_aObjLiveList[iLoop] = null; break; } } Assert.IsNotNull(objResult, "Object not belonging to pool returned!"); //deactivate objResult.gameObject.SetActive(false); //put back in deadlist for (int iLoop = 0; iLoop < m_Size; iLoop++) { if (null == m_aObjDeadList[iLoop]) { m_aObjDeadList[iLoop] = objResult; break; } } m_FreeCount++; objResult.transform.parent = this.transform; }
//Creates an object of the ObjPrefab type. Used to populate the arrays private IPoolableObject CreateObject() { GameObject objResult = null; objResult = GameObject.Instantiate(m_objPrefab.gameObject, this.transform, false); objResult.SetActive(false); IPoolableObject objComponent = objResult.GetComponent <IPoolableObject>(); objComponent.SetOwner(this); return(objComponent); }
//Gets an object from the pool, taking it from the dead list public GameObject GetObject() { IPoolableObject objResult = null; if (0 == m_FreeCount) { if (m_CanReturnNull) { return(null); } else { Assert.IsTrue(false, "Requesting object from a depleted pool. Probably replanning usage is required, and resizing accordingly"); } } for (int iLoop = 0; iLoop < m_Size; iLoop++) { //first non null object , saves it if (null != m_aObjDeadList[iLoop]) { objResult = m_aObjDeadList[iLoop]; m_aObjDeadList[iLoop] = null; for (int jLoop = 0; jLoop < m_Size; jLoop++) { //first null object , saves there if (null == m_aObjLiveList[jLoop]) { m_aObjLiveList[jLoop] = objResult; break; } } break; } } Assert.IsTrue(null != objResult, "Didn't find any available object from pool"); if (objResult) { objResult.gameObject.SetActive(true); objResult.transform.parent = null; objResult.IsKilled = false; } m_FreeCount--; return(objResult.gameObject); }