예제 #1
0
        private IEnumerator DoInitSet(PoolingSet poolingSet)
        {
            if (!poolingSet.IsValid())
            {
                yield break;
            }

            _amount            = poolingSet.AmountToPool;
            poolingSet.Objects = new List <GameObject>(_amount);

            _hasFinishedCreatingSet = false;

            for (int j = 0; j < _amount; j++)
            {
                AddObjectToPoolingSet(poolingSet, j.ToString());
                if (_incrementalInitialization)
                {
                    yield return(null);
                }
            }

            if (!_poolingSets.Contains(poolingSet))
            {
                _poolingSets.Add(poolingSet);
            }

            _hasFinishedCreatingSet = true;
        }
예제 #2
0
        private GameObject GetPooledObject(PoolingSet p_set)
        {
            List <GameObject> p_collection = p_set.Objects;

            for (int i = 0; i < p_collection.Count; i++)
            {
                if (!p_collection[i].activeInHierarchy)
                {
                    //Debug.Log("Got: " + p_collection[i].name);
                    if (p_set.ParentTransform != null)
                    {
                        p_collection[i].transform.SetParent(p_set.ParentTransform);
                    }
                    p_collection[i].GetComponent <IPoolingItem>()?.Reset();
                    return(p_collection[i]);
                }
            }

            if (!_canIncreaseInSize)
            {
                return(null);
            }
            Debug.LogWarning("No object would be available, but we made more of: " + p_set.Prefab.name);
            return(AddObjectToPoolingSet(p_set, p_collection.Count.ToString()));
        }
예제 #3
0
        public GameObject GetPooledObject(GameObject p_prefab)
        {
            PoolingSet set = GetSet(p_prefab);

            if (set == null)
            {
                Debug.LogWarning("No set contains the needed prefab: " + p_prefab.name);
                return(null);
            }

            return(GetPooledObject(set));
        }
예제 #4
0
        public void AddPoolingSet(PoolingSet poolingSet)
        {
            if (!poolingSet.IsValid())
            {
                Debug.LogWarning("Set not valid!"); return;
            }
            if (!_hasInit || !_hasFinishedCreatingSet)
            {
                Debug.LogWarning("Another operation is still taking place."); return;
            }

            StartCoroutine(DoInitSet(poolingSet));
        }
예제 #5
0
        private GameObject AddObjectToPoolingSet(PoolingSet p_poolingSet, string p_suffix)
        {
            _go       = Instantiate(p_poolingSet.Prefab);
            _go.name += p_poolingSet.Prefab.name + " " + p_suffix;

            if (p_poolingSet.ParentTransform != null)
            {
                _go.transform.SetParent(p_poolingSet.ParentTransform);
            }

            _go.SetActive(false);
            p_poolingSet.Objects.Add(_go);
            return(_go);
        }
예제 #6
0
        public void ClearSet(PoolingSet poolingSet)
        {
            if (poolingSet == null)
            {
                return;
            }

            var objs = poolingSet.Objects;

            if (objs == null || objs.Count == 0)
            {
                return;
            }

            for (int i = poolingSet.Objects.Count; i >= 0; i--)
            {
                Destroy(objs[i]);
            }
            _poolingSets.Remove(poolingSet);
        }