Exemplo n.º 1
0
        public void InitializePool(EPools pool, int overrideAmount = 0, Action <GameObject> OnObjectInstantiated = null)
        {
            PoolScriptableObject.PoolDataStruct data = _poolSettings.Data[pool.ToString()];

            GameObject nPool = new GameObject();

            nPool.name = pool.ToString();
            nPool.transform.SetParent(this.transform);
            List <GameObject> objects = new List <GameObject>();
            int amount = overrideAmount > 0 ? overrideAmount : data.StartingAmount;

            for (int i = 0; i < amount; i++)
            {
                GameObject obj = Instantiate(data.Object, nPool.transform);
                obj.SetActive((false));
                objects.Add(obj);
                OnObjectInstantiated?.Invoke(obj);
            }

            Pool cPool = new Pool
            {
                poolType = pool,
                objects  = new Stack <GameObject>(objects),
                parent   = nPool
            };

            _pools.Add(cPool.poolType, cPool);
        }
Exemplo n.º 2
0
        public GameObject Instantiate(EPools pool, Vector3 pos, Transform parent = null)
        {
            GameObject obj;

            if (_pools[pool].objects.Count > 0)
            {
                obj = _pools[pool].objects.Pop();
                obj.transform.SetParent(parent);
                obj.transform.position = pos;
                obj.SetActive((true));
            }
            else
            {
                obj = Instantiate(_poolSettings.Data[pool.ToString()].Object, parent);
                obj.transform.position = pos;
                obj.SetActive((true));
            }

            return(obj);
        }