///////////////////////////////////////////// public ObjectPool(int numElements, GameObject go) { if (numElements < 1) { Debug.LogError("Failed to init the Object Pool"); return; } #if DEBUG mMinimumAvailableElements = numElements; #endif mActiveObjects = new List <PoolElement>(); mAvailableObjects = new Queue <PoolElement>(); GameObject root = new GameObject("Pool_" + go.name); for (int i = 0; i < numElements; i++) { GameObject newOb = Object.Instantiate(go); newOb.transform.parent = root.transform; PoolElement component = newOb.GetComponent <PoolElement>(); if (component == null) { Debug.LogError("ObjectPool: the GameObject " + go + " doesn't have a PoolElement component."); } component.SetProprietaryPool(this); component.Deactivate(); mAvailableObjects.Enqueue(component); } }
//Set an object to disabled ///////////////////////////////////////////// public void Destroy(PoolElement element) { if (!mActiveObjects.Contains(element)) { Debug.LogWarning("ObjectPool: object is not active " + element + "."); return; } Debug.Log("ObjectPool: deactivating object " + element.gameObject); element.Deactivate(); mActiveObjects.Remove(element); mAvailableObjects.Enqueue(element); }