This class is a required component for any prefab that is to be pooled
Inheritance: UnityEngine.MonoBehaviour
Beispiel #1
0
        public void AddPool(PoolableGameObject pool, System.Action destroyCallback)
        {
            if (_gamePools == null)
            {
                _gamePools = new List<PoolInfo>();
            }

            _gamePools.Add(new PoolInfo(pool, destroyCallback));
        }
        /// <summary>
        /// Increments the prefab's or GameObject's object pool.
        /// </summary>
        /// <param name="objToInc">The prefab or GameObject to increment.</param>
        public static void IncrementPoolWithObj(GameObject objToInc)
        {
            PoolableGameObject poolable = objToInc.GetComponent <PoolableGameObject>();

            if (poolable == null)
            {
                Debug.LogErrorFormat(REQUIRES_COMP, objToInc.name, "IncrementPool");
                return;
            }

            poolable.IncrementPool();
        }
        /// <summary>
        /// Adds to the prefab's or GameObject's object pool.
        /// </summary>
        /// <param name="objToAddTo">The prefab or GameObject to add to.</param>
        /// <param name="amount">Amount to add.</param>
        public static void AddToPoolWithObj(GameObject objToAddTo, int amount)
        {
            PoolableGameObject poolable = objToAddTo.GetComponent <PoolableGameObject>();

            if (poolable == null)
            {
                Debug.LogErrorFormat(REQUIRES_COMP, objToAddTo.name, "AddToPool");
                return;
            }

            poolable.AddToPool(amount);
        }
        /// <summary>
        /// Releases an object back to its object pool.
        /// </summary>
        /// <param name="objToRelease">Object to release</param>
        public static void ReleaseObj(GameObject objToRelease)
        {
            PoolableGameObject poolable = objToRelease.GetComponent <PoolableGameObject>();

            if (poolable == null)
            {
                Debug.LogErrorFormat(REQUIRES_COMP, objToRelease.name, "Release");
                return;
            }

            poolable.Release();
        }
        /// <summary>
        /// Receives a GameObject from objToCreateFrom's object
        /// pool.
        /// </summary>
        /// <param name="objToCreateFrom">The prefab or GameObject that we
        /// want a duplicated object of.</param>
        /// <returns>A GameObject from the object pool.</returns>
        public static GameObject GetObj(GameObject objToCreateFrom)
        {
            PoolableGameObject poolable = objToCreateFrom.GetComponent <PoolableGameObject>();

            if (poolable == null)
            {
                Debug.LogErrorFormat(REQUIRES_COMP, objToCreateFrom.name, "Get");
                return(null);
            }

            return(poolable.Get().gameObject);
        }
        /// <summary>
        /// Releases all live objects and clears the pool.
        /// </summary>
        /// <param name="objToClear"></param>
        public static void ReleaseAndClearPoolWithObj(GameObject objToClear)
        {
            PoolableGameObject poolable = objToClear.GetComponent <PoolableGameObject>();

            if (poolable == null)
            {
                Debug.LogErrorFormat(REQUIRES_COMP, objToClear.name, "ReleaseAndClearPool");
                return;
            }

            poolable.ReleaseObjectsAndClearPool();
        }
        /// <summary>
        /// Populates the prefab's pool to the desired amount specified in the inspector.
        /// </summary>
        /// <param name="objToPopulate">The prefab to populate.</param>
        public static void PopulateToDesiredWithObj(GameObject objToPopulate)
        {
            PoolableGameObject poolable = objToPopulate.GetComponent <PoolableGameObject>();

            if (poolable == null)
            {
                Debug.LogErrorFormat(REQUIRES_COMP, objToPopulate.name, "PopulateToDesired");
                return;
            }

            poolable.PopulateToDesired();
        }
        /// <summary>
        /// Adds 'numberToAdd' to the pool.
        /// </summary>
        /// <param name="numberToAdd">Amount to add to the pool.</param>
        public void AddToPool(int numberToAdd)
        {
            if (numberToAdd <= 0)
            {
                return;
            }

            if (GameObjectPool.verboseLogging)
            {
                Debug.LogFormat("Adding {0} {1} to  pool.", numberToAdd, gameObject.name);
            }

            Initialize();

            bool activeState = gameObject.activeSelf;

            gameObject.SetActive(false);

            for (int i = 0; i < numberToAdd; i++)
            {
                if (useCap && (_pooledObjs.Count + _numOfActiveObjs >= capAmount))
                {
                    // We're done here
                    break;
                }

                PoolableGameObject newObj = Instantiate(gameObject).GetComponent <PoolableGameObject>();

                if (persistAcrossScenes)
                {
                    DontDestroyOnLoad(newObj.gameObject);
                }

                newObj.transform.SetParent(container);
                newObj._originalObject = this;

                _pooledObjs.Push(newObj);

                if (!newObj._createMsgSent)
                {
                    SendCreationMessage(newObj);
                }

                if (useCap || persistAcrossScenes)
                {
                    _pooledNodes.Push(new LinkedListNode <PoolableGameObject>(null));
                }
            }

            gameObject.SetActive(activeState);
        }
        private void SendCreationMessage(PoolableGameObject newObj)
        {
            newObj._createMsgSent = true;

            if (onObjectCreation != null)
            {
                onObjectCreation(newObj.gameObject);
            }

            _poolables = GetComponentsInChildren <IGameObjectPoolable>(true);

            for (int i = 0; i < _poolables.Length; i++)
            {
                _poolables[i].OnObjectCreated();
            }
        }
