com.wallstop-studios.unity-helpers 2.0.0-rc40 → 2.0.0-rc42

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.
@@ -171,10 +171,9 @@
171
171
  {
172
172
  if (component == null)
173
173
  {
174
- return Enumerable.Empty<Transform>();
174
+ yield break;
175
175
  }
176
176
 
177
- Queue<Transform> results = new();
178
177
  Queue<Transform> iteration = new();
179
178
  iteration.Enqueue(component.transform);
180
179
  while (iteration.TryDequeue(out Transform current))
@@ -182,12 +181,10 @@
182
181
  for (int i = 0; i < current.childCount; ++i)
183
182
  {
184
183
  Transform childTransform = current.GetChild(i);
185
- results.Enqueue(childTransform);
186
184
  iteration.Enqueue(childTransform);
185
+ yield return childTransform;
187
186
  }
188
187
  }
189
-
190
- return results;
191
188
  }
192
189
  }
193
190
  }
@@ -72,6 +72,76 @@
72
72
  #endif
73
73
  }
74
74
 
75
+ public static Func<TInstance, TValue> GetFieldGetter<TInstance, TValue>(FieldInfo field)
76
+ {
77
+ #if WEB_GL
78
+ return Getter;
79
+ TValue Getter(TInstance instance)
80
+ {
81
+ return (TValue)field.GetValue(instance);
82
+ }
83
+ #else
84
+ DynamicMethod dynamicMethod = new(
85
+ $"GetGeneric{field.DeclaringType.Name}{field.Name}",
86
+ typeof(TValue),
87
+ new[] { typeof(TInstance) },
88
+ field.DeclaringType,
89
+ true
90
+ );
91
+
92
+ ILGenerator il = dynamicMethod.GetILGenerator();
93
+
94
+ if (!field.IsStatic)
95
+ {
96
+ if (typeof(TInstance).IsValueType)
97
+ {
98
+ il.Emit(OpCodes.Ldarga_S, 0);
99
+ }
100
+ else
101
+ {
102
+ il.Emit(OpCodes.Ldarg_0);
103
+ }
104
+
105
+ if (field.DeclaringType != typeof(TInstance))
106
+ {
107
+ il.Emit(
108
+ field.DeclaringType.IsValueType ? OpCodes.Unbox : OpCodes.Castclass,
109
+ field.DeclaringType
110
+ );
111
+ }
112
+
113
+ il.Emit(OpCodes.Ldfld, field);
114
+ }
115
+ else
116
+ {
117
+ il.Emit(OpCodes.Ldsfld, field);
118
+ }
119
+
120
+ if (field.FieldType.IsValueType)
121
+ {
122
+ if (!typeof(TValue).IsValueType)
123
+ {
124
+ il.Emit(OpCodes.Box, field.FieldType);
125
+ }
126
+ }
127
+ else
128
+ {
129
+ if (typeof(TValue).IsValueType)
130
+ {
131
+ il.Emit(OpCodes.Unbox_Any, typeof(TValue));
132
+ }
133
+ else if (typeof(TValue) != field.FieldType)
134
+ {
135
+ il.Emit(OpCodes.Castclass, typeof(TValue));
136
+ }
137
+ }
138
+
139
+ il.Emit(OpCodes.Ret);
140
+ return (Func<TInstance, TValue>)
141
+ dynamicMethod.CreateDelegate(typeof(Func<TInstance, TValue>));
142
+ #endif
143
+ }
144
+
75
145
  public static FieldSetter<TInstance, TValue> GetFieldSetter<TInstance, TValue>(
76
146
  FieldInfo field
77
147
  )
@@ -22,7 +22,7 @@
22
22
  }
23
23
  else
24
24
  {
25
- Debug.LogError("NO SUCH METHOD " + methodName + " FOR " + type);
25
+ Debug.LogError($"NO SUCH METHOD {methodName} FOR {type}");
26
26
  }
27
27
  }
28
28
 
