using System; using System.Collections.Generic; using UnityEngine.Events; namespace XUtliPoolLib { public class ObjectPool : IObjectPool { public int countAll { get; private set; } public int countActive { get { return this.countAll - this.countInactive; } } public int countInactive { get { return this.m_Stack.Count; } } private readonly Stack m_Stack = new Stack(); private readonly UnityAction m_ActionOnGet; private readonly UnityAction m_ActionOnRelease; private ObjectPool.CreateObj m_objCreator = null; public delegate T CreateObj(); public ObjectPool(ObjectPool.CreateObj creator, UnityAction actionOnGet, UnityAction actionOnRelease) { this.m_objCreator = creator; this.m_ActionOnGet = actionOnGet; this.m_ActionOnRelease = actionOnRelease; ObjectPoolCache.s_AllPool.Add(this); } public T Get() { bool flag = this.m_Stack.Count == 0; T t; if (flag) { t = this.m_objCreator(); int countAll = this.countAll; this.countAll = countAll + 1; } else { t = this.m_Stack.Pop(); } bool flag2 = this.m_ActionOnGet != null; if (flag2) { this.m_ActionOnGet.Invoke(t); } return t; } public void Release(T element) { bool flag = element != null; if (flag) { bool flag2 = this.m_ActionOnRelease != null; if (flag2) { this.m_ActionOnRelease.Invoke(element); } this.m_Stack.Push(element); } } } }