Exemplo n.º 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);
            }
        }
Exemplo n.º 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;
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
                }
            }
        }
Exemplo n.º 7
0
 public void RemoveFromPool(IGenericPoolElement element)
 {
     AvailableElements.Remove(element);
     InUseElements.Remove(element);
 }
Exemplo n.º 8
0
 public void AddToPool(IGenericPoolElement poolElement)
 {
     poolElement.PoolRef = GetHashCode();
     InUseElements.Add(poolElement);
 }
Exemplo n.º 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);
        }