@@ -7,7 +7,6 @@
7
7
  {
8
8
  public const int BufferSize = 10_000;
9
9
 
10
- public static readonly HashSet<Collider2D> UniqueColliders = new();
11
10
  public static readonly Collider2D[] Colliders = new Collider2D[BufferSize];
12
11
  public static readonly RaycastHit2D[] RaycastHits = new RaycastHit2D[BufferSize];
13
12
 
@@ -16,6 +15,8 @@
16
15
  DO NOT USE with random values.
17
16
  */
18
17
  public static readonly Dictionary<float, WaitForSeconds> WaitForSeconds = new();
18
+ public static readonly Dictionary<float, WaitForSecondsRealtime> WaitForSecondsRealtime =
19
+ new();
19
20
  public static readonly System.Random Random = new();
20
21
  public static readonly WaitForFixedUpdate WaitForFixedUpdate = new();
21
22
  public static readonly WaitForEndOfFrame WaitForEndOfFrame = new();
@@ -22,6 +22,7 @@
22
22
 
23
23
  private readonly Random _random = new();
24
24
 
25
+ // TODO: Test on static fields
25
26
  [Test]
26
27
  public void GetFieldGetterClass()
27
28
  {
@@ -37,6 +38,7 @@
37
38
  }
38
39
  }
39
40
 
41
+ // TODO: Test on static fields
40
42
  [Test]
41
43
  public void GetFieldGetterStruct()
42
44
  {
@@ -52,6 +54,7 @@
52
54
  }
53
55
  }
54
56
 
57
+ // TODO: Test on static fields
55
58
  [Test]
56
59
  public void GetFieldSetterClass()
57
60
  {
@@ -67,6 +70,7 @@
67
70
  }
68
71
  }
69
72
 
73
+ // TODO: Test on static fields
70
74
  [Test]
71
75
  public void GetFieldSetterStruct()
72
76
  {
@@ -83,22 +87,24 @@
83
87
  }
84
88
  }
85
89
 
90
+ // TODO: Test on static fields
86
91
  [Test]
87
92
  public void GetFieldSetterClassGeneric()
88
93
  {
89
94
  TestClass testClass = new();
90
- FieldSetter<TestClass, int> structSetter = ReflectionHelpers.GetFieldSetter<
95
+ FieldSetter<TestClass, int> classSetter = ReflectionHelpers.GetFieldSetter<
91
96
  TestClass,
92
97
  int
93
98
  >(typeof(TestClass).GetField(nameof(TestClass.intValue)));
94
99
  for (int i = 0; i < NumTries; ++i)
95
100
  {
96
101
  int expected = _random.Next(int.MinValue, int.MaxValue);
97
- structSetter(ref testClass, expected);
102
+ classSetter(ref testClass, expected);
98
103
  Assert.AreEqual(expected, testClass.intValue);
99
104
  }
100
105
  }
101
106
 
107
+ // TODO: Test on static fields
102
108
  [Test]
103
109
  public void GetFieldSetterStructGeneric()
104
110
  {
@@ -115,6 +121,36 @@
115
121
  }
116
122
  }
117
123
 
124
+ // TODO: Test on static fields
125
+ [Test]
126
+ public void GetFieldGetterClassGeneric()
127
+ {
128
+ TestClass testClass = new();
129
+ Func<TestClass, int> classGetter = ReflectionHelpers.GetFieldGetter<TestClass, int>(
130
+ typeof(TestClass).GetField(nameof(TestClass.intValue))
131
+ );
132
+ for (int i = 0; i < NumTries; ++i)
133
+ {
134
+ testClass.intValue = _random.Next(int.MinValue, int.MaxValue);
135
+ Assert.AreEqual(testClass.intValue, classGetter(testClass));
136
+ }
137
+ }
138
+
139
+ // TODO: Test on static fields
140
+ [Test]
141
+ public void GetFieldGetterStructGeneric()
142
+ {
143
+ TestStruct testStruct = new();
144
+ Func<TestStruct, int> structSetter = ReflectionHelpers.GetFieldGetter<TestStruct, int>(
145
+ typeof(TestStruct).GetField(nameof(TestStruct.intValue))
146
+ );
147
+ for (int i = 0; i < NumTries; ++i)
148
+ {
149
+ testStruct.intValue = _random.Next(int.MinValue, int.MaxValue);
150
+ Assert.AreEqual(testStruct.intValue, structSetter(testStruct));
151
+ }
152
+ }
153
+
118
154
  [Test]
119
155
  public void ArrayCreator()
120
156
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.wallstop-studios.unity-helpers",
3
- "version": "2.0.0-rc40",
3
+ "version": "2.0.0-rc42",
4
4
  "displayName": "Unity Helpers",
5
5
  "description": "Various Unity Helper Library",
6
6
  "dependencies": {},