diff options
Diffstat (limited to 'Client/Assets/Scripts/XUtliPoolLib/ObjectPool.cs')
-rw-r--r-- | Client/Assets/Scripts/XUtliPoolLib/ObjectPool.cs | 81 |
1 files changed, 81 insertions, 0 deletions
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<T> : 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<T> m_Stack = new Stack<T>();
+
+ private readonly UnityAction<T> m_ActionOnGet;
+
+ private readonly UnityAction<T> m_ActionOnRelease;
+
+ private ObjectPool<T>.CreateObj m_objCreator = null;
+
+ public delegate T CreateObj();
+
+ public ObjectPool(ObjectPool<T>.CreateObj creator, UnityAction<T> actionOnGet, UnityAction<T> 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);
+ }
+ }
+ }
+}
|