forked from hunterzonewu/unity-decompiled
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectPool`1.cs
More file actions
66 lines (58 loc) · 1.76 KB
/
ObjectPool`1.cs
File metadata and controls
66 lines (58 loc) · 1.76 KB
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
// Decompiled with JetBrains decompiler
// Type: UnityEngine.UI.ObjectPool`1
// Assembly: UnityEngine.UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 2216A18B-AF52-44A5-85A0-A1CAA19C1090
// Assembly location: C:\Users\Blake\sandbox\unity\test-project\Library\UnityAssemblies\UnityEngine.UI.dll
using System.Collections.Generic;
using UnityEngine.Events;
namespace UnityEngine.UI
{
internal class ObjectPool<T> where T : new()
{
private readonly Stack<T> m_Stack = new Stack<T>();
private readonly UnityAction<T> m_ActionOnGet;
private readonly UnityAction<T> m_ActionOnRelease;
public int countAll { get; private set; }
public int countActive
{
get
{
return this.countAll - this.countInactive;
}
}
public int countInactive
{
get
{
return this.m_Stack.Count;
}
}
public ObjectPool(UnityAction<T> actionOnGet, UnityAction<T> actionOnRelease)
{
this.m_ActionOnGet = actionOnGet;
this.m_ActionOnRelease = actionOnRelease;
}
public T Get()
{
T obj;
if (this.m_Stack.Count == 0)
{
obj = new T();
++this.countAll;
}
else
obj = this.m_Stack.Pop();
if (this.m_ActionOnGet != null)
this.m_ActionOnGet(obj);
return obj;
}
public void Release(T element)
{
if (this.m_Stack.Count > 0 && object.ReferenceEquals((object) this.m_Stack.Peek(), (object) element))
Debug.LogError((object) "Internal error. Trying to destroy object that is already released to pool.");
if (this.m_ActionOnRelease != null)
this.m_ActionOnRelease(element);
this.m_Stack.Push(element);
}
}
}