-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathObjectPooler.cs
318 lines (270 loc) · 8.44 KB
/
ObjectPooler.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
[Serializable]
public class ObjectPool
{
public GameObject ObjectToPool;
public string PoolName;
public int AmountToPool;
public bool ShouldExpand = true;
public ObjectPool() { }
public ObjectPool(GameObject prefab, string name, int amount, bool expandable = true)
{
ObjectToPool = prefab;
PoolName = name;
AmountToPool = amount;
ShouldExpand = expandable;
}
public void Initialize()
{
if (ObjectToPool != null)
{
Id = ObjectToPool.GetInstanceID();
Initialized = true;
}
}
public bool Initialized { get; private set; }
public int Id { get; private set; }
public GameObject ParentPoolObject { get; set; }
public LinkedList<GameObjectContainer> Items { get; } = new LinkedList<GameObjectContainer>();
public int Recycles { get; set; }
public int Created { get; set; }
public int Expands { get; set; }
}
public interface IPoolable
{
void Spawn();
void Despawn();
}
public class GameObjectContainer
{
public ObjectPool Pool;
public GameObject Object;
public List<IPoolable> PoolingEnabledComponents;
public int Cycles;
public int TimesSkipped;
public int TimesSelected;
public int Despawns;
public int Spawns;
public int ObjectId;
}
public class ObjectPooler : MonoBehaviour
{
public static ObjectPooler Instance;
public string RootPoolName = "Pooled Objects";
public List<ObjectPool> Pools;
public Dictionary<int, GameObjectContainer> Map { get; } = new Dictionary<int, GameObjectContainer>();
void Awake()
{
Instance = this;
SceneManager.sceneLoaded += SceneManagerOnSceneLoaded;
}
private void SceneManagerOnSceneLoaded(Scene arg0, LoadSceneMode loadSceneMode)
{
Debug.Log("Resetting Object Pools");
Reset();
}
private void CreatePools()
{
foreach (var item in Pools)
{
for (int i = 0; i < item.AmountToPool; i++)
{
CreatePooledObject(item);
}
}
}
public void Reset()
{
foreach (var pool in Pools)
{
if(pool == null)
continue;
foreach (var item in pool.Items)
{
if(item == null)
continue;
if (item.Object != null)
{
Destroy(item.Object);
}
item.Object = null;
item.Pool = null;
item.PoolingEnabledComponents.Clear();
}
pool.Items.Clear();
pool.ParentPoolObject = null;
}
Map.Clear();
CreatePools();
}
/// <summary>
/// Find/Create the parent which pooled objects should be attached to.
/// </summary>
private GameObject GetParentPoolObject(string objectPoolName)
{
if (string.IsNullOrEmpty(objectPoolName))
objectPoolName = RootPoolName;
var parentObject = GameObject.Find(objectPoolName);
if (parentObject != null)
return parentObject;
parentObject = new GameObject
{
name = objectPoolName
};
if (objectPoolName == RootPoolName)
return parentObject;
var root = GameObject.Find(RootPoolName) ?? GetParentPoolObject(RootPoolName);
parentObject.transform.parent = root.transform;
return parentObject;
}
/// <summary>
/// Create a new item for a given pool
/// </summary>
private GameObjectContainer CreatePooledObject(ObjectPool pool)
{
if (pool.ObjectToPool == null)
{
throw new Exception($"Object pool entry '{pool.PoolName}' needs a prefab attached");
}
if (!pool.Initialized)
pool.Initialize();
var obj = Instantiate(pool.ObjectToPool);
obj.name = obj.name;
if (pool.ParentPoolObject == null)
pool.ParentPoolObject = GetParentPoolObject(pool.PoolName);
obj.transform.parent = pool.ParentPoolObject.transform;
obj.SetActive(false);
var container = new GameObjectContainer
{
Object = obj,
ObjectId = obj.GetInstanceID(),
Pool = pool,
PoolingEnabledComponents = obj.GetComponents<IPoolable>().ToList(),
};
Map.Add(obj.GetInstanceID(), container);
pool.Items.AddFirst(container);
pool.Created++;
return container;
}
/// <summary>
/// Create an instance of an GameObject prefab.
/// A replacment for 'Instantiate(...)'.
/// </summary>
/// <param name="prefab">A game object prefab to create an instance of</param>
/// <param name="position">The position of the spawned GameObject</param>
/// <param name="rotation">The rotation of the spawned GameObject</param>
/// <returns>pooled GameObject</returns>
public GameObject Spawn(GameObject prefab, Vector3 position, Quaternion rotation)
{
var id = prefab.GetInstanceID();
var pool = GetPoolForPrefab(id);
if (pool == null)
{
pool = new ObjectPool(prefab, prefab.name, 25);
Debug.Log($"Dynamically creating pool for prefab {prefab.name}");
Pools.Add(pool);
//throw new Exception($"Unable to find object pool for type");
}
var container = FindFreePoolItem(pool);
if (container == null)
{
if (!pool.ShouldExpand)
return null;
container = CreatePooledObject(pool);
pool.Expands++;
}
else
{
pool.Recycles++;
}
container.Spawns++;
RecycleItem(container, position, rotation);
return container.Object;
}
/// <summary>
/// Return GameObject to the pool.
/// A replacement for 'Destroy(...)'.
/// </summary>
/// <param name="o"></param>
public void Despawn(GameObject o)
{
if (o == null)
return;
var container = GetContainer(o.GetInstanceID());
if (container != null)
{
container.Despawns++;
foreach (var c in container.PoolingEnabledComponents)
{
c.Despawn();
}
}
o.SetActive(false);
}
/// <summary>
/// Reset transform and call IPoolable.Spawn on components.
/// </summary>
private static void RecycleItem(GameObjectContainer container, Vector3 position, Quaternion rotation)
{
var t = container.Object.transform;
t.rotation = rotation;
t.position = position;
container.Object.SetActive(true);
container.Cycles++;
foreach (var c in container.PoolingEnabledComponents)
{
c.Spawn();
}
}
/// <summary>
/// Get an item from a given pool that is not being used.
/// </summary>
private GameObjectContainer FindFreePoolItem(ObjectPool pool)
{
for (int i = 0; i < pool.Items.Count; i++)
{
var node = pool.Items.First;
pool.Items.RemoveFirst();
pool.Items.AddLast(node);
// Clean out objects that no longer exist (because of scene unload etc)
var obj = node.Value.Object;
if (obj == null)
{
DestroyContainer(node.Value);
continue;
}
if (!obj.activeInHierarchy)
{
node.Value.TimesSelected++;
return node.Value;
}
node.Value.TimesSkipped++;
}
return null;
}
private void DestroyContainer(GameObjectContainer container)
{
container.Pool.Items.Remove(container);
Map.Remove(container.ObjectId);
}
private ObjectPool GetPoolForPrefab(int prefabInstanceId)
{
for (int i = 0; i < Pools.Count; i++)
{
var pool = Pools[i];
if (pool.Id == prefabInstanceId)
return pool;
}
return null;
}
private GameObjectContainer GetContainer(int gameObjectInstanceId)
{
GameObjectContainer container;
Map.TryGetValue(gameObjectInstanceId, out container);
return container;
}
}