예제 #1
0
        /// <summary>
        /// Destroys overhead of the objects to specific value
        /// </summary>
        public virtual void TrimTo(int objCount)
        {
            if (objCount <= 0)
            {
#if DEBUG
                Debug.LogError("GenericPool:: Invalid object count to trim " + objCount);
#endif
                return;
            }

            _elementBuffer.Clear();
            foreach (IGenericPoolElement element in AvailableElements)
            {
                _elementBuffer.Add(element);
            }

            for (int i = objCount - 1; i < _elementBuffer.Count; i++)
            {
                IGenericPoolElement element = _elementBuffer[i];

                InUseElements.Remove(element);
                AvailableElements.Remove(element);
                Object.Destroy(element.gameObject);
            }
        }
예제 #2
0
        public void ReturnToPool(IGenericPoolElement poolElement)
        {
            Debug.Assert(!AvailableElements.Contains(poolElement),
                         "GenericPool:: This element is already in the pool. "
                         + "This may cause some bugs, consider not decommissioning twice!");

            AvailableElements.Add(poolElement);
            InUseElements.Remove(poolElement);

            poolElement.IsCommissioned = false;
        }
예제 #3
0
        /// <summary>
        /// Returns IGenericPoolElement back to the respective GenericPooler
        /// </summary>
        public static void ReturnToPool(this IGenericPoolElement element)
        {
            Debug.Assert(element != null, "Element is null");

            if (element.UsesAutoPool)
            {
                AutoPooler.ReturnToPool(element);
            }
            else
            {
                GenericPooler.ReturnToPool(element);
            }
        }
예제 #4
0
        /// <summary>
        /// Returns object back to the pool for it to be re-used
        /// </summary>
        public void ReturnToPool(IGenericPoolElement element)
        {
            HashReference.TryGetValue(element.PoolRef, out GenericPool genericPool);

#if DEBUG
            if (genericPool == null)
            {
                Debug.LogError($"GenericPooler Pool ReUse() call failed on {element.gameObject}. "
                               + "Make sure you're using correct poolHashRef to the correct pool (*.Instance mismatch?)",
                               element.gameObject);
            }
#endif

            genericPool.ReturnToPool(element);
        }
예제 #5
0
        /// <summary>
        /// Activates first available elements and updates available collection
        /// </summary>
        /// <returns>Activated component</returns>
        public T PoolFirst <T>() where T : MonoBehaviour
        {
            IGenericPoolElement element = null;

            // Get first, .First Allocates 32b
            foreach (IGenericPoolElement el in AvailableElements)
            {
                element = el;
                break;
            }

            AvailableElements.Remove(element);
            InUseElements.Add(element);

            return(element as T);
        }
예제 #6
0
        /// <summary>
        /// Removes element from the pool completely.
        /// Use this if .Destroy needed to be called on the pooled objects that's being destroyed
        ///
        /// Automatically detects which pool to use
        /// </summary>
        /// <param name="element"></param>
        public static void RemoveFromPool(this IGenericPoolElement element)
        {
            Debug.Assert(element != null, "Element is null");

            if (element.UsesAutoPool)
            {
                if (_autoPooler != null)
                {
                    _autoPooler.RemoveFromPool(element);
                }
            }
            else
            {
                if (_genericPooler != null)
                {
                    _genericPooler.RemoveFromPool(element);
                }
            }
        }
예제 #7
0
 public void RemoveFromPool(IGenericPoolElement element)
 {
     AvailableElements.Remove(element);
     InUseElements.Remove(element);
 }
예제 #8
0
 public void AddToPool(IGenericPoolElement poolElement)
 {
     poolElement.PoolRef = GetHashCode();
     InUseElements.Add(poolElement);
 }
예제 #9
0
        /// <summary>
        /// Returns object back to the pool for it to be re-used
        /// </summary>
        public virtual void RemoveFromPool(IGenericPoolElement genericObject)
        {
            HashReference.TryGetValue(genericObject.PoolRef, out GenericPool genericPool);

            genericPool?.RemoveFromPool(genericObject);
        }