-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectPool.cs
284 lines (223 loc) · 8.01 KB
/
ObjectPool.cs
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
[Serializable]
public class ObjectPool
{
private Stack<AbstractPoolable> pool;//, secondaryPool;
private List<AbstractPoolable> activePool;
private AbstractPoolable goToPool;
public int rangeOfPool;
private int numberOfInstancesToCreateWhenAllQueuesEmpty;
private bool useActivePool;
private bool autoCleanUpEnabled;
private MemoryPoolAutoCleanUpScheduler m_scheduler;
public ObjectPool(AbstractPoolable gotopool,
int numberofinitialelements = 14,
bool useactivepool = false,
bool isautocleanupenabled = true,
float cleantimeinterval = 3f)
{
InitializeStack(
gotopool,
numberofinitialelements,
useactivepool,
isautocleanupenabled);
if (isautocleanupenabled)
{
m_scheduler = new GameObject(gotopool.ToString() + "_memorycleanscheduler").AddComponent<MemoryPoolAutoCleanUpScheduler>();
m_scheduler.CleanTimeInterval = cleantimeinterval;
m_scheduler.OnAutoMemoryCleanRequest += AutoCleanUp;
}
}
~ObjectPool()
{
m_scheduler.OnAutoMemoryCleanRequest -= AutoCleanUp;
}
/// <summary>
/// </summary>
/// <param name="gotopool">Must implement IPoolable</param>
/// <param name="numberofinitialelements"></param>
/// <param name="startaddingtosecondarypoolat"></param>
private void InitializeStack(AbstractPoolable gotopool,
int numberofinitialelements = 4,
bool useactivepool = false,
bool isautocleanupenabled = true)
{
autoCleanUpEnabled = isautocleanupenabled;
if (gotopool == null)
{
Debug.LogError("Gotopool is null!");
return;
}
useActivePool = useactivepool;
activePool = new List<AbstractPoolable>();
rangeOfPool = numberofinitialelements;
goToPool = gotopool;
pool = new Stack<AbstractPoolable>();
for (int i = 0; i < numberofinitialelements; i++)
{
var gotoadd = GenerateNewInstance();
gotoadd.gameObject.SetActive(false);
pool.Push(gotoadd);
}
//setlists();
}
/*private void setlists()
{
prim = primaryPool.ToList();
sec = secondaryPool.ToList();
}*/
private AbstractPoolable GenerateNewInstance()
{
var gotoadd = GameObject.Instantiate(goToPool, new Vector3(0, -100, 0), Quaternion.identity);
gotoadd.SetPool(this);
//setlists();
return gotoadd;
}
public AbstractPoolable AddObjectAtPosition(Vector3 position, bool isactive = true)
{
if (pool == null)
{
Debug.LogError("Pool is null! Something wrong with your implementation");
return null;
}
if (pool.Count > 0)
{
var objecttoreturn = GetInstanceFromPrimaryPool(position, isactive);
if (useActivePool)
activePool.Add(objecttoreturn);
return objecttoreturn;
}
//if both empty instantiate
else
{
var objecttoreturn = GenerateInstances(position, isactive);
if (useActivePool)
activePool.Add(objecttoreturn);
return objecttoreturn;
}
}
public GameObject AddGameObjectAtPosition(Vector3 position, bool isactive = true)
{
if (pool == null)
{
Debug.LogError("Pool is null! Something wrong with your implementation");
return null;
}
if (pool.Count > 0)
{
var objecttoreturn = GetInstanceFromPrimaryPool(position, isactive);
if (useActivePool)
activePool.Add(objecttoreturn);
return objecttoreturn.gameObject;
}
//if both empty instantiate
else
{
var objecttoreturn = GenerateInstances(position, isactive);
if (useActivePool)
activePool.Add(objecttoreturn);
return objecttoreturn.gameObject;
}
}
private AbstractPoolable GenerateInstances(Vector3 position, bool isactive = false)
{
AbstractPoolable gotoaddtolist = null;
if (numberOfInstancesToCreateWhenAllQueuesEmpty == 0) numberOfInstancesToCreateWhenAllQueuesEmpty = 3;
//add new instances to the list
for (int i = 0; i < numberOfInstancesToCreateWhenAllQueuesEmpty; i++)
{
gotoaddtolist = GenerateNewInstance();
pool.Push(gotoaddtolist);
gotoaddtolist.gameObject.SetActive(isactive);
// gotoaddtolist.transform.SetParent(primaryParentGO.transform);
//Debug.Log("Adding element to the stack: " + gotoaddtolist + i);
}
Debug.Log("Adding elements: " + numberOfInstancesToCreateWhenAllQueuesEmpty + ", to the stack: " + gotoaddtolist);
gotoaddtolist = GenerateNewInstance();
gotoaddtolist.transform.position = position;
gotoaddtolist.gameObject.SetActive(isactive);
//setlists();
return gotoaddtolist;
}
private AbstractPoolable GetInstanceFromPrimaryPool(Vector3 position, bool isactive)
{
var objecttoreturn = pool.Pop();
if (!objecttoreturn)
{
Debug.Log("null in pool!");
return GenerateInstances(position); ;
}
objecttoreturn.transform.position = position;
objecttoreturn.gameObject.SetActive(isactive);
objecttoreturn.transform.parent = null;
//setlists();
//Debug.Log("adding from 1. pool: " + objecttoreturn);
return objecttoreturn;
}
/// <summary>
/// Pooling back.
/// </summary>
/// <param name="objecttohide"></param>
public void HideObject(AbstractPoolable objecttohide)
{
if (!objecttohide)
{
Debug.LogError("Objecttohide is null! Abort pooling!");
return;
}
AbstractPoolable objectpoolable = objecttohide.GetComponent<AbstractPoolable>();
if (objectpoolable == null)
{
Debug.LogError("A non-poolable object is trying to get into the pooling system." +
"Add an IPoolable script to your gameobject! " +
"Destroying! ");
DestroyGO(objecttohide);
return;
}
//setlists();
AddInstanceToPool(objecttohide);
}
private void AddInstanceToPool(AbstractPoolable objecttohide)
{
objecttohide.gameObject.SetActive(false);
if (useActivePool)
activePool.Remove(objecttohide);
if (pool != null)
pool.Push(objecttohide);
//setlists();
}
private void AutoCleanUp()
{
if (!autoCleanUpEnabled || pool.Count <= 1) return;
//clean first pool
if (pool.Count > rangeOfPool)
{
var cleanedone = pool.Pop();
DestroyGO(cleanedone);
Debug.Log("Cleaning: " + cleanedone + ", elements left in pool: " + pool.Count);
}
}
public void DisablePool()
{
activePool.ForEach(x => DestroyGO(x));
activePool.Clear();
if (m_scheduler)
m_scheduler.OnAutoMemoryCleanRequest -= AutoCleanUp;
if (pool.Count > 0)
foreach (var pooleditem in pool)
{
DestroyGO(pooleditem);
}
}
public bool IsEmpty() => pool == null || pool.Count == 0;
public void DestroyGO<T>(T objecttokill) where T : MonoBehaviour
{
if (objecttokill == null) return;
var tokill = objecttokill as GameObject;
if (tokill) GameObject.Destroy(objecttokill);
else GameObject.Destroy(objecttokill.gameObject);
}
}