From 6eb915c129fc90c6f4c82ae097dd6ffad5239efc Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 25 Jan 2021 14:28:30 +0800 Subject: +scripts --- Client/Assets/Scripts/XUtliPoolLib/ObjectPool.cs | 81 ++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 Client/Assets/Scripts/XUtliPoolLib/ObjectPool.cs (limited to 'Client/Assets/Scripts/XUtliPoolLib/ObjectPool.cs') diff --git a/Client/Assets/Scripts/XUtliPoolLib/ObjectPool.cs b/Client/Assets/Scripts/XUtliPoolLib/ObjectPool.cs new file mode 100644 index 00000000..e4a7931b --- /dev/null +++ b/Client/Assets/Scripts/XUtliPoolLib/ObjectPool.cs @@ -0,0 +1,81 @@ +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); + } + } + } +} -- cgit v1.1-26-g67d0