using UnityEngine; using System.Collections; using System.Collections.Generic; namespace MoreMountains.Tools { /// /// A base class, meant to be extended depending on the use (simple, multiple object pooler), and used as an interface by the spawners. /// Still handles common stuff like singleton and initialization on start(). /// DO NOT add this class to a prefab, nothing would happen. Instead, add SimpleObjectPooler or MultipleObjectPooler. /// public abstract class MMObjectPooler : MonoBehaviour { /// singleton pattern public static MMObjectPooler Instance; /// if this is true, the pool will try not to create a new waiting pool if it finds one with the same name. public bool MutualizeWaitingPools = false; /// if this is true, all waiting and active objects will be regrouped under an empty game object. Otherwise they'll just be at top level in the hierarchy public bool NestWaitingPool = true; /// this object is just used to group the pooled objects protected GameObject _waitingPool = null; protected MMObjectPool _objectPool; /// /// On awake we fill our object pool /// protected virtual void Awake() { Instance = this; FillObjectPool(); } /// /// Creates the waiting pool or tries to reuse one if there's already one available /// protected virtual void CreateWaitingPool() { if (!MutualizeWaitingPools) { // we create a container that will hold all the instances we create _waitingPool = new GameObject(DetermineObjectPoolName()); _objectPool = _waitingPool.AddComponent(); _objectPool.PooledGameObjects = new List(); return; } else { GameObject waitingPool = GameObject.Find (DetermineObjectPoolName ()); if (waitingPool != null) { _waitingPool = waitingPool; _objectPool = _waitingPool.MMGetComponentNoAlloc(); } else { _waitingPool = new GameObject(DetermineObjectPoolName()); _objectPool = _waitingPool.AddComponent(); _objectPool.PooledGameObjects = new List(); } } } /// /// Determines the name of the object pool. /// /// The object pool name. protected virtual string DetermineObjectPoolName() { return ("[ObjectPooler] " + this.name); } /// /// Implement this method to fill the pool with objects /// public virtual void FillObjectPool() { return ; } /// /// Implement this method to return a gameobject /// /// The pooled game object. public virtual GameObject GetPooledGameObject() { return null; } /// /// Destroys the object pool /// public virtual void DestroyObjectPool() { if (_waitingPool != null) { Destroy(_waitingPool.gameObject); } } } }