com.wallstop-studios.unity-helpers 2.0.0-rc06 → 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/Helper/Helpers.cs +1 -556
- 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/Random/AbstractRandom.cs +182 -150
- package/Runtime/Core/Random/SquirrelRandom.cs +9 -10
- package/Runtime/Core/Random/SystemRandom.cs +78 -41
- package/Tests/Runtime/Helper/ObjectHelperTests.cs +402 -0
- package/Tests/Runtime/Helper/ObjectHelperTests.cs.meta +3 -0
- package/Tests/Runtime/Performance/RandomPerformanceTests.cs +58 -3
- package/Tests/Runtime/Random/RandomTestBase.cs +261 -6
- package/Tests/Runtime/Random/SquirrelRandomTests.cs +5 -0
- package/package.json +1 -1
|
@@ -17,568 +17,13 @@
|
|
|
17
17
|
using UnityEditor.SceneManagement;
|
|
18
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)
|
|
28
|
-
where T : Object
|
|
29
|
-
{
|
|
30
|
-
if (ObjectsByTag.TryGetValue(tag, out Object value))
|
|
31
|
-
{
|
|
32
|
-
if (value != null && value is T typed)
|
|
33
|
-
{
|
|
34
|
-
return typed;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
_ = ObjectsByTag.Remove(tag);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
GameObject gameObject = GameObject.FindGameObjectWithTag(tag);
|
|
41
|
-
if (gameObject == null)
|
|
42
|
-
{
|
|
43
|
-
if (log)
|
|
44
|
-
{
|
|
45
|
-
component.LogWarn("Could not find {0}.", tag);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return default;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (gameObject.TryGetComponent(out T instance))
|
|
52
|
-
{
|
|
53
|
-
ObjectsByTag[tag] = instance;
|
|
54
|
-
return instance;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (log)
|
|
58
|
-
{
|
|
59
|
-
component.LogWarn(
|
|
60
|
-
"Failed to find {0} on {1} (name: {2}), id [{3}].",
|
|
61
|
-
typeof(T).Name,
|
|
62
|
-
tag,
|
|
63
|
-
gameObject.name,
|
|
64
|
-
gameObject.GetInstanceID()
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return default;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
public static T Find<T>(string tag, bool log = true)
|
|
72
|
-
where T : MonoBehaviour
|
|
73
|
-
{
|
|
74
|
-
if (ObjectsByTag.TryGetValue(tag, out Object value))
|
|
75
|
-
{
|
|
76
|
-
if (value != null && value is T typed)
|
|
77
|
-
{
|
|
78
|
-
return typed;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
_ = ObjectsByTag.Remove(tag);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
GameObject gameObject = GameObject.FindGameObjectWithTag(tag);
|
|
85
|
-
if (gameObject == null)
|
|
86
|
-
{
|
|
87
|
-
if (log)
|
|
88
|
-
{
|
|
89
|
-
LogObject.Log($"Could not find {tag}.");
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return default;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (gameObject.TryGetComponent(out T instance))
|
|
96
|
-
{
|
|
97
|
-
ObjectsByTag[tag] = instance;
|
|
98
|
-
return instance;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (log)
|
|
102
|
-
{
|
|
103
|
-
LogObject.Log($"Failed to find {typeof(T).Name} on {tag}");
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return default;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
public static void SetInstance<T>(string tag, T instance)
|
|
110
|
-
where T : MonoBehaviour
|
|
111
|
-
{
|
|
112
|
-
ObjectsByTag[tag] = instance;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
public static void ClearInstance<T>(string tag, T instance)
|
|
116
|
-
where T : MonoBehaviour
|
|
117
|
-
{
|
|
118
|
-
if (ObjectsByTag.TryGetValue(tag, out Object existing) && existing == instance)
|
|
119
|
-
{
|
|
120
|
-
_ = ObjectsByTag.Remove(tag);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
public static bool HasComponent<T>(this Object unityObject)
|
|
125
|
-
where T : Object
|
|
126
|
-
{
|
|
127
|
-
return unityObject switch
|
|
128
|
-
{
|
|
129
|
-
GameObject go => go.HasComponent<T>(),
|
|
130
|
-
Component component => component.HasComponent<T>(),
|
|
131
|
-
_ => false,
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
public static bool HasComponent<T>(this Component component)
|
|
136
|
-
where T : Object
|
|
137
|
-
{
|
|
138
|
-
return component.TryGetComponent<T>(out _);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
public static bool HasComponent<T>(this GameObject gameObject)
|
|
142
|
-
where T : Object
|
|
143
|
-
{
|
|
144
|
-
return gameObject.TryGetComponent<T>(out _);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
public static bool HasComponent(this GameObject gameObject, Type type)
|
|
148
|
-
{
|
|
149
|
-
return gameObject.TryGetComponent(type, out _);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
public static void LogNotAssigned(this Object component, string name)
|
|
153
|
-
{
|
|
154
|
-
component.LogWarn("{0} not found.", name);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
public static IEnumerable<GameObject> IterateOverChildGameObjects(
|
|
158
|
-
this GameObject gameObject
|
|
159
|
-
)
|
|
160
|
-
{
|
|
161
|
-
for (int i = 0; i < gameObject.transform.childCount; i++)
|
|
162
|
-
{
|
|
163
|
-
yield return gameObject.transform.GetChild(i).gameObject;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
public static IEnumerable<GameObject> IterateOverChildGameObjectsRecursively(
|
|
168
|
-
this GameObject gameObject
|
|
169
|
-
)
|
|
170
|
-
{
|
|
171
|
-
for (int i = 0; i < gameObject.transform.childCount; i++)
|
|
172
|
-
{
|
|
173
|
-
GameObject child = gameObject.transform.GetChild(i).gameObject;
|
|
174
|
-
yield return child;
|
|
175
|
-
foreach (GameObject go in child.IterateOverChildGameObjectsRecursively())
|
|
176
|
-
{
|
|
177
|
-
yield return go;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
public static IEnumerable<GameObject> IterateOverChildGameObjectsRecursivelyIncludingSelf(
|
|
183
|
-
this GameObject gameObject
|
|
184
|
-
)
|
|
185
|
-
{
|
|
186
|
-
yield return gameObject;
|
|
187
|
-
|
|
188
|
-
for (int i = 0; i < gameObject.transform.childCount; ++i)
|
|
189
|
-
{
|
|
190
|
-
GameObject child = gameObject.transform.GetChild(i).gameObject;
|
|
191
|
-
foreach (
|
|
192
|
-
GameObject c in child.IterateOverChildGameObjectsRecursivelyIncludingSelf()
|
|
193
|
-
)
|
|
194
|
-
{
|
|
195
|
-
yield return c;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
public static IEnumerable<GameObject> IterateOverParentGameObjects(
|
|
201
|
-
this GameObject gameObject
|
|
202
|
-
)
|
|
203
|
-
{
|
|
204
|
-
Transform currentTransform = gameObject.transform.parent;
|
|
205
|
-
while (currentTransform != null)
|
|
206
|
-
{
|
|
207
|
-
yield return currentTransform.gameObject;
|
|
208
|
-
currentTransform = currentTransform.parent;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
public static IEnumerable<GameObject> IterateOverParentGameObjectsRecursivelyIncludingSelf(
|
|
213
|
-
this GameObject gameObject
|
|
214
|
-
)
|
|
215
|
-
{
|
|
216
|
-
yield return gameObject;
|
|
217
|
-
|
|
218
|
-
foreach (GameObject parent in IterateOverParentGameObjects(gameObject))
|
|
219
|
-
{
|
|
220
|
-
yield return parent;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
public static void EnableRecursively<T>(
|
|
225
|
-
this Component component,
|
|
226
|
-
bool enabled,
|
|
227
|
-
Func<T, bool> exclude = null
|
|
228
|
-
)
|
|
229
|
-
where T : Behaviour
|
|
230
|
-
{
|
|
231
|
-
if (component == null)
|
|
232
|
-
{
|
|
233
|
-
return;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
foreach (T behaviour in component.GetComponents<T>())
|
|
237
|
-
{
|
|
238
|
-
if (behaviour != null && (exclude?.Invoke(behaviour) ?? true))
|
|
239
|
-
{
|
|
240
|
-
behaviour.enabled = enabled;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
Transform transform = (component as Transform) ?? component.transform;
|
|
245
|
-
if (transform == null)
|
|
246
|
-
{
|
|
247
|
-
return;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
for (int i = 0; i < transform.childCount; ++i)
|
|
251
|
-
{
|
|
252
|
-
Transform child = transform.GetChild(i);
|
|
253
|
-
EnableRecursively<T>(child, enabled);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
public static void EnableRendererRecursively<T>(
|
|
258
|
-
this Component component,
|
|
259
|
-
bool enabled,
|
|
260
|
-
Func<T, bool> exclude = null
|
|
261
|
-
)
|
|
262
|
-
where T : Renderer
|
|
263
|
-
{
|
|
264
|
-
if (component == null)
|
|
265
|
-
{
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
T behavior = component as T ?? component.GetComponent<T>();
|
|
270
|
-
if (behavior != null && (exclude?.Invoke(behavior) ?? true))
|
|
271
|
-
{
|
|
272
|
-
behavior.enabled = enabled;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
Transform transform = (component as Transform) ?? component.transform;
|
|
276
|
-
if (transform == null)
|
|
277
|
-
{
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
for (int i = 0; i < transform.childCount; ++i)
|
|
282
|
-
{
|
|
283
|
-
Transform child = transform.GetChild(i);
|
|
284
|
-
EnableRendererRecursively<T>(child, enabled);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
public static IEnumerable<T> IterateOverAllChildComponentsRecursively<T>(
|
|
289
|
-
this Component component
|
|
290
|
-
)
|
|
291
|
-
{
|
|
292
|
-
if (component == null)
|
|
293
|
-
{
|
|
294
|
-
yield break;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
foreach (T c in component.gameObject.GetComponents<T>())
|
|
298
|
-
{
|
|
299
|
-
yield return c;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
for (int i = 0; i < component.transform.childCount; ++i)
|
|
303
|
-
{
|
|
304
|
-
Transform child = component.transform.GetChild(i);
|
|
305
|
-
|
|
306
|
-
foreach (T c in child.IterateOverAllChildComponentsRecursively<T>())
|
|
307
|
-
{
|
|
308
|
-
yield return c;
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
public static IEnumerable<Transform> IterateOverAllChildren(this Component component)
|
|
314
|
-
{
|
|
315
|
-
if (component == null)
|
|
316
|
-
{
|
|
317
|
-
yield break;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
for (int i = 0; i < component.transform.childCount; ++i)
|
|
321
|
-
{
|
|
322
|
-
yield return component.transform.GetChild(i);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
public static IEnumerable<Transform> IterateOverAllParents(this Component component)
|
|
327
|
-
{
|
|
328
|
-
if (component == null)
|
|
329
|
-
{
|
|
330
|
-
yield break;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
Transform transform = component.transform;
|
|
334
|
-
while (transform.parent != null)
|
|
335
|
-
{
|
|
336
|
-
yield return transform.parent;
|
|
337
|
-
transform = transform.parent;
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
public static IEnumerable<Transform> IterateOverAllParentsIncludingSelf(
|
|
342
|
-
this Component component
|
|
343
|
-
)
|
|
344
|
-
{
|
|
345
|
-
if (component == null)
|
|
346
|
-
{
|
|
347
|
-
yield break;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
Transform transform = component.transform;
|
|
351
|
-
while (transform != null)
|
|
352
|
-
{
|
|
353
|
-
yield return transform;
|
|
354
|
-
transform = transform.parent;
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
public static IEnumerable<Transform> IterateOverAllChildrenRecursively(
|
|
359
|
-
this Component component
|
|
360
|
-
)
|
|
361
|
-
{
|
|
362
|
-
if (component == null)
|
|
363
|
-
{
|
|
364
|
-
yield break;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
for (int i = 0; i < component.transform.childCount; ++i)
|
|
368
|
-
{
|
|
369
|
-
Transform childTransform = component.transform.GetChild(i);
|
|
370
|
-
yield return childTransform;
|
|
371
|
-
foreach (
|
|
372
|
-
Transform childChildTransform in childTransform.IterateOverAllChildrenRecursively()
|
|
373
|
-
)
|
|
374
|
-
{
|
|
375
|
-
yield return childChildTransform;
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
public static bool IsLeft(Vector2 a, Vector2 b, Vector2 point)
|
|
381
|
-
{
|
|
382
|
-
// http://alienryderflex.com/point_left_of_ray/
|
|
383
|
-
|
|
384
|
-
//check which side of line AB the point P is on
|
|
385
|
-
if ((b.x - a.x) * (point.y - a.y) - (point.x - a.x) * (b.y - a.y) > 0)
|
|
386
|
-
{
|
|
387
|
-
return false;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
return true;
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
public static void DestroyAllChildrenGameObjects(this GameObject gameObject)
|
|
394
|
-
{
|
|
395
|
-
if (Application.isEditor)
|
|
396
|
-
{
|
|
397
|
-
EditorDestroyAllChildrenGameObjects(gameObject);
|
|
398
|
-
}
|
|
399
|
-
else
|
|
400
|
-
{
|
|
401
|
-
PlayDestroyAllChildrenGameObjects(gameObject);
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
public static void DestroyAllComponentsOfType<T>(this GameObject gameObject)
|
|
406
|
-
where T : Component
|
|
407
|
-
{
|
|
408
|
-
foreach (T component in gameObject.GetComponents<T>())
|
|
409
|
-
{
|
|
410
|
-
SmartDestroy(component);
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
public static void SmartDestroy(this Object obj, float? afterTime = null)
|
|
415
|
-
{
|
|
416
|
-
if (Application.isEditor && !Application.isPlaying)
|
|
417
|
-
{
|
|
418
|
-
Object.DestroyImmediate(obj);
|
|
419
|
-
}
|
|
420
|
-
else
|
|
421
|
-
{
|
|
422
|
-
if (afterTime.HasValue)
|
|
423
|
-
{
|
|
424
|
-
Object.Destroy(obj, afterTime.Value);
|
|
425
|
-
}
|
|
426
|
-
else
|
|
427
|
-
{
|
|
428
|
-
Object.Destroy(obj);
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
public static void DestroyAllChildrenGameObjectsImmediatelyConditionally(
|
|
434
|
-
this GameObject gameObject,
|
|
435
|
-
Func<GameObject, bool> acceptancePredicate
|
|
436
|
-
)
|
|
437
|
-
{
|
|
438
|
-
InternalDestroyAllChildrenGameObjects(
|
|
439
|
-
gameObject,
|
|
440
|
-
toDestroy =>
|
|
441
|
-
{
|
|
442
|
-
if (!acceptancePredicate(toDestroy))
|
|
443
|
-
{
|
|
444
|
-
return;
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
Object.DestroyImmediate(toDestroy);
|
|
448
|
-
}
|
|
449
|
-
);
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
public static void DestroyAllChildGameObjectsConditionally(
|
|
453
|
-
this GameObject gameObject,
|
|
454
|
-
Func<GameObject, bool> acceptancePredicate
|
|
455
|
-
)
|
|
456
|
-
{
|
|
457
|
-
InternalDestroyAllChildrenGameObjects(
|
|
458
|
-
gameObject,
|
|
459
|
-
toDestroy =>
|
|
460
|
-
{
|
|
461
|
-
if (!acceptancePredicate(toDestroy))
|
|
462
|
-
{
|
|
463
|
-
return;
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
toDestroy.Destroy();
|
|
467
|
-
}
|
|
468
|
-
);
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
public static void DestroyAllChildrenGameObjectsImmediately(this GameObject gameObject) =>
|
|
472
|
-
InternalDestroyAllChildrenGameObjects(gameObject, Object.DestroyImmediate);
|
|
473
|
-
|
|
474
|
-
public static void PlayDestroyAllChildrenGameObjects(this GameObject gameObject) =>
|
|
475
|
-
InternalDestroyAllChildrenGameObjects(gameObject, go => go.Destroy());
|
|
476
|
-
|
|
477
|
-
public static void EditorDestroyAllChildrenGameObjects(this GameObject gameObject) =>
|
|
478
|
-
InternalDestroyAllChildrenGameObjects(gameObject, go => go.Destroy());
|
|
479
|
-
|
|
480
|
-
private static void InternalDestroyAllChildrenGameObjects(
|
|
481
|
-
this GameObject gameObject,
|
|
482
|
-
Action<GameObject> destroyFunction
|
|
483
|
-
)
|
|
484
|
-
{
|
|
485
|
-
for (int i = gameObject.transform.childCount - 1; 0 <= i; --i)
|
|
486
|
-
{
|
|
487
|
-
destroyFunction(gameObject.transform.GetChild(i).gameObject);
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
public static bool IsPrefab(this GameObject gameObject)
|
|
492
|
-
{
|
|
493
|
-
Scene scene = gameObject.scene;
|
|
494
|
-
#if UNITY_EDITOR
|
|
495
|
-
if (scene.rootCount == 1 && string.Equals(scene.name, gameObject.name))
|
|
496
|
-
{
|
|
497
|
-
return true;
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
return PrefabUtility.GetPrefabAssetType(gameObject) switch
|
|
501
|
-
{
|
|
502
|
-
PrefabAssetType.NotAPrefab => false,
|
|
503
|
-
PrefabAssetType.MissingAsset => scene.rootCount == 0,
|
|
504
|
-
_ => true,
|
|
505
|
-
};
|
|
506
|
-
#else
|
|
507
|
-
return scene.rootCount == 0;
|
|
508
|
-
#endif
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
public static bool IsPrefab(this Component component)
|
|
512
|
-
{
|
|
513
|
-
return IsPrefab(component.gameObject);
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
public static Vector2 RadianToVector2(float radian)
|
|
517
|
-
{
|
|
518
|
-
return new Vector2(Mathf.Cos(radian), Mathf.Sin(radian));
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
public static Vector2 DegreeToVector2(float degree)
|
|
522
|
-
{
|
|
523
|
-
return RadianToVector2(degree * Mathf.Deg2Rad);
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
public static T GetOrAddComponent<T>(this GameObject unityObject)
|
|
527
|
-
where T : Component
|
|
528
|
-
{
|
|
529
|
-
if (!unityObject.TryGetComponent(out T instance))
|
|
530
|
-
{
|
|
531
|
-
instance = unityObject.AddComponent<T>();
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
return instance;
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
public static Component GetOrAddComponent(this GameObject unityObject, Type componentType)
|
|
538
|
-
{
|
|
539
|
-
if (!unityObject.TryGetComponent(componentType, out Component instance))
|
|
540
|
-
{
|
|
541
|
-
instance = unityObject.AddComponent(componentType);
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
return instance;
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
public static void ModifyAndSavePrefab(GameObject prefab, Action<GameObject> modifyAction)
|
|
548
|
-
{
|
|
549
|
-
if (prefab == null)
|
|
550
|
-
{
|
|
551
|
-
return;
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
#if UNITY_EDITOR
|
|
555
|
-
if (PrefabUtility.IsPartOfPrefabAsset(prefab))
|
|
556
|
-
{
|
|
557
|
-
string assetPath = AssetDatabase.GetAssetPath(prefab);
|
|
558
|
-
GameObject content = PrefabUtility.LoadPrefabContents(assetPath);
|
|
559
|
-
|
|
560
|
-
if (content == null)
|
|
561
|
-
{
|
|
562
|
-
Debug.LogError($"Unable to load {prefab} as a prefab");
|
|
563
|
-
return;
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
modifyAction(content);
|
|
567
|
-
_ = PrefabUtility.SaveAsPrefabAsset(content, assetPath);
|
|
568
|
-
PrefabUtility.UnloadPrefabContents(content);
|
|
569
|
-
}
|
|
570
|
-
else
|
|
571
|
-
{
|
|
572
|
-
modifyAction(prefab);
|
|
573
|
-
PrefabStage stage = PrefabStageUtility.GetPrefabStage(prefab);
|
|
574
|
-
if (stage)
|
|
575
|
-
{
|
|
576
|
-
_ = EditorSceneManager.MarkSceneDirty(stage.scene);
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
#endif
|
|
580
|
-
}
|
|
581
|
-
|
|
582
27
|
// https://gamedevelopment.tutsplus.com/tutorials/unity-solution-for-hitting-moving-targets--cms-29633
|
|
583
28
|
public static Vector2 PredictCurrentTarget(
|
|
584
29
|
this GameObject currentTarget,
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
namespace UnityHelpers.Core.Helper
|
|
2
|
+
{
|
|
3
|
+
using Extension;
|
|
4
|
+
using UnityEngine;
|
|
5
|
+
|
|
6
|
+
public static partial class Helpers
|
|
7
|
+
{
|
|
8
|
+
public static void LogNotAssigned(this Object component, string name)
|
|
9
|
+
{
|
|
10
|
+
component.LogWarn("{0} not found.", name);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
namespace UnityHelpers.Core.Helper
|
|
2
|
+
{
|
|
3
|
+
using UnityEngine;
|
|
4
|
+
|
|
5
|
+
public static partial class Helpers
|
|
6
|
+
{
|
|
7
|
+
public static bool IsLeft(Vector2 a, Vector2 b, Vector2 point)
|
|
8
|
+
{
|
|
9
|
+
// http://alienryderflex.com/point_left_of_ray/
|
|
10
|
+
|
|
11
|
+
//check which side of line AB the point P is on
|
|
12
|
+
if ((b.x - a.x) * (point.y - a.y) - (point.x - a.x) * (b.y - a.y) > 0)
|
|
13
|
+
{
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public static Vector2 RadianToVector2(float radian)
|
|
21
|
+
{
|
|
22
|
+
return new Vector2(Mathf.Cos(radian), Mathf.Sin(radian));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public static Vector2 DegreeToVector2(float degree)
|
|
26
|
+
{
|
|
27
|
+
return RadianToVector2(degree * Mathf.Deg2Rad);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|