com.wallstop-studios.unity-helpers 2.0.0-rc79.7 → 2.0.0-rc79.9

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.
@@ -83,7 +83,7 @@ namespace WallstopStudios.UnityHelpers.Core.Extension
83
83
  bool foundAny = false;
84
84
  foreach (Direction singleDirection in Directions)
85
85
  {
86
- if (direction.HasFlag(singleDirection))
86
+ if (direction.HasFlagNoAlloc(singleDirection))
87
87
  {
88
88
  foundAny = true;
89
89
  yield return singleDirection;
@@ -407,9 +407,79 @@ namespace WallstopStudios.UnityHelpers.Core.Random
407
407
  {
408
408
  return NextOf(list);
409
409
  }
410
-
411
410
  int index = Next(collection.Count);
412
- return collection.ElementAt(index);
411
+
412
+ switch (collection)
413
+ {
414
+ case HashSet<T> hashSet:
415
+ {
416
+ int i = 0;
417
+ foreach (T element in hashSet)
418
+ {
419
+ if (index == i++)
420
+ {
421
+ return element;
422
+ }
423
+ }
424
+ throw new ArgumentException(nameof(collection));
425
+ }
426
+ case SortedSet<T> sortedSet:
427
+ {
428
+ int i = 0;
429
+ foreach (T element in sortedSet)
430
+ {
431
+ if (index == i++)
432
+ {
433
+ return element;
434
+ }
435
+ }
436
+
437
+ throw new ArgumentException(nameof(collection));
438
+ }
439
+ case LinkedList<T> linkedList:
440
+ {
441
+ int i = 0;
442
+ foreach (T element in linkedList)
443
+ {
444
+ if (index == i++)
445
+ {
446
+ return element;
447
+ }
448
+ }
449
+
450
+ throw new ArgumentException(nameof(collection));
451
+ }
452
+ case Queue<T> queue:
453
+ {
454
+ int i = 0;
455
+ foreach (T element in queue)
456
+ {
457
+ if (index == i++)
458
+ {
459
+ return element;
460
+ }
461
+ }
462
+
463
+ throw new ArgumentException(nameof(collection));
464
+ }
465
+ case Stack<T> stack:
466
+ {
467
+ int i = 0;
468
+ foreach (T element in stack)
469
+ {
470
+ if (index == i++)
471
+ {
472
+ return element;
473
+ }
474
+ }
475
+
476
+ throw new ArgumentException(nameof(collection));
477
+ }
478
+ default:
479
+ {
480
+ return collection.ElementAt(index);
481
+ }
482
+ }
413
483
  }
414
484
 
415
485
  public T NextOf<T>(IReadOnlyList<T> list)
@@ -5,6 +5,7 @@ namespace WallstopStudios.UnityHelpers.Utils
5
5
  using System.Reflection;
6
6
  using System.Text;
7
7
  using UnityEngine;
8
+ using WallstopStudios.UnityHelpers.Core.Helper;
8
9
 
9
10
  public static class Buffers
10
11
  {
@@ -38,10 +39,16 @@ namespace WallstopStudios.UnityHelpers.Utils
38
39
  public static class WallstopGenericPool<T>
39
40
  where T : new()
40
41
  {
42
+ public static Action<T> clearAction;
43
+
41
44
  private static readonly List<T> _pool = new();
42
- private static readonly Action<T> _clearAction = GetClearAction();
43
45
  private static readonly Action<T> _onDispose = Release;
44
46
 
47
+ static WallstopGenericPool()
48
+ {
49
+ clearAction = GetClearAction();
50
+ }
51
+
45
52
  public static PooledResource<T> Get()
46
53
  {
47
54
  if (_pool.Count == 0)
@@ -57,26 +64,34 @@ namespace WallstopStudios.UnityHelpers.Utils
57
64
 
58
65
  private static Action<T> GetClearAction()
59
66
  {
60
- Type type = typeof(T);
61
- foreach (
62
- MethodInfo method in type.GetMethods(BindingFlags.Instance | BindingFlags.Public)
63
- )
67
+ try
64
68
  {
65
- if (
66
- string.Equals(method.Name, "Clear", StringComparison.Ordinal)
67
- && method.GetParameters().Length == 0
69
+ Type type = typeof(T);
70
+ foreach (
71
+ MethodInfo method in type.GetMethods(
72
+ BindingFlags.Instance | BindingFlags.Public
73
+ )
68
74
  )
69
75
  {
70
- return (Action<T>)Delegate.CreateDelegate(typeof(Action<T>), method);
76
+ if (
77
+ string.Equals(method.Name, "Clear", StringComparison.Ordinal)
78
+ && method.GetParameters().Length == 0
79
+ )
80
+ {
81
+ return (Action<T>)Delegate.CreateDelegate(typeof(Action<T>), method);
82
+ }
71
83
  }
72
84
  }
73
-
74
- return _ => { };
85
+ catch
86
+ {
87
+ // Swallow
88
+ }
89
+ return null;
75
90
  }
76
91
 
77
92
  private static void Release(T resource)
78
93
  {
79
- _clearAction.Invoke(resource);
94
+ clearAction?.Invoke(resource);
80
95
  _pool.Add(resource);
81
96
  }
82
97
  }
@@ -1,25 +1,62 @@
1
1
  namespace WallstopStudios.UnityHelpers.Utils
2
2
  {
3
+ using System;
3
4
  using System.Collections.Generic;
4
5
  using System.Linq;
5
6
  using Core.Extension;
6
7
  using UnityEngine;
7
8
 
9
+ [Flags]
10
+ public enum ChildSpawnMethod
11
+ {
12
+ None = 0,
13
+ Awake = 1 << 0,
14
+ OnEnabled = 1 << 1,
15
+ Start = 1 << 2,
16
+ }
17
+
8
18
  [DisallowMultipleComponent]
9
19
  public sealed class ChildSpawner : MonoBehaviour
10
20
  {
11
21
  private static readonly HashSet<GameObject> SpawnedPrefabs = new();
12
22
 
13
23
  [SerializeField]
14
- private GameObject[] _prefabs;
24
+ private ChildSpawnMethod _spawnMethod = ChildSpawnMethod.Start;
25
+
26
+ [SerializeField]
27
+ private GameObject[] _prefabs = Array.Empty<GameObject>();
15
28
 
16
29
  [SerializeField]
17
- private GameObject[] _editorOnlyPrefabs;
30
+ private GameObject[] _editorOnlyPrefabs = Array.Empty<GameObject>();
18
31
 
19
32
  [SerializeField]
20
- private GameObject[] _developmentOnlyPrefabs;
33
+ private GameObject[] _developmentOnlyPrefabs = Array.Empty<GameObject>();
34
+
35
+ private void Awake()
36
+ {
37
+ if (_spawnMethod.HasFlagNoAlloc(ChildSpawnMethod.Awake))
38
+ {
39
+ Spawn();
40
+ }
41
+ }
42
+
43
+ private void OnEnable()
44
+ {
45
+ if (_spawnMethod.HasFlagNoAlloc(ChildSpawnMethod.OnEnabled))
46
+ {
47
+ Spawn();
48
+ }
49
+ }
21
50
 
22
51
  private void Start()
52
+ {
53
+ if (_spawnMethod.HasFlagNoAlloc(ChildSpawnMethod.Start))
54
+ {
55
+ Spawn();
56
+ }
57
+ }
58
+
59
+ private void Spawn()
23
60
  {
24
61
  if (
25
62
  _prefabs
@@ -41,13 +41,9 @@ namespace WallstopStudios.UnityHelpers.Utils
41
41
 
42
42
  Type type = typeof(T);
43
43
  GameObject instance = new($"{type.Name}-Singleton", type);
44
- if (
45
- instance.TryGetComponent(out _instance)
46
- && _instance.Preserve
47
- && Application.isPlaying
48
- )
44
+ if (_instance == null)
49
45
  {
50
- DontDestroyOnLoad(instance);
46
+ _ = instance.TryGetComponent(out _instance);
51
47
  }
52
48
 
53
49
  return _instance;
@@ -61,6 +57,11 @@ namespace WallstopStudios.UnityHelpers.Utils
61
57
  {
62
58
  _instance = this as T;
63
59
  }
60
+
61
+ if (Preserve && Application.isPlaying)
62
+ {
63
+ DontDestroyOnLoad(gameObject);
64
+ }
64
65
  }
65
66
 
66
67
  protected virtual void Start()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.wallstop-studios.unity-helpers",
3
- "version": "2.0.0-rc79.7",
3
+ "version": "2.0.0-rc79.9",
4
4
  "displayName": "Unity Helpers",
5
5
  "description": "Various Unity Helper Library",
6
6
  "dependencies": {},
@@ -51,3 +51,5 @@
51
51
 
52
52
 
53
53
 
54
+
55
+