using System; using System.Collections.Generic; using UnityEngine; public class ObjectPoolBehavior : IObjectPool { public override int InUse { get { return this.activeChildren.Count; } } public override int NotInUse { get { return this.inactiveChildren.Count; } } public int poolSize = 20; [SerializeField] private List inactiveChildren = new List(); [SerializeField] public List activeChildren = new List(); public PoolableBehavior Prefab; public bool AutoInit; public bool DetachOnGet; public virtual void Awake() { if (this.AutoInit) { this.InitPool(this.Prefab); } } public void InitPool(PoolableBehavior prefab) { this.AutoInit = false; for (int i = 0; i < this.poolSize; i++) { this.CreateOneInactive(prefab); } } private void CreateOneInactive(PoolableBehavior prefab) { PoolableBehavior poolableBehavior = UnityEngine.Object.Instantiate(prefab); poolableBehavior.transform.SetParent(base.transform); poolableBehavior.gameObject.SetActive(false); poolableBehavior.OwnerPool = this; this.inactiveChildren.Add(poolableBehavior); } public void ReclaimOldest() { if (this.activeChildren.Count > 0) { this.Reclaim(this.activeChildren[0]); return; } this.InitPool(this.Prefab); } public void ReclaimAll() { foreach (PoolableBehavior obj in this.activeChildren.ToArray()) { this.Reclaim(obj); } } public override T Get() { List obj = this.inactiveChildren; PoolableBehavior poolableBehavior; lock (obj) { if (this.inactiveChildren.Count == 0) { if (this.activeChildren.Count == 0) { this.InitPool(this.Prefab); } else { this.CreateOneInactive(this.Prefab); } } poolableBehavior = this.inactiveChildren[this.inactiveChildren.Count - 1]; this.inactiveChildren.RemoveAt(this.inactiveChildren.Count - 1); this.activeChildren.Add(poolableBehavior); } if (this.DetachOnGet) { poolableBehavior.transform.SetParent(null, false); } poolableBehavior.gameObject.SetActive(true); poolableBehavior.Reset(); return poolableBehavior as T; } public override void Reclaim(PoolableBehavior obj) { if (!this) { DefaultPool.Instance.Reclaim(obj); return; } obj.gameObject.SetActive(false); obj.transform.SetParent(base.transform); List obj2 = this.inactiveChildren; lock (obj2) { if (this.activeChildren.Remove(obj)) { this.inactiveChildren.Add(obj); } else if (this.inactiveChildren.Contains(obj)) { Debug.Log("ObjectPoolBehavior: :| Something was reclaimed without being gotten"); } else { Debug.Log("ObjectPoolBehavior: Destroying this thing I don't own"); UnityEngine.Object.Destroy(obj.gameObject); } } } }