using System; using System.Collections.Generic; using UnityEngine; namespace HyperlinkText { public class ObjectPool where T : new() { public ObjectPool(Action onGet, Action onRelease) { this.m_OnGet = onGet; this.m_OnRelease = onRelease; } public int CountAll { get; set; } public int CountActive { get { return this.CountAll - this.CountInactive; } } public int CountInactive { get { return this.m_Stack.Count; } } public T Get() { T t; if (this.m_Stack.Count == 0) { t = Activator.CreateInstance(); this.CountAll++; } else { t = this.m_Stack.Pop(); } if (this.m_OnGet != null) { this.m_OnGet(t); } return t; } public void Release(T item) { if (this.m_Stack.Count > 0) { T t = this.m_Stack.Peek(); if (t.Equals(item)) { Debug.LogError("Internal error. Trying to destroy object that is already released to pool."); } } if (this.m_OnRelease != null) { this.m_OnRelease(item); } this.m_Stack.Push(item); } private readonly Stack m_Stack = new Stack(); private readonly Action m_OnGet; private readonly Action m_OnRelease; } }