Example #1
0
 /// <summary>
 /// Release object back to the pool
 /// </summary>
 /// <param name="spawnedObject">Object to be released.</param>
 public void Release(PoolableGameObject spawnedObject)
 {
     Debug.Assert(spawnedObject.SourceGameObjectPool == this, "Attempting to despawn an object to a pool that is not its spawn source");
     spawnedObject.OnReleaseToPool();
     spawnedObject.gameObject.SetActive(false);
     instanceQueue.Add(spawnedObject);
 }
Example #2
0
        /// <summary>
        /// Get an instance from the pool.
        /// </summary>
        /// <param name="parent">Transform where instance will be parented or null if no parent.</param>
        /// <returns>Instance of pooled prefab.</returns>
        public PoolableGameObject Get(Transform parent = null)
        {
            PoolableGameObject spawnedObject = null;

            if (instanceQueue.Count > 0)
            {
                spawnedObject = instanceQueue[0];
                instanceQueue.RemoveAt(0);
            }
            else
            {
                spawnedObject = parent != null?Object.Instantiate(_prefab, parent) : Object.Instantiate(_prefab);

                spawnedObject.SourceGameObjectPool = this;
            }

            //If prefab is active, then also activate the spawned game object
            if (Prefab.gameObject.activeSelf)
            {
                spawnedObject.gameObject.SetActive(true);
            }

            spawnedObject.OnGetFromPool();

            return(spawnedObject);
        }
        /// <summary>
        /// Get Pool for a given prefab
        /// If no pool exists for the prefab one will be created
        /// </summary>
        /// <param name="prefab">Prefab that will be pooled</param>
        /// <returns>Pool of instances of the given prefab</returns>
        public GameObjectPool GetPool(PoolableGameObject prefab)
        {
            if (poolDictionary.TryGetValue(prefab, out var pool))
            {
                return(pool);
            }

            pool = new GameObjectPool(prefab);

            poolDictionary.Add(prefab, pool);

            return(pool);
        }
Example #4
0
 /// <summary>
 /// Completely remove an instance from the pool. Called by the PooledGameObject OnDestroy.
 /// </summary>
 /// <param name="instance">Object to be removed from the pool.</param>
 internal void RemoveInstance(PoolableGameObject instance)
 {
     instanceQueue.Remove(instance);
 }
Example #5
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="prefab">Prefab to be used by this pool.</param>
 public GameObjectPool(PoolableGameObject prefab)
 {
     _prefab = prefab;
 }