summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XUtliPoolLib/ObjectPool.cs
blob: e4a7931b6bbed89e4e65d4bdad22f926dc8ce32c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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);
			}
		}
	}
}