blob: 657837b7d3a542afb7bcdaff365f06622488a28e (
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
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<PoolableBehavior> inactiveChildren = new List<PoolableBehavior>();
[SerializeField]
public List<PoolableBehavior> activeChildren = new List<PoolableBehavior>();
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<PoolableBehavior>(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<T>()
{
List<PoolableBehavior> 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<PoolableBehavior> 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);
}
}
}
}
|