com.wallstop-studios.unity-helpers 2.0.0-rc06 → 2.0.0-rc08

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.
Files changed (28) hide show
  1. package/Runtime/Core/Extension/DirectionExtensions.cs +5 -2
  2. package/Runtime/Core/Extension/IEnumerableExtensions.cs +19 -6
  3. package/Runtime/Core/Extension/RandomExtensions.cs +9 -87
  4. package/Runtime/Core/Extension/UnityExtensions.cs +2 -2
  5. package/Runtime/Core/Helper/Helpers.cs +1 -556
  6. package/Runtime/Core/Helper/Partials/LogHelpers.cs +13 -0
  7. package/Runtime/Core/Helper/Partials/LogHelpers.cs.meta +3 -0
  8. package/Runtime/Core/Helper/Partials/MathHelpers.cs +30 -0
  9. package/Runtime/Core/Helper/Partials/MathHelpers.cs.meta +3 -0
  10. package/Runtime/Core/Helper/Partials/ObjectHelpers.cs +388 -0
  11. package/Runtime/Core/Helper/Partials/ObjectHelpers.cs.meta +3 -0
  12. package/Runtime/Core/Helper/Partials/TransformHelpers.cs +167 -0
  13. package/Runtime/Core/Helper/Partials/TransformHelpers.cs.meta +3 -0
  14. package/Runtime/Core/Helper/Partials.meta +3 -0
  15. package/Runtime/Core/Random/AbstractRandom.cs +140 -154
  16. package/Runtime/Core/Random/IRandom.cs +26 -7
  17. package/Runtime/Core/Random/PerlinNoise.cs +369 -0
  18. package/Runtime/Core/Random/PerlinNoise.cs.meta +3 -0
  19. package/Runtime/Core/Random/SquirrelRandom.cs +9 -10
  20. package/Runtime/Core/Random/SystemRandom.cs +78 -41
  21. package/Tests/Runtime/Extensions/RandomExtensionTests.cs +27 -0
  22. package/Tests/Runtime/Extensions/RandomExtensionTests.cs.meta +3 -0
  23. package/Tests/Runtime/Helper/ObjectHelperTests.cs +402 -0
  24. package/Tests/Runtime/Helper/ObjectHelperTests.cs.meta +3 -0
  25. package/Tests/Runtime/Performance/RandomPerformanceTests.cs +58 -3
  26. package/Tests/Runtime/Random/RandomTestBase.cs +557 -6
  27. package/Tests/Runtime/Random/SquirrelRandomTests.cs +5 -0
  28. package/package.json +1 -1
