com.wallstop-studios.unity-helpers 2.0.0-rc05 → 2.0.0-rc06
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 +209 -84
- package/Runtime/Core/Helper/WallMath.cs +85 -22
- package/Runtime/Core/Random/AbstractRandom.cs +34 -20
- 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 +3 -5
- package/Runtime/Core/Random/SystemRandom.cs +20 -11
- 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/WallMathTests.cs +221 -0
- package/Tests/Runtime/Helper/WallMathTests.cs.meta +3 -0
- package/Tests/Runtime/Helper.meta +3 -0
- package/Tests/Runtime/Performance/SpatialTreePerformanceTest.cs +47 -34
- package/Tests/Runtime/Random/RandomTestBase.cs +23 -3
- package/Tests/Runtime/Serialization/JsonSerializationTest.cs +24 -11
- package/Tests/Runtime/Utils/SpriteRendererMetadataTests.cs +21 -17
- package/package.json +1 -1
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
namespace UnityHelpers.Core.Helper
|
|
2
|
+
{
|
|
3
|
+
using System;
|
|
4
|
+
|
|
5
|
+
public static class ArrayConverter
|
|
6
|
+
{
|
|
7
|
+
public static byte[] IntArrayToByteArrayBlockCopy(int[] ints)
|
|
8
|
+
{
|
|
9
|
+
if (ints == null)
|
|
10
|
+
{
|
|
11
|
+
throw new ArgumentNullException(nameof(ints));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
byte[] bytes = new byte[ints.Length * sizeof(int)];
|
|
15
|
+
Buffer.BlockCopy(ints, 0, bytes, 0, bytes.Length);
|
|
16
|
+
return bytes;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public static int[] ByteArrayToIntArrayBlockCopy(byte[] bytes)
|
|
20
|
+
{
|
|
21
|
+
if (bytes == null)
|
|
22
|
+
{
|
|
23
|
+
throw new ArgumentNullException(nameof(bytes), "Byte array cannot be null.");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (bytes.Length % sizeof(int) != 0)
|
|
27
|
+
{
|
|
28
|
+
throw new ArgumentException(
|
|
29
|
+
$"Byte array length must be a multiple of {sizeof(int)}.",
|
|
30
|
+
nameof(bytes)
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
int[] ints = new int[bytes.Length / sizeof(int)];
|
|
35
|
+
Buffer.BlockCopy(bytes, 0, ints, 0, bytes.Length);
|
|
36
|
+
return ints;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -1,21 +1,21 @@
|
|
|
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
20
|
public static class Helpers
|
|
21
21
|
{
|
|
@@ -24,7 +24,8 @@
|
|
|
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)
|
|
27
|
+
public static T Find<T>(this Object component, string tag, bool log = true)
|
|
28
|
+
where T : Object
|
|
28
29
|
{
|
|
29
30
|
if (ObjectsByTag.TryGetValue(tag, out Object value))
|
|
30
31
|
{
|
|
@@ -56,14 +57,19 @@
|
|
|
56
57
|
if (log)
|
|
57
58
|
{
|
|
58
59
|
component.LogWarn(
|
|
59
|
-
"Failed to find {0} on {1} (name: {2}), id [{3}].",
|
|
60
|
-
|
|
60
|
+
"Failed to find {0} on {1} (name: {2}), id [{3}].",
|
|
61
|
+
typeof(T).Name,
|
|
62
|
+
tag,
|
|
63
|
+
gameObject.name,
|
|
64
|
+
gameObject.GetInstanceID()
|
|
65
|
+
);
|
|
61
66
|
}
|
|
62
67
|
|
|
63
68
|
return default;
|
|
64
69
|
}
|
|
65
70
|
|
|
66
|
-
public static T Find<T>(string tag, bool log = true)
|
|
71
|
+
public static T Find<T>(string tag, bool log = true)
|
|
72
|
+
where T : MonoBehaviour
|
|
67
73
|
{
|
|
68
74
|
if (ObjectsByTag.TryGetValue(tag, out Object value))
|
|
69
75
|
{
|
|
@@ -100,12 +106,14 @@
|
|
|
100
106
|
return default;
|
|
101
107
|
}
|
|
102
108
|
|
|
103
|
-
public static void SetInstance<T>(string tag, T instance)
|
|
109
|
+
public static void SetInstance<T>(string tag, T instance)
|
|
110
|
+
where T : MonoBehaviour
|
|
104
111
|
{
|
|
105
112
|
ObjectsByTag[tag] = instance;
|
|
106
113
|
}
|
|
107
114
|
|
|
108
|
-
public static void ClearInstance<T>(string tag, T instance)
|
|
115
|
+
public static void ClearInstance<T>(string tag, T instance)
|
|
116
|
+
where T : MonoBehaviour
|
|
109
117
|
{
|
|
110
118
|
if (ObjectsByTag.TryGetValue(tag, out Object existing) && existing == instance)
|
|
111
119
|
{
|
|
@@ -113,22 +121,25 @@
|
|
|
113
121
|
}
|
|
114
122
|
}
|
|
115
123
|
|
|
116
|
-
public static bool HasComponent<T>(this Object unityObject)
|
|
124
|
+
public static bool HasComponent<T>(this Object unityObject)
|
|
125
|
+
where T : Object
|
|
117
126
|
{
|
|
118
127
|
return unityObject switch
|
|
119
128
|
{
|
|
120
129
|
GameObject go => go.HasComponent<T>(),
|
|
121
130
|
Component component => component.HasComponent<T>(),
|
|
122
|
-
_ => false
|
|
131
|
+
_ => false,
|
|
123
132
|
};
|
|
124
133
|
}
|
|
125
134
|
|
|
126
|
-
public static bool HasComponent<T>(this Component component)
|
|
135
|
+
public static bool HasComponent<T>(this Component component)
|
|
136
|
+
where T : Object
|
|
127
137
|
{
|
|
128
138
|
return component.TryGetComponent<T>(out _);
|
|
129
139
|
}
|
|
130
140
|
|
|
131
|
-
public static bool HasComponent<T>(this GameObject gameObject)
|
|
141
|
+
public static bool HasComponent<T>(this GameObject gameObject)
|
|
142
|
+
where T : Object
|
|
132
143
|
{
|
|
133
144
|
return gameObject.TryGetComponent<T>(out _);
|
|
134
145
|
}
|
|
@@ -143,7 +154,9 @@
|
|
|
143
154
|
component.LogWarn("{0} not found.", name);
|
|
144
155
|
}
|
|
145
156
|
|
|
146
|
-
public static IEnumerable<GameObject> IterateOverChildGameObjects(
|
|
157
|
+
public static IEnumerable<GameObject> IterateOverChildGameObjects(
|
|
158
|
+
this GameObject gameObject
|
|
159
|
+
)
|
|
147
160
|
{
|
|
148
161
|
for (int i = 0; i < gameObject.transform.childCount; i++)
|
|
149
162
|
{
|
|
@@ -151,7 +164,9 @@
|
|
|
151
164
|
}
|
|
152
165
|
}
|
|
153
166
|
|
|
154
|
-
public static IEnumerable<GameObject> IterateOverChildGameObjectsRecursively(
|
|
167
|
+
public static IEnumerable<GameObject> IterateOverChildGameObjectsRecursively(
|
|
168
|
+
this GameObject gameObject
|
|
169
|
+
)
|
|
155
170
|
{
|
|
156
171
|
for (int i = 0; i < gameObject.transform.childCount; i++)
|
|
157
172
|
{
|
|
@@ -165,21 +180,26 @@
|
|
|
165
180
|
}
|
|
166
181
|
|
|
167
182
|
public static IEnumerable<GameObject> IterateOverChildGameObjectsRecursivelyIncludingSelf(
|
|
168
|
-
this GameObject gameObject
|
|
183
|
+
this GameObject gameObject
|
|
184
|
+
)
|
|
169
185
|
{
|
|
170
186
|
yield return gameObject;
|
|
171
187
|
|
|
172
188
|
for (int i = 0; i < gameObject.transform.childCount; ++i)
|
|
173
189
|
{
|
|
174
190
|
GameObject child = gameObject.transform.GetChild(i).gameObject;
|
|
175
|
-
foreach (
|
|
191
|
+
foreach (
|
|
192
|
+
GameObject c in child.IterateOverChildGameObjectsRecursivelyIncludingSelf()
|
|
193
|
+
)
|
|
176
194
|
{
|
|
177
195
|
yield return c;
|
|
178
196
|
}
|
|
179
197
|
}
|
|
180
198
|
}
|
|
181
199
|
|
|
182
|
-
public static IEnumerable<GameObject> IterateOverParentGameObjects(
|
|
200
|
+
public static IEnumerable<GameObject> IterateOverParentGameObjects(
|
|
201
|
+
this GameObject gameObject
|
|
202
|
+
)
|
|
183
203
|
{
|
|
184
204
|
Transform currentTransform = gameObject.transform.parent;
|
|
185
205
|
while (currentTransform != null)
|
|
@@ -190,7 +210,8 @@
|
|
|
190
210
|
}
|
|
191
211
|
|
|
192
212
|
public static IEnumerable<GameObject> IterateOverParentGameObjectsRecursivelyIncludingSelf(
|
|
193
|
-
this GameObject gameObject
|
|
213
|
+
this GameObject gameObject
|
|
214
|
+
)
|
|
194
215
|
{
|
|
195
216
|
yield return gameObject;
|
|
196
217
|
|
|
@@ -200,7 +221,11 @@
|
|
|
200
221
|
}
|
|
201
222
|
}
|
|
202
223
|
|
|
203
|
-
public static void EnableRecursively<T>(
|
|
224
|
+
public static void EnableRecursively<T>(
|
|
225
|
+
this Component component,
|
|
226
|
+
bool enabled,
|
|
227
|
+
Func<T, bool> exclude = null
|
|
228
|
+
)
|
|
204
229
|
where T : Behaviour
|
|
205
230
|
{
|
|
206
231
|
if (component == null)
|
|
@@ -230,8 +255,11 @@
|
|
|
230
255
|
}
|
|
231
256
|
|
|
232
257
|
public static void EnableRendererRecursively<T>(
|
|
233
|
-
this Component component,
|
|
234
|
-
|
|
258
|
+
this Component component,
|
|
259
|
+
bool enabled,
|
|
260
|
+
Func<T, bool> exclude = null
|
|
261
|
+
)
|
|
262
|
+
where T : Renderer
|
|
235
263
|
{
|
|
236
264
|
if (component == null)
|
|
237
265
|
{
|
|
@@ -257,7 +285,9 @@
|
|
|
257
285
|
}
|
|
258
286
|
}
|
|
259
287
|
|
|
260
|
-
public static IEnumerable<T> IterateOverAllChildComponentsRecursively<T>(
|
|
288
|
+
public static IEnumerable<T> IterateOverAllChildComponentsRecursively<T>(
|
|
289
|
+
this Component component
|
|
290
|
+
)
|
|
261
291
|
{
|
|
262
292
|
if (component == null)
|
|
263
293
|
{
|
|
@@ -308,7 +338,9 @@
|
|
|
308
338
|
}
|
|
309
339
|
}
|
|
310
340
|
|
|
311
|
-
public static IEnumerable<Transform> IterateOverAllParentsIncludingSelf(
|
|
341
|
+
public static IEnumerable<Transform> IterateOverAllParentsIncludingSelf(
|
|
342
|
+
this Component component
|
|
343
|
+
)
|
|
312
344
|
{
|
|
313
345
|
if (component == null)
|
|
314
346
|
{
|
|
@@ -323,7 +355,9 @@
|
|
|
323
355
|
}
|
|
324
356
|
}
|
|
325
357
|
|
|
326
|
-
public static IEnumerable<Transform> IterateOverAllChildrenRecursively(
|
|
358
|
+
public static IEnumerable<Transform> IterateOverAllChildrenRecursively(
|
|
359
|
+
this Component component
|
|
360
|
+
)
|
|
327
361
|
{
|
|
328
362
|
if (component == null)
|
|
329
363
|
{
|
|
@@ -334,7 +368,9 @@
|
|
|
334
368
|
{
|
|
335
369
|
Transform childTransform = component.transform.GetChild(i);
|
|
336
370
|
yield return childTransform;
|
|
337
|
-
foreach (
|
|
371
|
+
foreach (
|
|
372
|
+
Transform childChildTransform in childTransform.IterateOverAllChildrenRecursively()
|
|
373
|
+
)
|
|
338
374
|
{
|
|
339
375
|
yield return childChildTransform;
|
|
340
376
|
}
|
|
@@ -366,7 +402,8 @@
|
|
|
366
402
|
}
|
|
367
403
|
}
|
|
368
404
|
|
|
369
|
-
public static void DestroyAllComponentsOfType<T>(this GameObject gameObject)
|
|
405
|
+
public static void DestroyAllComponentsOfType<T>(this GameObject gameObject)
|
|
406
|
+
where T : Component
|
|
370
407
|
{
|
|
371
408
|
foreach (T component in gameObject.GetComponents<T>())
|
|
372
409
|
{
|
|
@@ -395,10 +432,12 @@
|
|
|
395
432
|
|
|
396
433
|
public static void DestroyAllChildrenGameObjectsImmediatelyConditionally(
|
|
397
434
|
this GameObject gameObject,
|
|
398
|
-
Func<GameObject, bool> acceptancePredicate
|
|
435
|
+
Func<GameObject, bool> acceptancePredicate
|
|
436
|
+
)
|
|
399
437
|
{
|
|
400
438
|
InternalDestroyAllChildrenGameObjects(
|
|
401
|
-
gameObject,
|
|
439
|
+
gameObject,
|
|
440
|
+
toDestroy =>
|
|
402
441
|
{
|
|
403
442
|
if (!acceptancePredicate(toDestroy))
|
|
404
443
|
{
|
|
@@ -406,15 +445,18 @@
|
|
|
406
445
|
}
|
|
407
446
|
|
|
408
447
|
Object.DestroyImmediate(toDestroy);
|
|
409
|
-
}
|
|
448
|
+
}
|
|
449
|
+
);
|
|
410
450
|
}
|
|
411
451
|
|
|
412
452
|
public static void DestroyAllChildGameObjectsConditionally(
|
|
413
453
|
this GameObject gameObject,
|
|
414
|
-
Func<GameObject, bool> acceptancePredicate
|
|
454
|
+
Func<GameObject, bool> acceptancePredicate
|
|
455
|
+
)
|
|
415
456
|
{
|
|
416
457
|
InternalDestroyAllChildrenGameObjects(
|
|
417
|
-
gameObject,
|
|
458
|
+
gameObject,
|
|
459
|
+
toDestroy =>
|
|
418
460
|
{
|
|
419
461
|
if (!acceptancePredicate(toDestroy))
|
|
420
462
|
{
|
|
@@ -422,7 +464,8 @@
|
|
|
422
464
|
}
|
|
423
465
|
|
|
424
466
|
toDestroy.Destroy();
|
|
425
|
-
}
|
|
467
|
+
}
|
|
468
|
+
);
|
|
426
469
|
}
|
|
427
470
|
|
|
428
471
|
public static void DestroyAllChildrenGameObjectsImmediately(this GameObject gameObject) =>
|
|
@@ -436,7 +479,8 @@
|
|
|
436
479
|
|
|
437
480
|
private static void InternalDestroyAllChildrenGameObjects(
|
|
438
481
|
this GameObject gameObject,
|
|
439
|
-
Action<GameObject> destroyFunction
|
|
482
|
+
Action<GameObject> destroyFunction
|
|
483
|
+
)
|
|
440
484
|
{
|
|
441
485
|
for (int i = gameObject.transform.childCount - 1; 0 <= i; --i)
|
|
442
486
|
{
|
|
@@ -479,7 +523,8 @@
|
|
|
479
523
|
return RadianToVector2(degree * Mathf.Deg2Rad);
|
|
480
524
|
}
|
|
481
525
|
|
|
482
|
-
public static T GetOrAddComponent<T>(this GameObject unityObject)
|
|
526
|
+
public static T GetOrAddComponent<T>(this GameObject unityObject)
|
|
527
|
+
where T : Component
|
|
483
528
|
{
|
|
484
529
|
if (!unityObject.TryGetComponent(out T instance))
|
|
485
530
|
{
|
|
@@ -536,8 +581,12 @@
|
|
|
536
581
|
|
|
537
582
|
// https://gamedevelopment.tutsplus.com/tutorials/unity-solution-for-hitting-moving-targets--cms-29633
|
|
538
583
|
public static Vector2 PredictCurrentTarget(
|
|
539
|
-
this GameObject currentTarget,
|
|
540
|
-
Vector2
|
|
584
|
+
this GameObject currentTarget,
|
|
585
|
+
Vector2 launchLocation,
|
|
586
|
+
float projectileSpeed,
|
|
587
|
+
bool predictiveFiring,
|
|
588
|
+
Vector2 targetVelocity
|
|
589
|
+
)
|
|
541
590
|
{
|
|
542
591
|
Vector2 target = currentTarget.transform.position;
|
|
543
592
|
|
|
@@ -551,17 +600,21 @@
|
|
|
551
600
|
return target;
|
|
552
601
|
}
|
|
553
602
|
|
|
554
|
-
float a =
|
|
555
|
-
|
|
603
|
+
float a =
|
|
604
|
+
(targetVelocity.x * targetVelocity.x)
|
|
605
|
+
+ (targetVelocity.y * targetVelocity.y)
|
|
606
|
+
- (projectileSpeed * projectileSpeed);
|
|
556
607
|
|
|
557
|
-
float b =
|
|
558
|
-
|
|
608
|
+
float b =
|
|
609
|
+
2
|
|
610
|
+
* (
|
|
611
|
+
targetVelocity.x * (target.x - launchLocation.x)
|
|
612
|
+
+ targetVelocity.y * (target.y - launchLocation.y)
|
|
613
|
+
);
|
|
559
614
|
|
|
560
615
|
float c =
|
|
561
|
-
((target.x - launchLocation.x) *
|
|
562
|
-
|
|
563
|
-
((target.y - launchLocation.y) *
|
|
564
|
-
(target.y - launchLocation.y));
|
|
616
|
+
((target.x - launchLocation.x) * (target.x - launchLocation.x))
|
|
617
|
+
+ ((target.y - launchLocation.y) * (target.y - launchLocation.y));
|
|
565
618
|
|
|
566
619
|
float disc = b * b - (4 * a * c);
|
|
567
620
|
if (disc < 0)
|
|
@@ -615,7 +668,7 @@
|
|
|
615
668
|
{
|
|
616
669
|
GameObject go => go,
|
|
617
670
|
Component c => c.gameObject,
|
|
618
|
-
_ => null
|
|
671
|
+
_ => null,
|
|
619
672
|
};
|
|
620
673
|
}
|
|
621
674
|
|
|
@@ -632,12 +685,18 @@
|
|
|
632
685
|
|
|
633
686
|
public static GameObject FindChildGameObjectWithTag(this GameObject gameObject, string tag)
|
|
634
687
|
{
|
|
635
|
-
return gameObject
|
|
688
|
+
return gameObject
|
|
689
|
+
.IterateOverChildGameObjectsRecursivelyIncludingSelf()
|
|
636
690
|
.FirstOrDefault(child => child.CompareTag(tag));
|
|
637
691
|
}
|
|
638
692
|
|
|
639
693
|
public static bool HasLineOfSight(
|
|
640
|
-
Vector2 initialLocation,
|
|
694
|
+
Vector2 initialLocation,
|
|
695
|
+
Vector2 direction,
|
|
696
|
+
Transform transform,
|
|
697
|
+
float totalDistance,
|
|
698
|
+
float delta
|
|
699
|
+
)
|
|
641
700
|
{
|
|
642
701
|
int hits = Physics2D.RaycastNonAlloc(initialLocation, direction, Buffers.RaycastHits);
|
|
643
702
|
for (int i = 0; i < hits; ++i)
|
|
@@ -659,13 +718,24 @@
|
|
|
659
718
|
}
|
|
660
719
|
|
|
661
720
|
public static Coroutine StartFunctionAsCoroutine(
|
|
662
|
-
this MonoBehaviour monoBehaviour,
|
|
663
|
-
|
|
721
|
+
this MonoBehaviour monoBehaviour,
|
|
722
|
+
Action action,
|
|
723
|
+
float updateRate,
|
|
724
|
+
bool useJitter = false,
|
|
725
|
+
bool waitBefore = false
|
|
726
|
+
)
|
|
664
727
|
{
|
|
665
|
-
return monoBehaviour.StartCoroutine(
|
|
728
|
+
return monoBehaviour.StartCoroutine(
|
|
729
|
+
FunctionAsCoroutine(action, updateRate, useJitter, waitBefore)
|
|
730
|
+
);
|
|
666
731
|
}
|
|
667
732
|
|
|
668
|
-
private static IEnumerator FunctionAsCoroutine(
|
|
733
|
+
private static IEnumerator FunctionAsCoroutine(
|
|
734
|
+
Action action,
|
|
735
|
+
float updateRate,
|
|
736
|
+
bool useJitter,
|
|
737
|
+
bool waitBefore
|
|
738
|
+
)
|
|
669
739
|
{
|
|
670
740
|
bool usedJitter = false;
|
|
671
741
|
while (true)
|
|
@@ -675,7 +745,7 @@
|
|
|
675
745
|
{
|
|
676
746
|
if (useJitter && !usedJitter)
|
|
677
747
|
{
|
|
678
|
-
float delay =
|
|
748
|
+
float delay = PRNG.Instance.NextFloat(updateRate);
|
|
679
749
|
startTime = Time.time;
|
|
680
750
|
while (!HasEnoughTimePassed(startTime, delay))
|
|
681
751
|
{
|
|
@@ -698,7 +768,7 @@
|
|
|
698
768
|
{
|
|
699
769
|
if (useJitter && !usedJitter)
|
|
700
770
|
{
|
|
701
|
-
float delay =
|
|
771
|
+
float delay = PRNG.Instance.NextFloat(updateRate);
|
|
702
772
|
startTime = Time.time;
|
|
703
773
|
while (!HasEnoughTimePassed(startTime, delay))
|
|
704
774
|
{
|
|
@@ -717,22 +787,37 @@
|
|
|
717
787
|
}
|
|
718
788
|
}
|
|
719
789
|
|
|
720
|
-
public static Coroutine ExecuteFunctionAfterDelay(
|
|
790
|
+
public static Coroutine ExecuteFunctionAfterDelay(
|
|
791
|
+
this MonoBehaviour monoBehaviour,
|
|
792
|
+
Action action,
|
|
793
|
+
float delay
|
|
794
|
+
)
|
|
721
795
|
{
|
|
722
796
|
return monoBehaviour.StartCoroutine(FunctionDelayAsCoroutine(action, delay));
|
|
723
797
|
}
|
|
724
798
|
|
|
725
|
-
public static Coroutine ExecuteFunctionNextFrame(
|
|
799
|
+
public static Coroutine ExecuteFunctionNextFrame(
|
|
800
|
+
this MonoBehaviour monoBehaviour,
|
|
801
|
+
Action action
|
|
802
|
+
)
|
|
726
803
|
{
|
|
727
804
|
return monoBehaviour.ExecuteFunctionAfterDelay(action, 0f);
|
|
728
805
|
}
|
|
729
806
|
|
|
730
|
-
public static Coroutine ExecuteFunctionAfterFrame(
|
|
807
|
+
public static Coroutine ExecuteFunctionAfterFrame(
|
|
808
|
+
this MonoBehaviour monoBehaviour,
|
|
809
|
+
Action action
|
|
810
|
+
)
|
|
731
811
|
{
|
|
732
812
|
return monoBehaviour.StartCoroutine(FunctionAfterFrame(action));
|
|
733
813
|
}
|
|
734
814
|
|
|
735
|
-
public static IEnumerator ExecuteOverTime(
|
|
815
|
+
public static IEnumerator ExecuteOverTime(
|
|
816
|
+
Action action,
|
|
817
|
+
int totalCount,
|
|
818
|
+
float duration,
|
|
819
|
+
bool delay = true
|
|
820
|
+
)
|
|
736
821
|
{
|
|
737
822
|
if (action == null)
|
|
738
823
|
{
|
|
@@ -750,7 +835,10 @@
|
|
|
750
835
|
{
|
|
751
836
|
float percent = (Time.time - startTime) / duration;
|
|
752
837
|
// optional delay execution from happening on 0, 1, 2, ... n-1 to 1, 2, ... n
|
|
753
|
-
if (
|
|
838
|
+
if (
|
|
839
|
+
totalExecuted < totalCount
|
|
840
|
+
&& ((totalExecuted + (delay ? 1f : 0f)) / totalCount) <= percent
|
|
841
|
+
)
|
|
754
842
|
{
|
|
755
843
|
action();
|
|
756
844
|
++totalExecuted;
|
|
@@ -759,7 +847,7 @@
|
|
|
759
847
|
yield return null;
|
|
760
848
|
}
|
|
761
849
|
|
|
762
|
-
for (; totalExecuted < totalCount;)
|
|
850
|
+
for (; totalExecuted < totalCount; )
|
|
763
851
|
{
|
|
764
852
|
action();
|
|
765
853
|
++totalExecuted;
|
|
@@ -828,7 +916,11 @@
|
|
|
828
916
|
|
|
829
917
|
public static Vector3Int AsVector3Int(this Vector3 vector)
|
|
830
918
|
{
|
|
831
|
-
return new Vector3Int(
|
|
919
|
+
return new Vector3Int(
|
|
920
|
+
(int)Math.Round(vector.x),
|
|
921
|
+
(int)Math.Round(vector.y),
|
|
922
|
+
(int)Math.Round(vector.z)
|
|
923
|
+
);
|
|
832
924
|
}
|
|
833
925
|
|
|
834
926
|
public static Vector3 AsVector3(this (uint x, uint y, uint z) vector)
|
|
@@ -856,7 +948,8 @@
|
|
|
856
948
|
return new Vector3Int(vector.x, vector.y);
|
|
857
949
|
}
|
|
858
950
|
|
|
859
|
-
public static T CopyTo<T>(this T original, GameObject destination)
|
|
951
|
+
public static T CopyTo<T>(this T original, GameObject destination)
|
|
952
|
+
where T : Component
|
|
860
953
|
{
|
|
861
954
|
Type type = original.GetType();
|
|
862
955
|
T copied = destination.GetComponent(type) as T;
|
|
@@ -877,8 +970,10 @@
|
|
|
877
970
|
}
|
|
878
971
|
}
|
|
879
972
|
|
|
880
|
-
foreach (
|
|
881
|
-
|
|
973
|
+
foreach (
|
|
974
|
+
FieldInfo field in type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
|
|
975
|
+
.Where(field => Attribute.IsDefined(field, typeof(SerializeField)))
|
|
976
|
+
)
|
|
882
977
|
{
|
|
883
978
|
try
|
|
884
979
|
{
|
|
@@ -915,30 +1010,46 @@
|
|
|
915
1010
|
return new Rect(bounds.x, bounds.y, bounds.size.x, bounds.size.y);
|
|
916
1011
|
}
|
|
917
1012
|
|
|
918
|
-
public static
|
|
1013
|
+
public static Vector2 GetRandomPointInCircle(
|
|
1014
|
+
Vector2 center,
|
|
1015
|
+
float radius,
|
|
1016
|
+
IRandom random = null
|
|
1017
|
+
)
|
|
919
1018
|
{
|
|
920
|
-
random ??=
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
return new
|
|
1019
|
+
random ??= PRNG.Instance;
|
|
1020
|
+
double r = radius * Math.Sqrt(random.NextDouble());
|
|
1021
|
+
double theta = random.NextDouble() * 2 * Math.PI;
|
|
1022
|
+
return new Vector2(
|
|
1023
|
+
center.x + (float)(r * Math.Cos(theta)),
|
|
1024
|
+
center.y + (float)(r * Math.Sin(theta))
|
|
1025
|
+
);
|
|
924
1026
|
}
|
|
925
1027
|
|
|
926
|
-
public static GameObject GetPlayerObjectInChildHierarchy(
|
|
1028
|
+
public static GameObject GetPlayerObjectInChildHierarchy(
|
|
1029
|
+
this GameObject gameObject,
|
|
1030
|
+
string playerTag = "Player"
|
|
1031
|
+
)
|
|
927
1032
|
{
|
|
928
|
-
return gameObject.GetTagObjectInChildHierarchy(
|
|
1033
|
+
return gameObject.GetTagObjectInChildHierarchy(playerTag);
|
|
929
1034
|
}
|
|
930
1035
|
|
|
931
|
-
public static GameObject GetTagObjectInChildHierarchy(
|
|
1036
|
+
public static GameObject GetTagObjectInChildHierarchy(
|
|
1037
|
+
this GameObject gameObject,
|
|
1038
|
+
string tag
|
|
1039
|
+
)
|
|
932
1040
|
{
|
|
933
|
-
return gameObject
|
|
1041
|
+
return gameObject
|
|
1042
|
+
.IterateOverChildGameObjectsRecursivelyIncludingSelf()
|
|
934
1043
|
.FirstOrDefault(go => go.CompareTag(tag));
|
|
935
1044
|
}
|
|
936
1045
|
|
|
937
1046
|
//https://answers.unity.com/questions/722748/refreshing-the-polygon-collider-2d-upon-sprite-cha.html
|
|
938
1047
|
public static void UpdateShapeToSprite(this Component component)
|
|
939
1048
|
{
|
|
940
|
-
if (
|
|
941
|
-
component.TryGetComponent(out
|
|
1049
|
+
if (
|
|
1050
|
+
!component.TryGetComponent(out SpriteRenderer spriteRenderer)
|
|
1051
|
+
|| component.TryGetComponent(out PolygonCollider2D collider)
|
|
1052
|
+
)
|
|
942
1053
|
{
|
|
943
1054
|
return;
|
|
944
1055
|
}
|
|
@@ -973,7 +1084,9 @@
|
|
|
973
1084
|
return new Vector3Int(x, y, z);
|
|
974
1085
|
}
|
|
975
1086
|
|
|
976
|
-
public static GameObject TryGetClosestParentWithComponentIncludingSelf<T>(
|
|
1087
|
+
public static GameObject TryGetClosestParentWithComponentIncludingSelf<T>(
|
|
1088
|
+
this GameObject current
|
|
1089
|
+
)
|
|
977
1090
|
where T : Component
|
|
978
1091
|
{
|
|
979
1092
|
while (current != null)
|
|
@@ -991,7 +1104,9 @@
|
|
|
991
1104
|
}
|
|
992
1105
|
|
|
993
1106
|
#if UNITY_EDITOR
|
|
994
|
-
public static IEnumerable<GameObject> EnumeratePrefabs(
|
|
1107
|
+
public static IEnumerable<GameObject> EnumeratePrefabs(
|
|
1108
|
+
IEnumerable<string> assetPaths = null
|
|
1109
|
+
)
|
|
995
1110
|
{
|
|
996
1111
|
assetPaths ??= new[] { "Assets/Prefabs", "Assets/Resources" };
|
|
997
1112
|
|
|
@@ -1011,7 +1126,9 @@
|
|
|
1011
1126
|
{
|
|
1012
1127
|
assetPaths ??= new[] { "Assets/Prefabs", "Assets/Resources", "Assets/TileMaps" };
|
|
1013
1128
|
|
|
1014
|
-
foreach (
|
|
1129
|
+
foreach (
|
|
1130
|
+
string assetGuid in AssetDatabase.FindAssets("t:" + typeof(T).Name, assetPaths)
|
|
1131
|
+
)
|
|
1015
1132
|
{
|
|
1016
1133
|
string path = AssetDatabase.GUIDToAssetPath(assetGuid);
|
|
1017
1134
|
T so = AssetDatabase.LoadAssetAtPath<T>(path);
|
|
@@ -1093,8 +1210,12 @@
|
|
|
1093
1210
|
{
|
|
1094
1211
|
MethodInfo awakeInfo = AwakeMethodsByType.GetOrAdd(
|
|
1095
1212
|
script.GetType(),
|
|
1096
|
-
type =>
|
|
1097
|
-
|
|
1213
|
+
type =>
|
|
1214
|
+
type.GetMethod(
|
|
1215
|
+
"Awake",
|
|
1216
|
+
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
|
|
1217
|
+
)
|
|
1218
|
+
);
|
|
1098
1219
|
if (awakeInfo != null)
|
|
1099
1220
|
{
|
|
1100
1221
|
_ = awakeInfo.Invoke(script, null);
|
|
@@ -1102,7 +1223,11 @@
|
|
|
1102
1223
|
}
|
|
1103
1224
|
}
|
|
1104
1225
|
|
|
1105
|
-
public static Vector2 GetAngleWithSpeed(
|
|
1226
|
+
public static Vector2 GetAngleWithSpeed(
|
|
1227
|
+
Vector2 targetDirection,
|
|
1228
|
+
Vector2 currentDirection,
|
|
1229
|
+
float rotationSpeed
|
|
1230
|
+
)
|
|
1106
1231
|
{
|
|
1107
1232
|
if (targetDirection == Vector2.zero)
|
|
1108
1233
|
{
|
|
@@ -1149,4 +1274,4 @@
|
|
|
1149
1274
|
}
|
|
1150
1275
|
}
|
|
1151
1276
|
}
|
|
1152
|
-
}
|
|
1277
|
+
}
|