-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstancePool.cs
More file actions
353 lines (333 loc) · 10.7 KB
/
InstancePool.cs
File metadata and controls
353 lines (333 loc) · 10.7 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;
namespace UnityEssentials.Pooling
{
/// <summary>
/// A pool of GameObjects with a specific component attached.
/// </summary>
/// <typeparam name="T">The component attached to each instance of this pool.</typeparam>
public class InstancePool<T> where T : Component
{
/// <summary>
/// The scene to which this pool is bound to. If set, all objects are destroyed when this scene is unloaded. If left null, instances are created in the active scene (unless <see cref="dontDestroyOnLoad"/> is set)
/// </summary>
public Scene? TargetScene { get; private set; }
/// <summary>
/// The maximum number of objects that can be instantiated for this pool.
/// </summary>
public int maxPoolSize;
/// <summary>
/// An optional function that supplies custom instantiation behaviour (when an object is first created)
/// </summary>
public Func<T> instantiator;
/// <summary>
/// An optional callback that is invoked when an instance is activated.
/// </summary>
public Action<T> activator = null;
/// <summary>
/// An optional callback that is invoked when an instance is deactivated.
/// </summary>
public Action<T> deactivator = null;
/// <summary>
/// If set to true, old, already active instances are reused if all objects are in use and the max pool size is reached.
/// </summary>
public bool replaceLast = false;
/// <summary>
/// If set to true, instances will be independent from any loaded scenes and are not destroyed when loading another scene.
/// </summary>
public bool dontDestroyOnLoad = false;
/// <summary>
/// An optional duration that limits the maximum active time for each instance until they are forcefully released.
/// </summary>
public float? maxActiveTime = null;
/// <summary>
/// The number of currently unused instances in this pool.
/// </summary>
public int InactiveInstanceCount => inactivePool.Count;
/// <summary>
/// The number of currently used instances in this pool.
/// </summary>
public int ActiveInstanceCount => activePool.Count;
/// <summary>
/// The total number of instances that exist in this pool.
/// </summary>
public int TotalInstanceCount => InactiveInstanceCount + ActiveInstanceCount;
private readonly List<T> inactivePool;
private readonly List<T> activePool;
private readonly Dictionary<T, float> lastActivationTimes;
private readonly T[] iterationCache;
private float lastUpdateTime;
/// <summary>
/// Creates a new instance pool.
/// </summary>
public InstancePool(Scene? scene, int maxPoolSize, Func<T> instantiator = null, Action<T> activator = null, Action<T> deactivator = null,
bool dontDestroyOnLoad = false, bool createAllInstances = false, float? maxActiveTime = null)
{
if(scene.HasValue && !scene.Value.IsValid()) throw new ArgumentException("Scene is invalid");
inactivePool = new List<T>();
activePool = new List<T>();
lastActivationTimes = new Dictionary<T, float>();
TargetScene = scene;
this.maxPoolSize = maxPoolSize;
this.instantiator = instantiator ?? DefaultInstantiator;
this.activator = activator;
this.deactivator = deactivator;
this.dontDestroyOnLoad = dontDestroyOnLoad;
this.maxActiveTime = maxActiveTime;
iterationCache = new T[maxPoolSize];
if(createAllInstances)
{
CreateAllInstances();
}
SceneManager.sceneUnloaded += OnSceneUnloaded;
Application.quitting += OnApplicationQuit;
#if UNITY_EDITOR
UnityEditor.AssemblyReloadEvents.beforeAssemblyReload += OnAssemblyReload;
#endif
}
/// <summary>
/// Manually updates this pool.
/// Updates are responsible for releasing instances when their max lifetime has exceeded (if enabled).
/// Updates are also automatically invoked whenever an instance is activated or released.
/// </summary>
public void Update()
{
float gameTime = Time.time;
if(gameTime == lastUpdateTime) return;
lastUpdateTime = gameTime;
if(maxActiveTime.HasValue)
{
//Release any instances that have exceeded their max life time
int activePoolCount = activePool.Count;
for(int i = 0; i < activePoolCount; i++)
{
var instance = activePool[i];
float activeTime = gameTime - lastActivationTimes[instance];
if(activeTime > maxActiveTime.Value)
{
ReleaseInstance(instance);
activePoolCount--;
i--;
}
}
}
}
/// <summary>
/// Activates an instance from this object pool. When no inactive instances are present, a new instance is instantiated.
/// If the active objects pool is full and 'replaceLast' is enabled, the oldest active instance is recycled,
/// otherwise this method returns null.
/// </summary>
/// <returns>The instance that was activated. Can be a newly created instance or a recycled one.</returns>
public T ActivateInstance()
{
Update();
T inst;
if(inactivePool.Count > 0)
{
inst = inactivePool[0];
inactivePool.RemoveAt(0);
}
else
{
if(TotalInstanceCount < maxPoolSize)
{
inst = CreateInstance();
}
else
{
if(replaceLast)
{
//Take the oldest active object and reactivate it.
inst = activePool[0];
activePool.RemoveAt(0);
inst.gameObject.SetActive(false);
deactivator?.Invoke(inst);
}
else
{
Debug.LogError("Max pool size reached. Increase the max pool size or have less objects active at the same time or enable 'alwaysDecativateOldest' to keep activating instances.");
return null;
}
}
}
activePool.Add(inst);
inst.gameObject.SetActive(true);
lastActivationTimes[inst] = Time.time;
activator?.Invoke(inst);
return inst;
}
/// <summary>
/// Forces activation of a new instance by deactivating an already active one if needed, regardless of the 'replaceLast' setting.
/// </summary>
/// <returns>The instance that was activated. Can be a newly created instance or a recycled one.</returns>
public T ForceActivateInstance()
{
if(ActiveInstanceCount >= maxPoolSize)
{
//Deactivate the oldest active object, so it can be reactivated again.
ReleaseInstance(activePool[0]);
}
return ActivateInstance();
}
/// <summary>
/// Releases and deactivates the given instance from the active pool.
/// </summary>
public void ReleaseInstance(T inst)
{
Update();
if(activePool.Remove(inst))
{
inst.gameObject.SetActive(false);
deactivator?.Invoke(inst);
inactivePool.Add(inst);
}
else
{
if(inactivePool.Contains(inst))
{
Debug.LogError("The given pooled object was already deactivated.");
}
else
{
throw new InvalidOperationException("The given object was not part of the pool.");
}
}
}
/// <summary>
/// Releases all active instances from this pool.
/// </summary>
public void ReleaseAllInstances()
{
ForEachActiveInstance((inst) => ReleaseInstance(inst));
}
/// <summary>
/// Destroys all instances created from this pool.
/// </summary>
public void DestroyAllInstances()
{
foreach(var active in activePool)
{
if(active != null) Object.Destroy(active.gameObject);
}
foreach(var inactive in inactivePool)
{
if(inactive != null) Object.Destroy(inactive.gameObject);
}
activePool.Clear();
inactivePool.Clear();
lastActivationTimes.Clear();
//Debug.Log($"InstancePool<{typeof(T).Name}> and all of its instances have been destroyed.");
}
/// <summary>
/// Iterates over all active instances in this pool.
/// </summary>
public void ForEachActiveInstance(Action<T> iterator)
{
int count = activePool.Count;
activePool.CopyTo(iterationCache);
for(int i = 0; i < count; i++)
{
iterator(iterationCache[i]);
}
}
/// <summary>
/// Iterates over all inactive instances in this pool.
/// </summary>
public void ForEachInactiveInstance(Action<T> iterator)
{
int count = inactivePool.Count;
inactivePool.CopyTo(iterationCache);
for(int i = 0; i < count; i++)
{
iterator(iterationCache[i]);
}
}
/// <summary>
/// Instantiates all objects used by this pool.
/// </summary>
public void CreateAllInstances()
{
while(TotalInstanceCount < maxPoolSize)
{
var inst = CreateInstance();
inactivePool.Add(inst);
}
}
/// <summary>
/// Transfers all instances to the given scene. Newly created instances will also become part of the given scene.
/// </summary>
/// <param name="includeDontDestroyOnLoad">If true, instances that are marked as 'DontDestroyOnLoad' will also be moved to the scene.</param>
public void TransferToScene(Scene newScene, bool includeDontDestroyOnLoad = false)
{
Update();
foreach(var inst in activePool)
{
if(inst && (includeDontDestroyOnLoad || inst.gameObject.scene.name != "DontDestroyOnLoad")) SceneManager.MoveGameObjectToScene(inst.gameObject, newScene);
}
foreach(var inst in inactivePool)
{
if(inst && (includeDontDestroyOnLoad || inst.gameObject.scene.name != "DontDestroyOnLoad")) SceneManager.MoveGameObjectToScene(inst.gameObject, newScene);
}
TargetScene = newScene;
}
/// <summary>
/// Returns the duration the given instance has been active for.
/// </summary>
/// <param name="skipChecks">If true, this method will not check if the given instance belongs to this pool, which may raise an exception.
/// Recommended when invoking this inside tight loops.</param>
public float GetInstanceActiveTime(T instance, bool skipChecks = false)
{
if(skipChecks || activePool.Contains(instance))
{
return Time.time - lastActivationTimes[instance];
}
else
{
Debug.LogError("Unable to get life time for this instance, object is not active or not part of this pool.");
return 0;
}
}
private T CreateInstance()
{
T inst;
inst = instantiator.Invoke();
if(inst == null)
{
throw new NullReferenceException("Instantiated object was null.");
}
if(dontDestroyOnLoad)
{
Object.DontDestroyOnLoad(inst.gameObject);
}
else if(TargetScene.HasValue && inst.gameObject.scene != TargetScene)
{
SceneManager.MoveGameObjectToScene(inst.gameObject, TargetScene.Value);
}
return inst;
}
private T DefaultInstantiator()
{
var go = new GameObject("PooledObject");
if(typeof(T) == typeof(Transform)) return (T)(Component)go.transform;
else return go.AddComponent<T>();
}
private void OnSceneUnloaded(Scene scene)
{
if(scene == TargetScene) DestroyAllInstances();
}
private void OnApplicationQuit() => DestroyAllInstances();
#if UNITY_EDITOR
private void OnAssemblyReload()
{
if(Application.isPlaying)
{
Debug.LogWarning("Assembly reload detected, destroying all pooled instances to avoid leaking objects.");
DestroyAllInstances();
}
}
#endif
}
}