@@ -0,0 +1,388 @@
1
+ namespace UnityHelpers.Core.Helper
2
+ {
3
+ using System;
4
+ using Extension;
5
+ using UnityEngine;
6
+ using UnityEngine.SceneManagement;
7
+ using Object = UnityEngine.Object;
8
+ #if UNITY_EDITOR
9
+ using UnityEditor;
10
+ using UnityEditor.SceneManagement;
11
+ #endif
12
+
13
+ public static partial class Helpers
14
+ {
15
+ public static T Find<T>(this Object component, string tag, bool log = true)
16
+ where T : Object
17
+ {
18
+ if (ObjectsByTag.TryGetValue(tag, out Object value))
19
+ {
20
+ if (value != null && value is T typed)
21
+ {
22
+ return typed;
23
+ }
24
+
25
+ _ = ObjectsByTag.Remove(tag);
26
+ }
27
+
28
+ GameObject gameObject = GameObject.FindGameObjectWithTag(tag);
29
+ if (gameObject == null)
30
+ {
31
+ if (log)
32
+ {
33
+ component.LogWarn("Could not find {0}.", tag);
34
+ }
35
+
36
+ return default;
37
+ }
38
+
39
+ if (gameObject.TryGetComponent(out T instance))
40
+ {
41
+ ObjectsByTag[tag] = instance;
42
+ return instance;
43
+ }
44
+
45
+ if (log)
46
+ {
47
+ component.LogWarn(
48
+ "Failed to find {0} on {1} (name: {2}), id [{3}].",
49
+ typeof(T).Name,
50
+ tag,
51
+ gameObject.name,
52
+ gameObject.GetInstanceID()
53
+ );
54
+ }
55
+
56
+ return default;
57
+ }
58
+
59
+ public static T Find<T>(string tag, bool log = true)
60
+ where T : MonoBehaviour
61
+ {
62
+ if (ObjectsByTag.TryGetValue(tag, out Object value))
63
+ {
64
+ if (value != null && value is T typed)
65
+ {
66
+ return typed;
67
+ }
68
+
69
+ _ = ObjectsByTag.Remove(tag);
70
+ }
71
+
72
+ GameObject gameObject = GameObject.FindGameObjectWithTag(tag);
73
+ if (gameObject == null)
74
+ {
75
+ if (log)
76
+ {
77
+ LogObject.Log($"Could not find {tag}.");
78
+ }
79
+
80
+ return default;
81
+ }
82
+
83
+ if (gameObject.TryGetComponent(out T instance))
84
+ {
85
+ ObjectsByTag[tag] = instance;
86
+ return instance;
87
+ }
88
+
89
+ if (log)
90
+ {
91
+ LogObject.Log($"Failed to find {typeof(T).Name} on {tag}");
92
+ }
93
+
94
+ return default;
95
+ }
96
+
97
+ public static void SetInstance<T>(string tag, T instance)
98
+ where T : MonoBehaviour
99
+ {
100
+ ObjectsByTag[tag] = instance;
101
+ }
102
+
103
+ public static void ClearInstance<T>(string tag, T instance)
104
+ where T : MonoBehaviour
105
+ {
106
+ if (ObjectsByTag.TryGetValue(tag, out Object existing) && existing == instance)
107
+ {
108
+ _ = ObjectsByTag.Remove(tag);
109
+ }
110
+ }
111
+
112
+ public static bool HasComponent<T>(this Object unityObject)
113
+ where T : Object
114
+ {
115
+ return unityObject switch
116
+ {
117
+ GameObject go => go.HasComponent<T>(),
118
+ Component component => component.HasComponent<T>(),
119
+ _ => false,
120
+ };
121
+ }
122
+
123
+ public static bool HasComponent<T>(this Component component)
124
+ where T : Object
125
+ {
126
+ return component.TryGetComponent<T>(out _);
127
+ }
128
+
129
+ public static bool HasComponent<T>(this GameObject gameObject)
130
+ where T : Object
131
+ {
132
+ return gameObject.TryGetComponent<T>(out _);
133
+ }
134
+
135
+ public static bool HasComponent(this Object unityObject, Type type)
136
+ {
137
+ return unityObject switch
138
+ {
139
+ GameObject go => go.TryGetComponent(type, out _),
140
+ Component component => component.TryGetComponent(type, out _),
141
+ _ => false,
142
+ };
143
+ }
144
+
145
+ public static void EnableRecursively<T>(
146
+ this Component component,
147
+ bool enabled,
148
+ Func<T, bool> exclude = null
149
+ )
150
+ where T : Behaviour
151
+ {
152
+ if (component == null)
153
+ {
154
+ return;
155
+ }
156
+
157
+ foreach (T behaviour in component.GetComponents<T>())
158
+ {
159
+ if (behaviour != null && !(exclude?.Invoke(behaviour) ?? false))
160
+ {
161
+ behaviour.enabled = enabled;
162
+ }
163
+ }
164
+
165
+ Transform transform = (component as Transform) ?? component.transform;
166
+ if (transform == null)
167
+ {
168
+ return;
169
+ }
170
+
171
+ for (int i = 0; i < transform.childCount; ++i)
172
+ {
173
+ Transform child = transform.GetChild(i);
174
+ EnableRecursively<T>(child, enabled, exclude);
175
+ }
176
+ }
177
+
178
+ public static void EnableRendererRecursively<T>(
179
+ this Component component,
180
+ bool enabled,
181
+ Func<T, bool> exclude = null
182
+ )
183
+ where T : Renderer
184
+ {
185
+ if (component == null)
186
+ {
187
+ return;
188
+ }
189
+
190
+ T behavior = component as T ?? component.GetComponent<T>();
191
+ if (behavior != null && !(exclude?.Invoke(behavior) ?? false))
192
+ {
193
+ behavior.enabled = enabled;
194
+ }
195
+
196
+ Transform transform = (component as Transform) ?? component.transform;
197
+ if (transform == null)
198
+ {
199
+ return;
200
+ }
201
+
202
+ for (int i = 0; i < transform.childCount; ++i)
203
+ {
204
+ Transform child = transform.GetChild(i);
205
+ EnableRendererRecursively<T>(child, enabled, exclude);
206
+ }
207
+ }
208
+
209
+ public static void DestroyAllChildrenGameObjects(this GameObject gameObject)
210
+ {
211
+ if (Application.isEditor)
212
+ {
213
+ EditorDestroyAllChildrenGameObjects(gameObject);
214
+ }
215
+ else
216
+ {
217
+ PlayDestroyAllChildrenGameObjects(gameObject);
218
+ }
219
+ }
220
+
221
+ public static void DestroyAllComponentsOfType<T>(this GameObject gameObject)
222
+ where T : Component
223
+ {
224
+ foreach (T component in gameObject.GetComponents<T>())
225
+ {
226
+ SmartDestroy(component);
227
+ }
228
+ }
229
+
230
+ public static void SmartDestroy(this Object obj, float? afterTime = null)
231
+ {
232
+ if (Application.isEditor && !Application.isPlaying)
233
+ {
234
+ Object.DestroyImmediate(obj);
235
+ }
236
+ else
237
+ {
238
+ if (afterTime.HasValue)
239
+ {
240
+ Object.Destroy(obj, afterTime.Value);
241
+ }
242
+ else
243
+ {
244
+ Object.Destroy(obj);
245
+ }
246
+ }
247
+ }
248
+
249
+ public static void DestroyAllChildrenGameObjectsImmediatelyConditionally(
250
+ this GameObject gameObject,
251
+ Func<GameObject, bool> acceptancePredicate
252
+ )
253
+ {
254
+ InternalDestroyAllChildrenGameObjects(
255
+ gameObject,
256
+ toDestroy =>
257
+ {
258
+ if (!acceptancePredicate(toDestroy))
259
+ {
260
+ return;
261
+ }
262
+
263
+ Object.DestroyImmediate(toDestroy);
264
+ }
265
+ );
266
+ }
267
+
268
+ public static void DestroyAllChildGameObjectsConditionally(
269
+ this GameObject gameObject,
270
+ Func<GameObject, bool> acceptancePredicate
271
+ )
272
+ {
273
+ InternalDestroyAllChildrenGameObjects(
274
+ gameObject,
275
+ toDestroy =>
276
+ {
277
+ if (!acceptancePredicate(toDestroy))
278
+ {
279
+ return;
280
+ }
281
+
282
+ toDestroy.Destroy();
283
+ }
284
+ );
285
+ }
286
+
287
+ public static void DestroyAllChildrenGameObjectsImmediately(this GameObject gameObject) =>
288
+ InternalDestroyAllChildrenGameObjects(gameObject, Object.DestroyImmediate);
289
+
290
+ public static void PlayDestroyAllChildrenGameObjects(this GameObject gameObject) =>
291
+ InternalDestroyAllChildrenGameObjects(gameObject, go => go.Destroy());
292
+
293
+ public static void EditorDestroyAllChildrenGameObjects(this GameObject gameObject) =>
294
+ InternalDestroyAllChildrenGameObjects(gameObject, go => go.Destroy());
295
+
296
+ private static void InternalDestroyAllChildrenGameObjects(
297
+ this GameObject gameObject,
298
+ Action<GameObject> destroyFunction
299
+ )
300
+ {
301
+ for (int i = gameObject.transform.childCount - 1; 0 <= i; --i)
302
+ {
303
+ destroyFunction(gameObject.transform.GetChild(i).gameObject);
304
+ }
305
+ }
306
+
307
+ public static bool IsPrefab(this GameObject gameObject)
308
+ {
309
+ Scene scene = gameObject.scene;
310
+ #if UNITY_EDITOR
311
+ if (scene.rootCount == 1 && string.Equals(scene.name, gameObject.name))
312
+ {
313
+ return true;
314
+ }
315
+
316
+ return PrefabUtility.GetPrefabAssetType(gameObject) switch
317
+ {
318
+ PrefabAssetType.NotAPrefab => false,
319
+ PrefabAssetType.MissingAsset => scene.rootCount == 0,
320
+ _ => true,
321
+ };
322
+ #else
323
+ return scene.rootCount == 0;
324
+ #endif
325
+ }
326
+
327
+ public static bool IsPrefab(this Component component)
328
+ {
329
+ return IsPrefab(component.gameObject);
330
+ }
331
+
332
+ public static T GetOrAddComponent<T>(this GameObject unityObject)
333
+ where T : Component
334
+ {
335
+ if (!unityObject.TryGetComponent(out T instance))
336
+ {
337
+ instance = unityObject.AddComponent<T>();
338
+ }
339
+
340
+ return instance;
341
+ }
342
+
343
+ public static Component GetOrAddComponent(this GameObject unityObject, Type componentType)
344
+ {
345
+ if (!unityObject.TryGetComponent(componentType, out Component instance))
346
+ {
347
+ instance = unityObject.AddComponent(componentType);
348
+ }
349
+
350
+ return instance;
351
+ }
352
+
353
+ public static void ModifyAndSavePrefab(GameObject prefab, Action<GameObject> modifyAction)
354
+ {
355
+ if (prefab == null)
356
+ {
357
+ return;
358
+ }
359
+
360
+ #if UNITY_EDITOR
361
+ if (PrefabUtility.IsPartOfPrefabAsset(prefab))
362
+ {
363
+ string assetPath = AssetDatabase.GetAssetPath(prefab);
364
+ GameObject content = PrefabUtility.LoadPrefabContents(assetPath);
365
+
366
+ if (content == null)
367
+ {
368
+ Debug.LogError($"Unable to load {prefab} as a prefab");
369
+ return;
370
+ }
371
+
372
+ modifyAction(content);
373
+ _ = PrefabUtility.SaveAsPrefabAsset(content, assetPath);
374
+ PrefabUtility.UnloadPrefabContents(content);
375
+ }
376
+ else
377
+ {
378
+ modifyAction(prefab);
379
+ PrefabStage stage = PrefabStageUtility.GetPrefabStage(prefab);
380
+ if (stage)
381
+ {
382
+ _ = EditorSceneManager.MarkSceneDirty(stage.scene);
383
+ }
384
+ }
385
+ #endif
386
+ }
387
+ }
388
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 321716db87b24857a466b1c6c895c72b
3
+ timeCreated: 1732646645
@@ -0,0 +1,167 @@
1
+ namespace UnityHelpers.Core.Helper
2
+ {
3
+ using System.Collections.Generic;
4
+ using UnityEngine;
5
+
6
+ public static partial class Helpers
7
+ {
8
+ public static IEnumerable<GameObject> IterateOverChildGameObjects(
9
+ this GameObject gameObject
10
+ )
11
+ {
12
+ for (int i = 0; i < gameObject.transform.childCount; i++)
13
+ {
14
+ yield return gameObject.transform.GetChild(i).gameObject;
15
+ }
16
+ }
17
+
18
+ public static IEnumerable<GameObject> IterateOverChildGameObjectsRecursively(
19
+ this GameObject gameObject
20
+ )
21
+ {
22
+ for (int i = 0; i < gameObject.transform.childCount; i++)
23
+ {
24
+ GameObject child = gameObject.transform.GetChild(i).gameObject;
25
+ yield return child;
26
+ foreach (GameObject go in child.IterateOverChildGameObjectsRecursively())
27
+ {
28
+ yield return go;
29
+ }
30
+ }
31
+ }
32
+
33
+ public static IEnumerable<GameObject> IterateOverChildGameObjectsRecursivelyIncludingSelf(
34
+ this GameObject gameObject
35
+ )
36
+ {
37
+ yield return gameObject;
38
+
39
+ for (int i = 0; i < gameObject.transform.childCount; ++i)
40
+ {
41
+ GameObject child = gameObject.transform.GetChild(i).gameObject;
42
+ foreach (
43
+ GameObject c in child.IterateOverChildGameObjectsRecursivelyIncludingSelf()
44
+ )
45
+ {
46
+ yield return c;
47
+ }
48
+ }
49
+ }
50
+
51
+ public static IEnumerable<GameObject> IterateOverParentGameObjects(
52
+ this GameObject gameObject
53
+ )
54
+ {
55
+ Transform currentTransform = gameObject.transform.parent;
56
+ while (currentTransform != null)
57
+ {
58
+ yield return currentTransform.gameObject;
59
+ currentTransform = currentTransform.parent;
60
+ }
61
+ }
62
+
63
+ public static IEnumerable<GameObject> IterateOverParentGameObjectsRecursivelyIncludingSelf(
64
+ this GameObject gameObject
65
+ )
66
+ {
67
+ yield return gameObject;
68
+
69
+ foreach (GameObject parent in IterateOverParentGameObjects(gameObject))
70
+ {
71
+ yield return parent;
72
+ }
73
+ }
74
+
75
+ public static IEnumerable<T> IterateOverAllChildComponentsRecursively<T>(
76
+ this Component component
77
+ )
78
+ {
79
+ if (component == null)
80
+ {
81
+ yield break;
82
+ }
83
+
84
+ foreach (T c in component.gameObject.GetComponents<T>())
85
+ {
86
+ yield return c;
87
+ }
88
+
89
+ for (int i = 0; i < component.transform.childCount; ++i)
90
+ {
91
+ Transform child = component.transform.GetChild(i);
92
+
93
+ foreach (T c in child.IterateOverAllChildComponentsRecursively<T>())
94
+ {
95
+ yield return c;
96
+ }
97
+ }
98
+ }
99
+
100
+ public static IEnumerable<Transform> IterateOverAllChildren(this Component component)
101
+ {
102
+ if (component == null)
103
+ {
104
+ yield break;
105
+ }
106
+
107
+ for (int i = 0; i < component.transform.childCount; ++i)
108
+ {
109
+ yield return component.transform.GetChild(i);
110
+ }
111
+ }
112
+
113
+ public static IEnumerable<Transform> IterateOverAllParents(this Component component)
114
+ {
115
+ if (component == null)
116
+ {
117
+ yield break;
118
+ }
119
+
120
+ Transform transform = component.transform;
121
+ while (transform.parent != null)
122
+ {
123
+ yield return transform.parent;
124
+ transform = transform.parent;
125
+ }
126
+ }
127
+
128
+ public static IEnumerable<Transform> IterateOverAllParentsIncludingSelf(
129
+ this Component component
130
+ )
131
+ {
132
+ if (component == null)
133
+ {
134
+ yield break;
135
+ }
136
+
137
+ Transform transform = component.transform;
138
+ while (transform != null)
139
+ {
140
+ yield return transform;
141
+ transform = transform.parent;
142
+ }
143
+ }
144
+
145
+ public static IEnumerable<Transform> IterateOverAllChildrenRecursively(
146
+ this Component component
147
+ )
148
+ {
149
+ if (component == null)
150
+ {
151
+ yield break;
152
+ }
153
+
154
+ for (int i = 0; i < component.transform.childCount; ++i)
155
+ {
156
+ Transform childTransform = component.transform.GetChild(i);
157
+ yield return childTransform;
158
+ foreach (
159
+ Transform childChildTransform in childTransform.IterateOverAllChildrenRecursively()
160
+ )
161
+ {
162
+ yield return childChildTransform;
163
+ }
164
+ }
165
+ }
166
+ }
167
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 6fa254f39ddb413799e14fc26fccc6ea
3
+ timeCreated: 1732647396
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 4316246bccd04b778b1bdeee96f54110
3
+ timeCreated: 1732646634