using System; using System.Collections.Generic; using UnityEngine; namespace ActionCustom { internal class ObjectPool where T : class { private readonly Stack stack; internal ObjectPool() { stack = new Stack(); } internal ObjectPool(int count) { if (count < 0) { throw new ArgumentOutOfRangeException("count"); } stack = new Stack(count); } public int countAll { get; private set; } public int countActive { get { return countAll - countInactive; } } public int countInactive { get { return stack.Count; } } public T Get() { T t; if (stack.Count == 0) { t = default(T) ?? Activator.CreateInstance(); countAll++; } else { t = stack.Pop(); } return t; } public void Release(T element) { if (stack.Count > 0 && ReferenceEquals(stack.Peek(), element)) { Debug.LogError("Internal error. Trying to destroy object that is already released to pool."); return; } stack.Push(element); } } }