Пример #1
0
        /// <summary>
        /// Add unity game object to pool.
        /// </summary>
        /// <param name="amount">Amount of game object clones to add to pool.</param>
        /// <param name="prefab">Prefab refernce which will be added to pool.</param>
        /// <returns>Pooled object key. It's needed to fetch pooled game object from pool.</returns>
        public string PopulatePool(int amount, BasePoolable prefab)
        {
            if (prefab == null)
            {
                Debug.LogError("No prefab passed");
            }

            var key = prefab.GetPoolKey();

            if (!_prefabCache.ContainsKey(key))
            {
                _prefabCache.Add(key, prefab);
            }

            if (!_gameObjectPool.TryGetValue(key, out var indicatedPool))
            {
                indicatedPool = new Stack <BasePoolable>();
            }

            for (var i = 0; i < amount; i++)
            {
                var el = InstantiateObject(prefab);
                indicatedPool.Push(el);
            }

            _gameObjectPool.Add(key, indicatedPool);

            return(key);
        }
Пример #2
0
        public void ReturnObjectToPool(string objectKey, BasePoolable obj)
        {
            if (!_gameObjectPool.TryGetValue(objectKey, out var indicatedPool))
            {
                Debug.LogError("Object pool not found for given key.");
            }

            obj.OnAddToPool();
            indicatedPool.Push(obj);
        }
Пример #3
0
        protected virtual BasePoolable InstantiateObject(BasePoolable prefab)
        {
            var obj = Instantiate(prefab);

            if (obj == null)
            {
                Debug.LogError($"Can not instantiate {prefab.name} for pool.");
            }

            // Get reference of poolable component
            //var component = obj.GetComponent<BasePoolable>();

            //if(component == null) {
            //    Debug.LogWarning("No poolable component attached to the object");
            //} else {
            //    component.OnAddToPool();
            //}
            obj.OnAddToPool();

            return(obj);
        }