Пример #1
0
        public void Initialize(PoolElement original)
        {
            _original = original;
            _pool     = new List <PoolElement>();
            _released = new List <PoolElement>();

            gameObject.SetActive(false);
        }
Пример #2
0
        public void PutElement(PoolElement element, int count)
        {
            for (int i = 0; i < count; i++)
            {
                var x = Duplicate(element);

                x.Initialize(this);
                _pool.Add(x);

                RefreshCleanUpTime();
            }
        }
Пример #3
0
        public void ReturnElement(PoolElement element)
        {
            if (_released.Contains(element))
            {
                _released.Remove(element);
            }

            _pool.Add(element);

            element.OnReturn();

            element.transform.SetParent(transform);
            element.transform.SetPositionAndRotation(transform.position, transform.rotation);
        }
Пример #4
0
        private PoolElement Duplicate(PoolElement element)
        {
            if (element == null)
            {
                Debug.LogWarning("Не могу создать пустой элемент");
                return(null);
            }

            var result = Instantiate(element, transform);

            if (!result.gameObject.activeSelf)
            {
                result.gameObject.SetActive(true);
            }

            return(result);
        }
Пример #5
0
        private IPool CreatePool <T>(PoolElement originalPrefab) where T : PoolElement
        {
            var type = typeof(T);

            var poolRoot = new GameObject("[Pool]: " + type.Name);

            if (!_root)
            {
                CreateModuleRoot();
            }

            poolRoot.transform.SetParent(_root.transform);

            var pool = poolRoot.AddComponent <Pool>();

            pool.Initialize(originalPrefab);

            _pools.Add(type, pool);

            return(pool);
        }