-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helper.cs
322 lines (265 loc) · 8.32 KB
/
Helper.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
319
320
321
322
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Helper
{
#region Design Patterns
#region POOLS
// OBJECT POOL W\ GAMEOBJECTS
public class ObjectPool
{
public GameObject basePrefab;
private Queue<GameObject> objectPool;
public string PrefabName => basePrefab.name;
public ObjectPool(GameObject prefab, int poolSize)
{
basePrefab = prefab;
objectPool = new Queue<GameObject>();
for (int i = 0; i < poolSize; i++)
{
GameObject obj = MonoBehaviour.Instantiate(prefab);
obj.name = PrefabName; // to remove the (Clone) label
obj.SetActive(false);
objectPool.Enqueue(obj);
}
}
public GameObject GetObject()
{
if (objectPool.Count > 0)
{
GameObject obj = objectPool.Dequeue();
return obj;
}
else
{
GameObject obj = MonoBehaviour.Instantiate(basePrefab);
obj.name = PrefabName; // to remove the (Clone) label
return obj;
}
}
public void ReturnObject(GameObject obj)
{
obj.SetActive(false);
objectPool.Enqueue(obj);
}
}
// GENERIC POOL
public class GenericObjectPool<T> where T : Component
{
public T basePrefab;
private Queue<T> objectPool;
public GenericObjectPool(T prefab, int poolSize)
{
basePrefab = prefab;
objectPool = new Queue<T>();
for (int i = 0; i < poolSize; i++)
{
var obj = MonoBehaviour.Instantiate(prefab);
obj.gameObject.SetActive(false);
objectPool.Enqueue(obj);
}
}
public T GetObject()
{
if (objectPool.Count > 0)
{
T obj = objectPool.Dequeue();
return obj;
}
else
{
T obj = MonoBehaviour.Instantiate(basePrefab);
return obj;
}
}
public void ReturnObject(T obj)
{
obj.gameObject.SetActive(false);
objectPool.Enqueue(obj);
}
}
#endregion
// FACTORY W\ POOLS OF GAMEOBJECTS (we ask for the GO name, maybe try to implement the asking with an enum (?))
public class Factory : MonoBehaviour
{
public int poolSize;
public GameObject[] prefabs;
protected Dictionary<string, Helper.ObjectPool> pools;
private void Start()
{
InitPools();
}
private void InitPools()
{
pools = new Dictionary<string, Helper.ObjectPool>();
foreach (GameObject go in prefabs)
pools.Add(go.name, new Helper.ObjectPool(go, poolSize));
}
public GameObject CreateObject(string name, Vector3 position)
{
return CreateObject(name, position, Quaternion.identity);
}
public GameObject CreateObject(string objectName, Vector3 position, Quaternion rotation, bool setActive = true)
{
if (!pools.ContainsKey(objectName))
{
Debug.LogWarning(string.Format("Could not find a pooled object with name '{0}'", name));
return null;
}
GameObject instance = pools[objectName].GetObject();
instance.transform.position = position;
instance.transform.rotation = rotation;
instance.SetActive(setActive);
return instance;
}
public bool ReturnObject(GameObject instance)
{
if (!pools.ContainsKey(instance.name))
{
Debug.LogWarning(string.Format("Could not find a pooled object with name '{0}'", name));
return false;
}
pools[instance.name].ReturnObject(instance);
return true;
}
}
// FACTORY AND OBJ POOLING WITH GENERICS
public class GenericFactory<T> : MonoBehaviour where T : Component
{
public int poolSize;
public T[] prefabs;
protected Dictionary<string, Helper.GenericObjectPool<T>> pools;
private void Start()
{
InitPools();
}
private void InitPools()
{
pools = new Dictionary<string, Helper.GenericObjectPool<T>>();
foreach (T go in prefabs)
pools.Add(go.gameObject.name, new Helper.GenericObjectPool<T>(go, poolSize));
}
public T CreateObject(string name, Vector3 position)
{
return CreateObject(name, position, Quaternion.identity);
}
public T CreateObject(string objectName, Vector3 position, Quaternion rotation, bool setActive = true)
{
if (!pools.ContainsKey(objectName))
{
return null;
}
T instance = pools[objectName].GetObject();
instance.transform.position = position;
instance.transform.rotation = rotation;
instance.gameObject.SetActive(setActive);
return instance;
}
public bool ReturnObject(T instance)
{
if (!pools.ContainsKey(instance.gameObject.name))
{
return false;
}
pools[instance.gameObject.name].ReturnObject(instance);
return true;
}
}
#endregion
#region Utility
public static T RandomArrayValue<T>(List<T> array)
{
if (array.Count <= 0)
return default;
return array[UnityEngine.Random.Range(0, array.Count)];
}
public static T RandomArrayValue<T>(T[] array)
{
if (array.Length <= 0)
return default;
return array[UnityEngine.Random.Range(0, array.Length)];
}
public static void Shuffle<T>(IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = UnityEngine.Random.Range(0, n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
public static IEnumerator WaitCoroutine(float _time)
{
yield return new WaitForSeconds(_time);
}
/// <summary>
/// Destroy all child objects of this transform (Unintentionally evil sounding).
/// Use it like so:
/// <code>
/// transform.DestroyChildren();
/// </code>
/// </summary>
public static void DestroyChildren(this Transform t)
{
foreach (Transform child in t) Object.Destroy(child.gameObject);
}
#endregion
}
/// <summary>
/// A static instance is similar to a singleton, but instead of destroying any new
/// instances, it overrides the current instance. This is handy for resetting the state
/// and saves you doing it manually
/// </summary>
public abstract class StaticInstance<T> : MonoBehaviour where T : MonoBehaviour
{
public static T Instance { get; protected set; }
protected virtual void Awake()
{
Instance = this as T;
}
protected virtual void OnApplicationQuit()
{
Instance = null;
Destroy(gameObject);
}
}
/// <summary>
/// This transforms the static instance into a basic singleton. This will destroy any new
/// versions created, leaving the original instance intact
/// </summary>
public abstract class Singleton<T> : StaticInstance<T> where T : MonoBehaviour
{
protected override void Awake()
{
if (Instance != null)
{
Destroy(gameObject);
return;
}
base.Awake();
}
}
/// <summary>
/// Finally we have a persistent version of the singleton. This will survive through scene
/// loads. Perfect for system classes which require stateful, persistent data. Or audio sources
/// where music plays through loading screens, etc
/// </summary>
public abstract class PersistentSingleton<T> : Singleton<T> where T : MonoBehaviour
{
protected override void Awake()
{
if (Instance != null)
{
Destroy(gameObject);
return;
}
else
{
Instance = this as T;
DontDestroyOnLoad(gameObject);
}
}
}