Exemplo n.º 1
0
        private void AddObject()
        {
            PoolableObject newObject = Instantiate(_prefab, transform);

            newObject.gameObject.SetActive(false);
            newObject.OnRecycle.AddListener(() =>
            {
                ReturnObject(newObject);
                newObject.OnReset.Invoke();
            });

            _freeObjects.Enqueue(newObject);
        }
Exemplo n.º 2
0
        public GameObject GetObject()
        {
            if (_freeObjects.Count == 0)
            {
                GameObject failsafeObject = _emptyPoolStrategy.HandleEmptyPool(_freeObjects, _activeObjects, _prefab, AddObject).gameObject;

                if (failsafeObject != null)
                {
                    return(failsafeObject);
                }
            }

            PoolableObject newObject = _freeObjects.Dequeue();

            _activeObjects.Add(newObject);

            return(newObject.gameObject);
        }
        public override PoolableObject HandleEmptyPool(Queue <PoolableObject> pool, List <PoolableObject> activeObjects, PoolableObject prefab, Action addObject)
        {
            PoolableObject newObject = activeObjects[0];

            newObject.OnRecycle.Invoke();

            return(newObject);
        }
Exemplo n.º 4
0
        public override PoolableObject HandleEmptyPool(Queue <PoolableObject> pool, List <PoolableObject> activeObjects, PoolableObject prefab, Action addObject)
        {
            PoolableObject newObject = Instantiate(prefab);

            newObject.gameObject.SetActive(false);
            newObject.OnReset.Invoke();
            return(newObject);
        }
Exemplo n.º 5
0
 public abstract PoolableObject HandleEmptyPool(Queue <PoolableObject> pool, List <PoolableObject> activeObjects, PoolableObject prefab, Action addObject);
        public override PoolableObject HandleEmptyPool(Queue <PoolableObject> pool, List <PoolableObject> activeObjects, PoolableObject prefab, Action addObject)
        {
            for (int i = 0; i < _amount; i++)
            {
                addObject();
            }

            return(null);
        }
 public override PoolableObject HandleEmptyPool(Queue <PoolableObject> pool, List <PoolableObject> activeObjects, PoolableObject prefab, Action addObject)
 {
     throw new IndexOutOfRangeException("You're trying to spawn more objects than your pool size.");
 }