Beispiel #10
0
        /// <summary>
        /// Populates the GameObject's object pool to the specified amount within an enumerator. Intended to work with
        /// https://github.com/GalvanicGames/unity-game-loader
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        public static IEnumerator PopulatePoolCo(this GameObject obj, int amount)
        {
            PoolableGameObject poolable = obj.GetComponent <PoolableGameObject>();

            if (poolable == null)
            {
                Debug.LogErrorFormat(REQUIRES_COMP, obj.name, "PopulatePool");
            }
            else
            {
                int amountToAdd = amount - poolable.AmountInPool();

                for (int i = 0; i < amountToAdd; i++)
                {
                    poolable.IncrementPool();
                    yield return(null);
                }
            }

            yield return(null);
        }
Beispiel #11
0
        private void SendCreationMessage(PoolableGameObject newObj)
        {
            newObj._createMsgSent = true;

            // The object is inactive so we have to do this manually.
            MonoBehaviour[] behaviours = newObj.GetComponentsInChildren <MonoBehaviour>(true);

            for (int i = 0; i < behaviours.Length; i++)
            {
                MethodInfo method = behaviours[i].GetType().GetMethod(
                    CREATION_FUNCTION,
                    BindingFlags.NonPublic |
                    BindingFlags.Instance |
                    BindingFlags.Public,
                    null,
                    System.Type.EmptyTypes,
                    null);

                if (method != null)
                {
                    method.Invoke(behaviours[i], null);
                }
            }
        }
        private void SendCreationMessage(PoolableGameObject newObj)
        {
            // The object is inactive so we have to do this manually.
            MonoBehaviour[] behaviours = newObj.GetComponentsInChildren<MonoBehaviour>(true);

            for (int i = 0; i < behaviours.Length; i++)
            {
                MethodInfo method = behaviours[i].GetType().GetMethod(
                    CREATION_FUNCTION,
                    BindingFlags.NonPublic |
                    BindingFlags.Instance |
                    BindingFlags.Public,
                    null,
                    System.Type.EmptyTypes,
                    null);

                if (method != null)
                {
                    method.Invoke(behaviours[i], null);
                }
            }
        }
        private void SendCreationMessage(PoolableGameObject newObj)
        {
            newObj._createMsgSent = true;

            if (onObjectCreation != null)
            {
                onObjectCreation(newObj.gameObject);
            }

            _poolables = GetComponentsInChildren<IGameObjectPoolable>(true);

            for (int i = 0; i < _poolables.Length; i++)
            {
                _poolables[i].OnObjectCreated();
            }
        }
Beispiel #14
0
 public PoolInfo(PoolableGameObject p, System.Action callback)
 {
     pool = p;
     destroyCallback = callback;
 }