Пример #1
0
        public virtual T Retrieve()
        {
            // If there are no available items and the pool can expand, expand it
            if (Expandable && Available.Count == 0)
            {
                ExpandPool();
            }

            if (Available.Count <= 0)
            {
                throw new NoFreeItemsException();
            }

            var item = Available.Pop();

            InUse.Add(item);             // Record this as an item inuse

            // Notify the object it has become active
            try {
                if (item is IPoolNotifiable notifiable)
                {
                    notifiable.PoolInUse(true);
                }
            } catch (Exception ex) {
                if (item is Object unityItem)
                {
                    Debug.LogError(ex, unityItem);
                }
                else
                {
                    Debug.LogError(ex);
                }
            }

            OnRetrieve?.Invoke(this, item);


            // Notify the object it has been returned to the pool
            try {
                if (item is IPoolNotifiablePost notifiable)
                {
                    notifiable.PoolInUsePost(true);
                }
            } catch (Exception ex) {
                if (item is Object unityItem)
                {
                    Debug.LogError(ex, unityItem);
                }
                else
                {
                    Debug.LogError(ex);
                }
            }
            return(item);
        }
Пример #2
0
        /// <summary>
        /// Retrieves an unused object from the pool
        /// </summary>
        /// <returns>An available object from the pool</returns>
        public T Retrieve()
        {
            // If there are no available items and the pool can expand, expand it
            if (Expandable && Available.Count == 0)
            {
                ExpandPool();
            }

            if (Available.Count <= 0)
            {
                throw new NoFreeItemsException();
            }

            T found = Available.Pop();

            InUse.Add(found);             // Record this as an item inuse
            found.InUse = true;
            found.PoolRetrieved();
            OnRetrieve?.Invoke(found);
            return(found);
        }