com.wallstop-studios.unity-helpers 2.0.0-rc05 → 2.0.0-rc07
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Runtime/Core/DataStructure/TimedCache.cs +4 -3
- package/Runtime/Core/Extension/IListExtensions.cs +2 -2
- package/Runtime/Core/Extension/RandomExtensions.cs +23 -4
- package/Runtime/Core/Extension/UnityExtensions.cs +278 -90
- package/Runtime/Core/Helper/ArrayConverter.cs +39 -0
- package/Runtime/Core/Helper/ArrayConverter.cs.meta +3 -0
- package/Runtime/Core/Helper/Helpers.cs +133 -563
- package/Runtime/Core/Helper/Partials/LogHelpers.cs +13 -0
- package/Runtime/Core/Helper/Partials/LogHelpers.cs.meta +3 -0
- package/Runtime/Core/Helper/Partials/MathHelpers.cs +30 -0
- package/Runtime/Core/Helper/Partials/MathHelpers.cs.meta +3 -0
- package/Runtime/Core/Helper/Partials/ObjectHelpers.cs +388 -0
- package/Runtime/Core/Helper/Partials/ObjectHelpers.cs.meta +3 -0
- package/Runtime/Core/Helper/Partials/TransformHelpers.cs +167 -0
- package/Runtime/Core/Helper/Partials/TransformHelpers.cs.meta +3 -0
- package/Runtime/Core/Helper/Partials.meta +3 -0
- package/Runtime/Core/Helper/WallMath.cs +85 -22
- package/Runtime/Core/Random/AbstractRandom.cs +208 -162
- package/Runtime/Core/Random/DotNetRandom.cs +3 -5
- package/Runtime/Core/Random/PRNG.cs +7 -0
- package/Runtime/Core/Random/PRNG.cs.meta +3 -0
- package/Runtime/Core/Random/PcgRandom.cs +4 -6
- package/Runtime/Core/Random/RandomState.cs +31 -3
- package/Runtime/Core/Random/SquirrelRandom.cs +12 -15
- package/Runtime/Core/Random/SystemRandom.cs +92 -46
- package/Runtime/Core/Random/ThreadLocalRandom.cs +2 -1
- package/Runtime/Core/Random/UnityRandom.cs +2 -4
- package/Runtime/Core/Random/WyRandom.cs +2 -4
- package/Runtime/Core/Random/XorShiftRandom.cs +3 -5
- package/Runtime/Core/Serialization/Serializer.cs +36 -14
- package/Runtime/Utils/CircleLineRenderer.cs +17 -5
- package/Tests/Runtime/DataStructures/SpatialTreeTests.cs +34 -10
- package/Tests/Runtime/Helper/ArrayConverterTests.cs +19 -0
- package/Tests/Runtime/Helper/ArrayConverterTests.cs.meta +3 -0
- package/Tests/Runtime/Helper/ObjectHelperTests.cs +402 -0
- package/Tests/Runtime/Helper/ObjectHelperTests.cs.meta +3 -0
- package/Tests/Runtime/Helper/WallMathTests.cs +221 -0
- package/Tests/Runtime/Helper/WallMathTests.cs.meta +3 -0
- package/Tests/Runtime/Helper.meta +3 -0
- package/Tests/Runtime/Performance/RandomPerformanceTests.cs +58 -3
- package/Tests/Runtime/Performance/SpatialTreePerformanceTest.cs +47 -34
- package/Tests/Runtime/Random/RandomTestBase.cs +284 -9
- package/Tests/Runtime/Random/SquirrelRandomTests.cs +5 -0
- package/Tests/Runtime/Serialization/JsonSerializationTest.cs +24 -11
- package/Tests/Runtime/Utils/SpriteRendererMetadataTests.cs +21 -17
- package/package.json +1 -1
|
@@ -1,543 +1,37 @@
|
|
|
1
1
|
namespace UnityHelpers.Core.Helper
|
|
2
2
|
{
|
|
3
|
-
using Extension;
|
|
4
3
|
using System;
|
|
5
4
|
using System.Collections;
|
|
6
5
|
using System.Collections.Generic;
|
|
7
6
|
using System.Linq;
|
|
8
7
|
using System.Reflection;
|
|
9
8
|
using DataStructure.Adapters;
|
|
9
|
+
using Extension;
|
|
10
10
|
using Random;
|
|
11
|
-
#if UNITY_EDITOR
|
|
12
|
-
using UnityEditor;
|
|
13
|
-
using UnityEditor.SceneManagement;
|
|
14
|
-
#endif
|
|
15
11
|
using UnityEngine;
|
|
16
12
|
using UnityEngine.SceneManagement;
|
|
17
13
|
using Utils;
|
|
18
14
|
using Object = UnityEngine.Object;
|
|
15
|
+
#if UNITY_EDITOR
|
|
16
|
+
using UnityEditor;
|
|
17
|
+
using UnityEditor.SceneManagement;
|
|
18
|
+
#endif
|
|
19
19
|
|
|
20
|
-
public static class Helpers
|
|
20
|
+
public static partial class Helpers
|
|
21
21
|
{
|
|
22
22
|
private static readonly WaitForEndOfFrame WaitForEndOfFrame = new();
|
|
23
23
|
private static readonly Dictionary<Type, MethodInfo> AwakeMethodsByType = new();
|
|
24
24
|
private static readonly Object LogObject = new();
|
|
25
25
|
private static readonly Dictionary<string, Object> ObjectsByTag = new();
|
|
26
26
|
|
|
27
|
-
public static T Find<T>(this Object component, string tag, bool log = true) where T : Object
|
|
28
|
-
{
|
|
29
|
-
if (ObjectsByTag.TryGetValue(tag, out Object value))
|
|
30
|
-
{
|
|
31
|
-
if (value != null && value is T typed)
|
|
32
|
-
{
|
|
33
|
-
return typed;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
_ = ObjectsByTag.Remove(tag);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
GameObject gameObject = GameObject.FindGameObjectWithTag(tag);
|
|
40
|
-
if (gameObject == null)
|
|
41
|
-
{
|
|
42
|
-
if (log)
|
|
43
|
-
{
|
|
44
|
-
component.LogWarn("Could not find {0}.", tag);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return default;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (gameObject.TryGetComponent(out T instance))
|
|
51
|
-
{
|
|
52
|
-
ObjectsByTag[tag] = instance;
|
|
53
|
-
return instance;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (log)
|
|
57
|
-
{
|
|
58
|
-
component.LogWarn(
|
|
59
|
-
"Failed to find {0} on {1} (name: {2}), id [{3}].", typeof(T).Name, tag, gameObject.name,
|
|
60
|
-
gameObject.GetInstanceID());
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return default;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
public static T Find<T>(string tag, bool log = true) where T : MonoBehaviour
|
|
67
|
-
{
|
|
68
|
-
if (ObjectsByTag.TryGetValue(tag, out Object value))
|
|
69
|
-
{
|
|
70
|
-
if (value != null && value is T typed)
|
|
71
|
-
{
|
|
72
|
-
return typed;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
_ = ObjectsByTag.Remove(tag);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
GameObject gameObject = GameObject.FindGameObjectWithTag(tag);
|
|
79
|
-
if (gameObject == null)
|
|
80
|
-
{
|
|
81
|
-
if (log)
|
|
82
|
-
{
|
|
83
|
-
LogObject.Log($"Could not find {tag}.");
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return default;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (gameObject.TryGetComponent(out T instance))
|
|
90
|
-
{
|
|
91
|
-
ObjectsByTag[tag] = instance;
|
|
92
|
-
return instance;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (log)
|
|
96
|
-
{
|
|
97
|
-
LogObject.Log($"Failed to find {typeof(T).Name} on {tag}");
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return default;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
public static void SetInstance<T>(string tag, T instance) where T : MonoBehaviour
|
|
104
|
-
{
|
|
105
|
-
ObjectsByTag[tag] = instance;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
public static void ClearInstance<T>(string tag, T instance) where T : MonoBehaviour
|
|
109
|
-
{
|
|
110
|
-
if (ObjectsByTag.TryGetValue(tag, out Object existing) && existing == instance)
|
|
111
|
-
{
|
|
112
|
-
_ = ObjectsByTag.Remove(tag);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
public static bool HasComponent<T>(this Object unityObject) where T : Object
|
|
117
|
-
{
|
|
118
|
-
return unityObject switch
|
|
119
|
-
{
|
|
120
|
-
GameObject go => go.HasComponent<T>(),
|
|
121
|
-
Component component => component.HasComponent<T>(),
|
|
122
|
-
_ => false
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
public static bool HasComponent<T>(this Component component) where T : Object
|
|
127
|
-
{
|
|
128
|
-
return component.TryGetComponent<T>(out _);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
public static bool HasComponent<T>(this GameObject gameObject) where T : Object
|
|
132
|
-
{
|
|
133
|
-
return gameObject.TryGetComponent<T>(out _);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
public static bool HasComponent(this GameObject gameObject, Type type)
|
|
137
|
-
{
|
|
138
|
-
return gameObject.TryGetComponent(type, out _);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
public static void LogNotAssigned(this Object component, string name)
|
|
142
|
-
{
|
|
143
|
-
component.LogWarn("{0} not found.", name);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
public static IEnumerable<GameObject> IterateOverChildGameObjects(this GameObject gameObject)
|
|
147
|
-
{
|
|
148
|
-
for (int i = 0; i < gameObject.transform.childCount; i++)
|
|
149
|
-
{
|
|
150
|
-
yield return gameObject.transform.GetChild(i).gameObject;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
public static IEnumerable<GameObject> IterateOverChildGameObjectsRecursively(this GameObject gameObject)
|
|
155
|
-
{
|
|
156
|
-
for (int i = 0; i < gameObject.transform.childCount; i++)
|
|
157
|
-
{
|
|
158
|
-
GameObject child = gameObject.transform.GetChild(i).gameObject;
|
|
159
|
-
yield return child;
|
|
160
|
-
foreach (GameObject go in child.IterateOverChildGameObjectsRecursively())
|
|
161
|
-
{
|
|
162
|
-
yield return go;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
public static IEnumerable<GameObject> IterateOverChildGameObjectsRecursivelyIncludingSelf(
|
|
168
|
-
this GameObject gameObject)
|
|
169
|
-
{
|
|
170
|
-
yield return gameObject;
|
|
171
|
-
|
|
172
|
-
for (int i = 0; i < gameObject.transform.childCount; ++i)
|
|
173
|
-
{
|
|
174
|
-
GameObject child = gameObject.transform.GetChild(i).gameObject;
|
|
175
|
-
foreach (GameObject c in child.IterateOverChildGameObjectsRecursivelyIncludingSelf())
|
|
176
|
-
{
|
|
177
|
-
yield return c;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
public static IEnumerable<GameObject> IterateOverParentGameObjects(this GameObject gameObject)
|
|
183
|
-
{
|
|
184
|
-
Transform currentTransform = gameObject.transform.parent;
|
|
185
|
-
while (currentTransform != null)
|
|
186
|
-
{
|
|
187
|
-
yield return currentTransform.gameObject;
|
|
188
|
-
currentTransform = currentTransform.parent;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
public static IEnumerable<GameObject> IterateOverParentGameObjectsRecursivelyIncludingSelf(
|
|
193
|
-
this GameObject gameObject)
|
|
194
|
-
{
|
|
195
|
-
yield return gameObject;
|
|
196
|
-
|
|
197
|
-
foreach (GameObject parent in IterateOverParentGameObjects(gameObject))
|
|
198
|
-
{
|
|
199
|
-
yield return parent;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
public static void EnableRecursively<T>(this Component component, bool enabled, Func<T, bool> exclude = null)
|
|
204
|
-
where T : Behaviour
|
|
205
|
-
{
|
|
206
|
-
if (component == null)
|
|
207
|
-
{
|
|
208
|
-
return;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
foreach (T behaviour in component.GetComponents<T>())
|
|
212
|
-
{
|
|
213
|
-
if (behaviour != null && (exclude?.Invoke(behaviour) ?? true))
|
|
214
|
-
{
|
|
215
|
-
behaviour.enabled = enabled;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
Transform transform = (component as Transform) ?? component.transform;
|
|
220
|
-
if (transform == null)
|
|
221
|
-
{
|
|
222
|
-
return;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
for (int i = 0; i < transform.childCount; ++i)
|
|
226
|
-
{
|
|
227
|
-
Transform child = transform.GetChild(i);
|
|
228
|
-
EnableRecursively<T>(child, enabled);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
public static void EnableRendererRecursively<T>(
|
|
233
|
-
this Component component, bool enabled,
|
|
234
|
-
Func<T, bool> exclude = null) where T : Renderer
|
|
235
|
-
{
|
|
236
|
-
if (component == null)
|
|
237
|
-
{
|
|
238
|
-
return;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
T behavior = component as T ?? component.GetComponent<T>();
|
|
242
|
-
if (behavior != null && (exclude?.Invoke(behavior) ?? true))
|
|
243
|
-
{
|
|
244
|
-
behavior.enabled = enabled;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
Transform transform = (component as Transform) ?? component.transform;
|
|
248
|
-
if (transform == null)
|
|
249
|
-
{
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
for (int i = 0; i < transform.childCount; ++i)
|
|
254
|
-
{
|
|
255
|
-
Transform child = transform.GetChild(i);
|
|
256
|
-
EnableRendererRecursively<T>(child, enabled);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
public static IEnumerable<T> IterateOverAllChildComponentsRecursively<T>(this Component component)
|
|
261
|
-
{
|
|
262
|
-
if (component == null)
|
|
263
|
-
{
|
|
264
|
-
yield break;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
foreach (T c in component.gameObject.GetComponents<T>())
|
|
268
|
-
{
|
|
269
|
-
yield return c;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
for (int i = 0; i < component.transform.childCount; ++i)
|
|
273
|
-
{
|
|
274
|
-
Transform child = component.transform.GetChild(i);
|
|
275
|
-
|
|
276
|
-
foreach (T c in child.IterateOverAllChildComponentsRecursively<T>())
|
|
277
|
-
{
|
|
278
|
-
yield return c;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
public static IEnumerable<Transform> IterateOverAllChildren(this Component component)
|
|
284
|
-
{
|
|
285
|
-
if (component == null)
|
|
286
|
-
{
|
|
287
|
-
yield break;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
for (int i = 0; i < component.transform.childCount; ++i)
|
|
291
|
-
{
|
|
292
|
-
yield return component.transform.GetChild(i);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
public static IEnumerable<Transform> IterateOverAllParents(this Component component)
|
|
297
|
-
{
|
|
298
|
-
if (component == null)
|
|
299
|
-
{
|
|
300
|
-
yield break;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
Transform transform = component.transform;
|
|
304
|
-
while (transform.parent != null)
|
|
305
|
-
{
|
|
306
|
-
yield return transform.parent;
|
|
307
|
-
transform = transform.parent;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
public static IEnumerable<Transform> IterateOverAllParentsIncludingSelf(this Component component)
|
|
312
|
-
{
|
|
313
|
-
if (component == null)
|
|
314
|
-
{
|
|
315
|
-
yield break;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
Transform transform = component.transform;
|
|
319
|
-
while (transform != null)
|
|
320
|
-
{
|
|
321
|
-
yield return transform;
|
|
322
|
-
transform = transform.parent;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
public static IEnumerable<Transform> IterateOverAllChildrenRecursively(this Component component)
|
|
327
|
-
{
|
|
328
|
-
if (component == null)
|
|
329
|
-
{
|
|
330
|
-
yield break;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
for (int i = 0; i < component.transform.childCount; ++i)
|
|
334
|
-
{
|
|
335
|
-
Transform childTransform = component.transform.GetChild(i);
|
|
336
|
-
yield return childTransform;
|
|
337
|
-
foreach (Transform childChildTransform in childTransform.IterateOverAllChildrenRecursively())
|
|
338
|
-
{
|
|
339
|
-
yield return childChildTransform;
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
public static bool IsLeft(Vector2 a, Vector2 b, Vector2 point)
|
|
345
|
-
{
|
|
346
|
-
// http://alienryderflex.com/point_left_of_ray/
|
|
347
|
-
|
|
348
|
-
//check which side of line AB the point P is on
|
|
349
|
-
if ((b.x - a.x) * (point.y - a.y) - (point.x - a.x) * (b.y - a.y) > 0)
|
|
350
|
-
{
|
|
351
|
-
return false;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
return true;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
public static void DestroyAllChildrenGameObjects(this GameObject gameObject)
|
|
358
|
-
{
|
|
359
|
-
if (Application.isEditor)
|
|
360
|
-
{
|
|
361
|
-
EditorDestroyAllChildrenGameObjects(gameObject);
|
|
362
|
-
}
|
|
363
|
-
else
|
|
364
|
-
{
|
|
365
|
-
PlayDestroyAllChildrenGameObjects(gameObject);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
public static void DestroyAllComponentsOfType<T>(this GameObject gameObject) where T : Component
|
|
370
|
-
{
|
|
371
|
-
foreach (T component in gameObject.GetComponents<T>())
|
|
372
|
-
{
|
|
373
|
-
SmartDestroy(component);
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
public static void SmartDestroy(this Object obj, float? afterTime = null)
|
|
378
|
-
{
|
|
379
|
-
if (Application.isEditor && !Application.isPlaying)
|
|
380
|
-
{
|
|
381
|
-
Object.DestroyImmediate(obj);
|
|
382
|
-
}
|
|
383
|
-
else
|
|
384
|
-
{
|
|
385
|
-
if (afterTime.HasValue)
|
|
386
|
-
{
|
|
387
|
-
Object.Destroy(obj, afterTime.Value);
|
|
388
|
-
}
|
|
389
|
-
else
|
|
390
|
-
{
|
|
391
|
-
Object.Destroy(obj);
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
public static void DestroyAllChildrenGameObjectsImmediatelyConditionally(
|
|
397
|
-
this GameObject gameObject,
|
|
398
|
-
Func<GameObject, bool> acceptancePredicate)
|
|
399
|
-
{
|
|
400
|
-
InternalDestroyAllChildrenGameObjects(
|
|
401
|
-
gameObject, toDestroy =>
|
|
402
|
-
{
|
|
403
|
-
if (!acceptancePredicate(toDestroy))
|
|
404
|
-
{
|
|
405
|
-
return;
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
Object.DestroyImmediate(toDestroy);
|
|
409
|
-
});
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
public static void DestroyAllChildGameObjectsConditionally(
|
|
413
|
-
this GameObject gameObject,
|
|
414
|
-
Func<GameObject, bool> acceptancePredicate)
|
|
415
|
-
{
|
|
416
|
-
InternalDestroyAllChildrenGameObjects(
|
|
417
|
-
gameObject, toDestroy =>
|
|
418
|
-
{
|
|
419
|
-
if (!acceptancePredicate(toDestroy))
|
|
420
|
-
{
|
|
421
|
-
return;
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
toDestroy.Destroy();
|
|
425
|
-
});
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
public static void DestroyAllChildrenGameObjectsImmediately(this GameObject gameObject) =>
|
|
429
|
-
InternalDestroyAllChildrenGameObjects(gameObject, Object.DestroyImmediate);
|
|
430
|
-
|
|
431
|
-
public static void PlayDestroyAllChildrenGameObjects(this GameObject gameObject) =>
|
|
432
|
-
InternalDestroyAllChildrenGameObjects(gameObject, go => go.Destroy());
|
|
433
|
-
|
|
434
|
-
public static void EditorDestroyAllChildrenGameObjects(this GameObject gameObject) =>
|
|
435
|
-
InternalDestroyAllChildrenGameObjects(gameObject, go => go.Destroy());
|
|
436
|
-
|
|
437
|
-
private static void InternalDestroyAllChildrenGameObjects(
|
|
438
|
-
this GameObject gameObject,
|
|
439
|
-
Action<GameObject> destroyFunction)
|
|
440
|
-
{
|
|
441
|
-
for (int i = gameObject.transform.childCount - 1; 0 <= i; --i)
|
|
442
|
-
{
|
|
443
|
-
destroyFunction(gameObject.transform.GetChild(i).gameObject);
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
public static bool IsPrefab(this GameObject gameObject)
|
|
448
|
-
{
|
|
449
|
-
Scene scene = gameObject.scene;
|
|
450
|
-
#if UNITY_EDITOR
|
|
451
|
-
if (scene.rootCount == 1 && string.Equals(scene.name, gameObject.name))
|
|
452
|
-
{
|
|
453
|
-
return true;
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
return PrefabUtility.GetPrefabAssetType(gameObject) switch
|
|
457
|
-
{
|
|
458
|
-
PrefabAssetType.NotAPrefab => false,
|
|
459
|
-
PrefabAssetType.MissingAsset => scene.rootCount == 0,
|
|
460
|
-
_ => true,
|
|
461
|
-
};
|
|
462
|
-
#else
|
|
463
|
-
return scene.rootCount == 0;
|
|
464
|
-
#endif
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
public static bool IsPrefab(this Component component)
|
|
468
|
-
{
|
|
469
|
-
return IsPrefab(component.gameObject);
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
public static Vector2 RadianToVector2(float radian)
|
|
473
|
-
{
|
|
474
|
-
return new Vector2(Mathf.Cos(radian), Mathf.Sin(radian));
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
public static Vector2 DegreeToVector2(float degree)
|
|
478
|
-
{
|
|
479
|
-
return RadianToVector2(degree * Mathf.Deg2Rad);
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
public static T GetOrAddComponent<T>(this GameObject unityObject) where T : Component
|
|
483
|
-
{
|
|
484
|
-
if (!unityObject.TryGetComponent(out T instance))
|
|
485
|
-
{
|
|
486
|
-
instance = unityObject.AddComponent<T>();
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
return instance;
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
public static Component GetOrAddComponent(this GameObject unityObject, Type componentType)
|
|
493
|
-
{
|
|
494
|
-
if (!unityObject.TryGetComponent(componentType, out Component instance))
|
|
495
|
-
{
|
|
496
|
-
instance = unityObject.AddComponent(componentType);
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
return instance;
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
public static void ModifyAndSavePrefab(GameObject prefab, Action<GameObject> modifyAction)
|
|
503
|
-
{
|
|
504
|
-
if (prefab == null)
|
|
505
|
-
{
|
|
506
|
-
return;
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
#if UNITY_EDITOR
|
|
510
|
-
if (PrefabUtility.IsPartOfPrefabAsset(prefab))
|
|
511
|
-
{
|
|
512
|
-
string assetPath = AssetDatabase.GetAssetPath(prefab);
|
|
513
|
-
GameObject content = PrefabUtility.LoadPrefabContents(assetPath);
|
|
514
|
-
|
|
515
|
-
if (content == null)
|
|
516
|
-
{
|
|
517
|
-
Debug.LogError($"Unable to load {prefab} as a prefab");
|
|
518
|
-
return;
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
modifyAction(content);
|
|
522
|
-
_ = PrefabUtility.SaveAsPrefabAsset(content, assetPath);
|
|
523
|
-
PrefabUtility.UnloadPrefabContents(content);
|
|
524
|
-
}
|
|
525
|
-
else
|
|
526
|
-
{
|
|
527
|
-
modifyAction(prefab);
|
|
528
|
-
PrefabStage stage = PrefabStageUtility.GetPrefabStage(prefab);
|
|
529
|
-
if (stage)
|
|
530
|
-
{
|
|
531
|
-
_ = EditorSceneManager.MarkSceneDirty(stage.scene);
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
#endif
|
|
535
|
-
}
|
|
536
|
-
|
|
537
27
|
// https://gamedevelopment.tutsplus.com/tutorials/unity-solution-for-hitting-moving-targets--cms-29633
|
|
538
28
|
public static Vector2 PredictCurrentTarget(
|
|
539
|
-
this GameObject currentTarget,
|
|
540
|
-
Vector2
|
|
29
|
+
this GameObject currentTarget,
|
|
30
|
+
Vector2 launchLocation,
|
|
31
|
+
float projectileSpeed,
|
|
32
|
+
bool predictiveFiring,
|
|
33
|
+
Vector2 targetVelocity
|
|
34
|
+
)
|
|
541
35
|
{
|
|
542
36
|
Vector2 target = currentTarget.transform.position;
|
|
543
37
|
|
|
@@ -551,17 +45,21 @@
|
|
|
551
45
|
return target;
|
|
552
46
|
}
|
|
553
47
|
|
|
554
|
-
float a =
|
|
555
|
-
|
|
48
|
+
float a =
|
|
49
|
+
(targetVelocity.x * targetVelocity.x)
|
|
50
|
+
+ (targetVelocity.y * targetVelocity.y)
|
|
51
|
+
- (projectileSpeed * projectileSpeed);
|
|
556
52
|
|
|
557
|
-
float b =
|
|
558
|
-
|
|
53
|
+
float b =
|
|
54
|
+
2
|
|
55
|
+
* (
|
|
56
|
+
targetVelocity.x * (target.x - launchLocation.x)
|
|
57
|
+
+ targetVelocity.y * (target.y - launchLocation.y)
|
|
58
|
+
);
|
|
559
59
|
|
|
560
60
|
float c =
|
|
561
|
-
((target.x - launchLocation.x) *
|
|
562
|
-
|
|
563
|
-
((target.y - launchLocation.y) *
|
|
564
|
-
(target.y - launchLocation.y));
|
|
61
|
+
((target.x - launchLocation.x) * (target.x - launchLocation.x))
|
|
62
|
+
+ ((target.y - launchLocation.y) * (target.y - launchLocation.y));
|
|
565
63
|
|
|
566
64
|
float disc = b * b - (4 * a * c);
|
|
567
65
|
if (disc < 0)
|
|
@@ -615,7 +113,7 @@
|
|
|
615
113
|
{
|
|
616
114
|
GameObject go => go,
|
|
617
115
|
Component c => c.gameObject,
|
|
618
|
-
_ => null
|
|
116
|
+
_ => null,
|
|
619
117
|
};
|
|
620
118
|
}
|
|
621
119
|
|
|
@@ -632,12 +130,18 @@
|
|
|
632
130
|
|
|
633
131
|
public static GameObject FindChildGameObjectWithTag(this GameObject gameObject, string tag)
|
|
634
132
|
{
|
|
635
|
-
return gameObject
|
|
133
|
+
return gameObject
|
|
134
|
+
.IterateOverChildGameObjectsRecursivelyIncludingSelf()
|
|
636
135
|
.FirstOrDefault(child => child.CompareTag(tag));
|
|
637
136
|
}
|
|
638
137
|
|
|
639
138
|
public static bool HasLineOfSight(
|
|
640
|
-
Vector2 initialLocation,
|
|
139
|
+
Vector2 initialLocation,
|
|
140
|
+
Vector2 direction,
|
|
141
|
+
Transform transform,
|
|
142
|
+
float totalDistance,
|
|
143
|
+
float delta
|
|
144
|
+
)
|
|
641
145
|
{
|
|
642
146
|
int hits = Physics2D.RaycastNonAlloc(initialLocation, direction, Buffers.RaycastHits);
|
|
643
147
|
for (int i = 0; i < hits; ++i)
|
|
@@ -659,13 +163,24 @@
|
|
|
659
163
|
}
|
|
660
164
|
|
|
661
165
|
public static Coroutine StartFunctionAsCoroutine(
|
|
662
|
-
this MonoBehaviour monoBehaviour,
|
|
663
|
-
|
|
166
|
+
this MonoBehaviour monoBehaviour,
|
|
167
|
+
Action action,
|
|
168
|
+
float updateRate,
|
|
169
|
+
bool useJitter = false,
|
|
170
|
+
bool waitBefore = false
|
|
171
|
+
)
|
|
664
172
|
{
|
|
665
|
-
return monoBehaviour.StartCoroutine(
|
|
173
|
+
return monoBehaviour.StartCoroutine(
|
|
174
|
+
FunctionAsCoroutine(action, updateRate, useJitter, waitBefore)
|
|
175
|
+
);
|
|
666
176
|
}
|
|
667
177
|
|
|
668
|
-
private static IEnumerator FunctionAsCoroutine(
|
|
178
|
+
private static IEnumerator FunctionAsCoroutine(
|
|
179
|
+
Action action,
|
|
180
|
+
float updateRate,
|
|
181
|
+
bool useJitter,
|
|
182
|
+
bool waitBefore
|
|
183
|
+
)
|
|
669
184
|
{
|
|
670
185
|
bool usedJitter = false;
|
|
671
186
|
while (true)
|
|
@@ -675,7 +190,7 @@
|
|
|
675
190
|
{
|
|
676
191
|
if (useJitter && !usedJitter)
|
|
677
192
|
{
|
|
678
|
-
float delay =
|
|
193
|
+
float delay = PRNG.Instance.NextFloat(updateRate);
|
|
679
194
|
startTime = Time.time;
|
|
680
195
|
while (!HasEnoughTimePassed(startTime, delay))
|
|
681
196
|
{
|
|
@@ -698,7 +213,7 @@
|
|
|
698
213
|
{
|
|
699
214
|
if (useJitter && !usedJitter)
|
|
700
215
|
{
|
|
701
|
-
float delay =
|
|
216
|
+
float delay = PRNG.Instance.NextFloat(updateRate);
|
|
702
217
|
startTime = Time.time;
|
|
703
218
|
while (!HasEnoughTimePassed(startTime, delay))
|
|
704
219
|
{
|
|
@@ -717,22 +232,37 @@
|
|
|
717
232
|
}
|
|
718
233
|
}
|
|
719
234
|
|
|
720
|
-
public static Coroutine ExecuteFunctionAfterDelay(
|
|
235
|
+
public static Coroutine ExecuteFunctionAfterDelay(
|
|
236
|
+
this MonoBehaviour monoBehaviour,
|
|
237
|
+
Action action,
|
|
238
|
+
float delay
|
|
239
|
+
)
|
|
721
240
|
{
|
|
722
241
|
return monoBehaviour.StartCoroutine(FunctionDelayAsCoroutine(action, delay));
|
|
723
242
|
}
|
|
724
243
|
|
|
725
|
-
public static Coroutine ExecuteFunctionNextFrame(
|
|
244
|
+
public static Coroutine ExecuteFunctionNextFrame(
|
|
245
|
+
this MonoBehaviour monoBehaviour,
|
|
246
|
+
Action action
|
|
247
|
+
)
|
|
726
248
|
{
|
|
727
249
|
return monoBehaviour.ExecuteFunctionAfterDelay(action, 0f);
|
|
728
250
|
}
|
|
729
251
|
|
|
730
|
-
public static Coroutine ExecuteFunctionAfterFrame(
|
|
252
|
+
public static Coroutine ExecuteFunctionAfterFrame(
|
|
253
|
+
this MonoBehaviour monoBehaviour,
|
|
254
|
+
Action action
|
|
255
|
+
)
|
|
731
256
|
{
|
|
732
257
|
return monoBehaviour.StartCoroutine(FunctionAfterFrame(action));
|
|
733
258
|
}
|
|
734
259
|
|
|
735
|
-
public static IEnumerator ExecuteOverTime(
|
|
260
|
+
public static IEnumerator ExecuteOverTime(
|
|
261
|
+
Action action,
|
|
262
|
+
int totalCount,
|
|
263
|
+
float duration,
|
|
264
|
+
bool delay = true
|
|
265
|
+
)
|
|
736
266
|
{
|
|
737
267
|
if (action == null)
|
|
738
268
|
{
|
|
@@ -750,7 +280,10 @@
|
|
|
750
280
|
{
|
|
751
281
|
float percent = (Time.time - startTime) / duration;
|
|
752
282
|
// optional delay execution from happening on 0, 1, 2, ... n-1 to 1, 2, ... n
|
|
753
|
-
if (
|
|
283
|
+
if (
|
|
284
|
+
totalExecuted < totalCount
|
|
285
|
+
&& ((totalExecuted + (delay ? 1f : 0f)) / totalCount) <= percent
|
|
286
|
+
)
|
|
754
287
|
{
|
|
755
288
|
action();
|
|
756
289
|
++totalExecuted;
|
|
@@ -759,7 +292,7 @@
|
|
|
759
292
|
yield return null;
|
|
760
293
|
}
|
|
761
294
|
|
|
762
|
-
for (; totalExecuted < totalCount;)
|
|
295
|
+
for (; totalExecuted < totalCount; )
|
|
763
296
|
{
|
|
764
297
|
action();
|
|
765
298
|
++totalExecuted;
|
|
@@ -828,7 +361,11 @@
|
|
|
828
361
|
|
|
829
362
|
public static Vector3Int AsVector3Int(this Vector3 vector)
|
|
830
363
|
{
|
|
831
|
-
return new Vector3Int(
|
|
364
|
+
return new Vector3Int(
|
|
365
|
+
(int)Math.Round(vector.x),
|
|
366
|
+
(int)Math.Round(vector.y),
|
|
367
|
+
(int)Math.Round(vector.z)
|
|
368
|
+
);
|
|
832
369
|
}
|
|
833
370
|
|
|
834
371
|
public static Vector3 AsVector3(this (uint x, uint y, uint z) vector)
|
|
@@ -856,7 +393,8 @@
|
|
|
856
393
|
return new Vector3Int(vector.x, vector.y);
|
|
857
394
|
}
|
|
858
395
|
|
|
859
|
-
public static T CopyTo<T>(this T original, GameObject destination)
|
|
396
|
+
public static T CopyTo<T>(this T original, GameObject destination)
|
|
397
|
+
where T : Component
|
|
860
398
|
{
|
|
861
399
|
Type type = original.GetType();
|
|
862
400
|
T copied = destination.GetComponent(type) as T;
|
|
@@ -877,8 +415,10 @@
|
|
|
877
415
|
}
|
|
878
416
|
}
|
|
879
417
|
|
|
880
|
-
foreach (
|
|
881
|
-
|
|
418
|
+
foreach (
|
|
419
|
+
FieldInfo field in type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
|
|
420
|
+
.Where(field => Attribute.IsDefined(field, typeof(SerializeField)))
|
|
421
|
+
)
|
|
882
422
|
{
|
|
883
423
|
try
|
|
884
424
|
{
|
|
@@ -915,30 +455,46 @@
|
|
|
915
455
|
return new Rect(bounds.x, bounds.y, bounds.size.x, bounds.size.y);
|
|
916
456
|
}
|
|
917
457
|
|
|
918
|
-
public static
|
|
458
|
+
public static Vector2 GetRandomPointInCircle(
|
|
459
|
+
Vector2 center,
|
|
460
|
+
float radius,
|
|
461
|
+
IRandom random = null
|
|
462
|
+
)
|
|
919
463
|
{
|
|
920
|
-
random ??=
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
return new
|
|
464
|
+
random ??= PRNG.Instance;
|
|
465
|
+
double r = radius * Math.Sqrt(random.NextDouble());
|
|
466
|
+
double theta = random.NextDouble() * 2 * Math.PI;
|
|
467
|
+
return new Vector2(
|
|
468
|
+
center.x + (float)(r * Math.Cos(theta)),
|
|
469
|
+
center.y + (float)(r * Math.Sin(theta))
|
|
470
|
+
);
|
|
924
471
|
}
|
|
925
472
|
|
|
926
|
-
public static GameObject GetPlayerObjectInChildHierarchy(
|
|
473
|
+
public static GameObject GetPlayerObjectInChildHierarchy(
|
|
474
|
+
this GameObject gameObject,
|
|
475
|
+
string playerTag = "Player"
|
|
476
|
+
)
|
|
927
477
|
{
|
|
928
|
-
return gameObject.GetTagObjectInChildHierarchy(
|
|
478
|
+
return gameObject.GetTagObjectInChildHierarchy(playerTag);
|
|
929
479
|
}
|
|
930
480
|
|
|
931
|
-
public static GameObject GetTagObjectInChildHierarchy(
|
|
481
|
+
public static GameObject GetTagObjectInChildHierarchy(
|
|
482
|
+
this GameObject gameObject,
|
|
483
|
+
string tag
|
|
484
|
+
)
|
|
932
485
|
{
|
|
933
|
-
return gameObject
|
|
486
|
+
return gameObject
|
|
487
|
+
.IterateOverChildGameObjectsRecursivelyIncludingSelf()
|
|
934
488
|
.FirstOrDefault(go => go.CompareTag(tag));
|
|
935
489
|
}
|
|
936
490
|
|
|
937
491
|
//https://answers.unity.com/questions/722748/refreshing-the-polygon-collider-2d-upon-sprite-cha.html
|
|
938
492
|
public static void UpdateShapeToSprite(this Component component)
|
|
939
493
|
{
|
|
940
|
-
if (
|
|
941
|
-
component.TryGetComponent(out
|
|
494
|
+
if (
|
|
495
|
+
!component.TryGetComponent(out SpriteRenderer spriteRenderer)
|
|
496
|
+
|| component.TryGetComponent(out PolygonCollider2D collider)
|
|
497
|
+
)
|
|
942
498
|
{
|
|
943
499
|
return;
|
|
944
500
|
}
|
|
@@ -973,7 +529,9 @@
|
|
|
973
529
|
return new Vector3Int(x, y, z);
|
|
974
530
|
}
|
|
975
531
|
|
|
976
|
-
public static GameObject TryGetClosestParentWithComponentIncludingSelf<T>(
|
|
532
|
+
public static GameObject TryGetClosestParentWithComponentIncludingSelf<T>(
|
|
533
|
+
this GameObject current
|
|
534
|
+
)
|
|
977
535
|
where T : Component
|
|
978
536
|
{
|
|
979
537
|
while (current != null)
|
|
@@ -991,7 +549,9 @@
|
|
|
991
549
|
}
|
|
992
550
|
|
|
993
551
|
#if UNITY_EDITOR
|
|
994
|
-
public static IEnumerable<GameObject> EnumeratePrefabs(
|
|
552
|
+
public static IEnumerable<GameObject> EnumeratePrefabs(
|
|
553
|
+
IEnumerable<string> assetPaths = null
|
|
554
|
+
)
|
|
995
555
|
{
|
|
996
556
|
assetPaths ??= new[] { "Assets/Prefabs", "Assets/Resources" };
|
|
997
557
|
|
|
@@ -1011,7 +571,9 @@
|
|
|
1011
571
|
{
|
|
1012
572
|
assetPaths ??= new[] { "Assets/Prefabs", "Assets/Resources", "Assets/TileMaps" };
|
|
1013
573
|
|
|
1014
|
-
foreach (
|
|
574
|
+
foreach (
|
|
575
|
+
string assetGuid in AssetDatabase.FindAssets("t:" + typeof(T).Name, assetPaths)
|
|
576
|
+
)
|
|
1015
577
|
{
|
|
1016
578
|
string path = AssetDatabase.GUIDToAssetPath(assetGuid);
|
|
1017
579
|
T so = AssetDatabase.LoadAssetAtPath<T>(path);
|
|
@@ -1093,8 +655,12 @@
|
|
|
1093
655
|
{
|
|
1094
656
|
MethodInfo awakeInfo = AwakeMethodsByType.GetOrAdd(
|
|
1095
657
|
script.GetType(),
|
|
1096
|
-
type =>
|
|
1097
|
-
|
|
658
|
+
type =>
|
|
659
|
+
type.GetMethod(
|
|
660
|
+
"Awake",
|
|
661
|
+
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
|
|
662
|
+
)
|
|
663
|
+
);
|
|
1098
664
|
if (awakeInfo != null)
|
|
1099
665
|
{
|
|
1100
666
|
_ = awakeInfo.Invoke(script, null);
|
|
@@ -1102,7 +668,11 @@
|
|
|
1102
668
|
}
|
|
1103
669
|
}
|
|
1104
670
|
|
|
1105
|
-
public static Vector2 GetAngleWithSpeed(
|
|
671
|
+
public static Vector2 GetAngleWithSpeed(
|
|
672
|
+
Vector2 targetDirection,
|
|
673
|
+
Vector2 currentDirection,
|
|
674
|
+
float rotationSpeed
|
|
675
|
+
)
|
|
1106
676
|
{
|
|
1107
677
|
if (targetDirection == Vector2.zero)
|
|
1108
678
|
{
|
|
@@ -1149,4 +719,4 @@
|
|
|
1149
719
|
}
|
|
1150
720
|
}
|
|
1151
721
|
}
|
|
1152
|
-
}
|
|
722
|
+
}
|