com.wallstop-studios.unity-helpers 1.0.0-rc4
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/.gitattributes +63 -0
- package/CHANGELOG.md +0 -0
- package/CHANGELOG.md.meta +7 -0
- package/Editor/AnimationCreator.cs +218 -0
- package/Editor/AnimationCreator.cs.meta +11 -0
- package/Editor/AnimationEventEditor.cs +742 -0
- package/Editor/AnimationEventEditor.cs.meta +11 -0
- package/Editor/PrefabCheckWizard.cs +140 -0
- package/Editor/PrefabCheckWizard.cs.meta +11 -0
- package/Editor/WallstopStudios.UnityHelpers.Editor.asmdef +16 -0
- package/Editor/WallstopStudios.UnityHelpers.Editor.asmdef.meta +7 -0
- package/Editor.meta +8 -0
- package/LICENSE +21 -0
- package/LICENSE.md +7 -0
- package/LICENSE.md.meta +7 -0
- package/LICENSE.meta +7 -0
- package/README.md +2 -0
- package/README.md.meta +7 -0
- package/Runtime/Core/Attributes/AnimationEventAttribute.cs +102 -0
- package/Runtime/Core/Attributes/AnimationEventAttribute.cs.meta +11 -0
- package/Runtime/Core/Attributes/AutomaticallyFindAttribute.cs +43 -0
- package/Runtime/Core/Attributes/AutomaticallyFindAttribute.cs.meta +11 -0
- package/Runtime/Core/Attributes/ChildComponentAttribute.cs +85 -0
- package/Runtime/Core/Attributes/ChildComponentAttribute.cs.meta +11 -0
- package/Runtime/Core/Attributes/KSerializableAttribute.cs +23 -0
- package/Runtime/Core/Attributes/KSerializableAttribute.cs.meta +11 -0
- package/Runtime/Core/Attributes/NotNullAttribute.cs +33 -0
- package/Runtime/Core/Attributes/NotNullAttribute.cs.meta +11 -0
- package/Runtime/Core/Attributes/ParentComponent.cs +85 -0
- package/Runtime/Core/Attributes/ParentComponent.cs.meta +11 -0
- package/Runtime/Core/Attributes/ReadOnlyAttribute.cs +8 -0
- package/Runtime/Core/Attributes/ReadOnlyAttribute.cs.meta +11 -0
- package/Runtime/Core/Attributes/RelationalComponentExtensions.cs +14 -0
- package/Runtime/Core/Attributes/RelationalComponentExtensions.cs.meta +11 -0
- package/Runtime/Core/Attributes/SiblingComponentAttribute.cs +89 -0
- package/Runtime/Core/Attributes/SiblingComponentAttribute.cs.meta +11 -0
- package/Runtime/Core/Attributes/ValidateAssignmentAttribute.cs +66 -0
- package/Runtime/Core/Attributes/ValidateAssignmentAttribute.cs.meta +11 -0
- package/Runtime/Core/Attributes.meta +8 -0
- package/Runtime/Core/DataStructure/Adapters/FastVector2Int.cs +91 -0
- package/Runtime/Core/DataStructure/Adapters/FastVector2Int.cs.meta +11 -0
- package/Runtime/Core/DataStructure/Adapters/FastVector3Int.cs +180 -0
- package/Runtime/Core/DataStructure/Adapters/FastVector3Int.cs.meta +11 -0
- package/Runtime/Core/DataStructure/Adapters/KGuid.cs +274 -0
- package/Runtime/Core/DataStructure/Adapters/KGuid.cs.meta +11 -0
- package/Runtime/Core/DataStructure/Adapters/KVector2.cs +79 -0
- package/Runtime/Core/DataStructure/Adapters/KVector2.cs.meta +11 -0
- package/Runtime/Core/DataStructure/Adapters.meta +8 -0
- package/Runtime/Core/DataStructure/Circle.cs +50 -0
- package/Runtime/Core/DataStructure/Circle.cs.meta +11 -0
- package/Runtime/Core/DataStructure/CyclicBuffer.cs +130 -0
- package/Runtime/Core/DataStructure/CyclicBuffer.cs.meta +11 -0
- package/Runtime/Core/DataStructure/ISpatialTree.cs +58 -0
- package/Runtime/Core/DataStructure/ISpatialTree.cs.meta +11 -0
- package/Runtime/Core/DataStructure/KDTree.cs +186 -0
- package/Runtime/Core/DataStructure/KDTree.cs.meta +11 -0
- package/Runtime/Core/DataStructure/QuadTree.cs +184 -0
- package/Runtime/Core/DataStructure/QuadTree.cs.meta +11 -0
- package/Runtime/Core/DataStructure/RTree.cs +247 -0
- package/Runtime/Core/DataStructure/RTree.cs.meta +11 -0
- package/Runtime/Core/DataStructure/StringWrapper.cs +90 -0
- package/Runtime/Core/DataStructure/StringWrapper.cs.meta +11 -0
- package/Runtime/Core/DataStructure/TimedCache.cs +50 -0
- package/Runtime/Core/DataStructure/TimedCache.cs.meta +11 -0
- package/Runtime/Core/DataStructure.meta +8 -0
- package/Runtime/Core/Extension/AnimatorExtensions.cs +25 -0
- package/Runtime/Core/Extension/AnimatorExtensions.cs.meta +11 -0
- package/Runtime/Core/Extension/CircleExtensions.cs +25 -0
- package/Runtime/Core/Extension/CircleExtensions.cs.meta +11 -0
- package/Runtime/Core/Extension/ColorExtensions.cs +72 -0
- package/Runtime/Core/Extension/ColorExtensions.cs.meta +11 -0
- package/Runtime/Core/Extension/DictionaryExtensions.cs +173 -0
- package/Runtime/Core/Extension/DictionaryExtensions.cs.meta +11 -0
- package/Runtime/Core/Extension/DirectionExtensions.cs +210 -0
- package/Runtime/Core/Extension/DirectionExtensions.cs.meta +11 -0
- package/Runtime/Core/Extension/HashSetExtensions.cs +12 -0
- package/Runtime/Core/Extension/HashSetExtensions.cs.meta +11 -0
- package/Runtime/Core/Extension/IEnumerableExtensions.cs +109 -0
- package/Runtime/Core/Extension/IEnumerableExtensions.cs.meta +11 -0
- package/Runtime/Core/Extension/IListExtensions.cs +49 -0
- package/Runtime/Core/Extension/IListExtensions.cs.meta +11 -0
- package/Runtime/Core/Extension/LoggingExtensions.cs +196 -0
- package/Runtime/Core/Extension/LoggingExtensions.cs.meta +11 -0
- package/Runtime/Core/Extension/RandomExtensions.cs +110 -0
- package/Runtime/Core/Extension/RandomExtensions.cs.meta +11 -0
- package/Runtime/Core/Extension/StringExtensions.cs +76 -0
- package/Runtime/Core/Extension/StringExtensions.cs.meta +11 -0
- package/Runtime/Core/Extension/UnityExtensions.cs +1409 -0
- package/Runtime/Core/Extension/UnityExtensions.cs.meta +11 -0
- package/Runtime/Core/Extension.meta +8 -0
- package/Runtime/Core/Helper/AssignUtilities.cs +14 -0
- package/Runtime/Core/Helper/AssignUtilities.cs.meta +11 -0
- package/Runtime/Core/Helper/Enumerables.cs +17 -0
- package/Runtime/Core/Helper/Enumerables.cs.meta +11 -0
- package/Runtime/Core/Helper/Geometry.cs +26 -0
- package/Runtime/Core/Helper/Geometry.cs.meta +11 -0
- package/Runtime/Core/Helper/Helpers.cs +1092 -0
- package/Runtime/Core/Helper/Helpers.cs.meta +11 -0
- package/Runtime/Core/Helper/IterationHelpers.cs +32 -0
- package/Runtime/Core/Helper/IterationHelpers.cs.meta +11 -0
- package/Runtime/Core/Helper/LifetimeHelpers.cs +12 -0
- package/Runtime/Core/Helper/LifetimeHelpers.cs.meta +11 -0
- package/Runtime/Core/Helper/Objects.cs +447 -0
- package/Runtime/Core/Helper/Objects.cs.meta +11 -0
- package/Runtime/Core/Helper/SpriteHelpers.cs +53 -0
- package/Runtime/Core/Helper/SpriteHelpers.cs.meta +11 -0
- package/Runtime/Core/Helper/StringInList.cs +31 -0
- package/Runtime/Core/Helper/StringInList.cs.meta +11 -0
- package/Runtime/Core/Helper/WallMath.cs +75 -0
- package/Runtime/Core/Helper/WallMath.cs.meta +11 -0
- package/Runtime/Core/Helper.meta +8 -0
- package/Runtime/Core/Math/Line.cs +51 -0
- package/Runtime/Core/Math/Line.cs.meta +11 -0
- package/Runtime/Core/Math/Parabola.cs +44 -0
- package/Runtime/Core/Math/Parabola.cs.meta +11 -0
- package/Runtime/Core/Math/PointPolygonCheck.cs +25 -0
- package/Runtime/Core/Math/PointPolygonCheck.cs.meta +11 -0
- package/Runtime/Core/Math/Range.cs +56 -0
- package/Runtime/Core/Math/Range.cs.meta +11 -0
- package/Runtime/Core/Math/XXHash.cs +308 -0
- package/Runtime/Core/Math/XXHash.cs.meta +11 -0
- package/Runtime/Core/Math.meta +8 -0
- package/Runtime/Core/Model/Direction.cs +26 -0
- package/Runtime/Core/Model/Direction.cs.meta +11 -0
- package/Runtime/Core/Model.meta +8 -0
- package/Runtime/Core/OneOf/FastOneOf.cs +145 -0
- package/Runtime/Core/OneOf/FastOneOf.cs.meta +11 -0
- package/Runtime/Core/OneOf/None.cs +6 -0
- package/Runtime/Core/OneOf/None.cs.meta +11 -0
- package/Runtime/Core/OneOf.meta +8 -0
- package/Runtime/Core/Random/AbstractRandom.cs +537 -0
- package/Runtime/Core/Random/AbstractRandom.cs.meta +11 -0
- package/Runtime/Core/Random/IRandom.cs +141 -0
- package/Runtime/Core/Random/IRandom.cs.meta +11 -0
- package/Runtime/Core/Random/NativePcgRandom.cs +97 -0
- package/Runtime/Core/Random/NativePcgRandom.cs.meta +11 -0
- package/Runtime/Core/Random/PcgRandom.cs +142 -0
- package/Runtime/Core/Random/PcgRandom.cs.meta +11 -0
- package/Runtime/Core/Random/RandomState.cs +92 -0
- package/Runtime/Core/Random/RandomState.cs.meta +11 -0
- package/Runtime/Core/Random/RandomUtilities.cs +26 -0
- package/Runtime/Core/Random/RandomUtilities.cs.meta +11 -0
- package/Runtime/Core/Random/SquirrelRandom.cs +82 -0
- package/Runtime/Core/Random/SquirrelRandom.cs.meta +11 -0
- package/Runtime/Core/Random/SystemRandom.cs +110 -0
- package/Runtime/Core/Random/SystemRandom.cs.meta +11 -0
- package/Runtime/Core/Random/ThreadLocalRandom.cs +11 -0
- package/Runtime/Core/Random/ThreadLocalRandom.cs.meta +11 -0
- package/Runtime/Core/Random/UnityRandom.cs +24 -0
- package/Runtime/Core/Random/UnityRandom.cs.meta +11 -0
- package/Runtime/Core/Random/XorShiftRandom.cs +45 -0
- package/Runtime/Core/Random/XorShiftRandom.cs.meta +11 -0
- package/Runtime/Core/Random.meta +8 -0
- package/Runtime/Core/Serialization/JsonConverters/Vector2Converter.cs +32 -0
- package/Runtime/Core/Serialization/JsonConverters/Vector2Converter.cs.meta +11 -0
- package/Runtime/Core/Serialization/JsonConverters/Vector3Converter.cs +32 -0
- package/Runtime/Core/Serialization/JsonConverters/Vector3Converter.cs.meta +11 -0
- package/Runtime/Core/Serialization/JsonConverters.meta +8 -0
- package/Runtime/Core/Serialization/Serializer.cs +125 -0
- package/Runtime/Core/Serialization/Serializer.cs.meta +11 -0
- package/Runtime/Core/Serialization.meta +8 -0
- package/Runtime/Core/Threading/SingleThreadedThreadPool.cs +102 -0
- package/Runtime/Core/Threading/SingleThreadedThreadPool.cs.meta +11 -0
- package/Runtime/Core/Threading.meta +8 -0
- package/Runtime/Core.meta +8 -0
- package/Runtime/Protobuf-Net/System.Buffers.dll +0 -0
- package/Runtime/Protobuf-Net/System.Buffers.dll.meta +33 -0
- package/Runtime/Protobuf-Net/System.Collections.Immutable.dll +0 -0
- package/Runtime/Protobuf-Net/System.Collections.Immutable.dll.meta +33 -0
- package/Runtime/Protobuf-Net/System.Numerics.Vectors.dll +0 -0
- package/Runtime/Protobuf-Net/System.Numerics.Vectors.dll.meta +33 -0
- package/Runtime/Protobuf-Net/System.Runtime.CompilerServices.Unsafe.dll +0 -0
- package/Runtime/Protobuf-Net/System.Runtime.CompilerServices.Unsafe.dll.meta +33 -0
- package/Runtime/Protobuf-Net/protobuf-net.Core.dll +0 -0
- package/Runtime/Protobuf-Net/protobuf-net.Core.dll.meta +33 -0
- package/Runtime/Protobuf-Net/protobuf-net.dll +0 -0
- package/Runtime/Protobuf-Net/protobuf-net.dll.meta +33 -0
- package/Runtime/Protobuf-Net.meta +8 -0
- package/Runtime/Utils/AnimationEventEqualityComparer.cs +149 -0
- package/Runtime/Utils/AnimationEventEqualityComparer.cs.meta +11 -0
- package/Runtime/Utils/AnimatorEnumStateMachine.cs +80 -0
- package/Runtime/Utils/AnimatorEnumStateMachine.cs.meta +11 -0
- package/Runtime/Utils/Buffers.cs +32 -0
- package/Runtime/Utils/Buffers.cs.meta +11 -0
- package/Runtime/Utils/CircleLineRenderer.cs +122 -0
- package/Runtime/Utils/CircleLineRenderer.cs.meta +11 -0
- package/Runtime/Utils/Oscillator.cs +25 -0
- package/Runtime/Utils/Oscillator.cs.meta +11 -0
- package/Runtime/Utils/SetTextureImportData.cs +67 -0
- package/Runtime/Utils/SetTextureImportData.cs.meta +11 -0
- package/Runtime/Utils.meta +8 -0
- package/Runtime/WallstopStudios.UnityHelpers.asmdef +14 -0
- package/Runtime/WallstopStudios.UnityHelpers.asmdef.meta +7 -0
- package/Runtime.meta +8 -0
- package/Tests/Editor/WallstopStudios.UnityHelpers.Tests.Editor.asmdef +17 -0
- package/Tests/Editor/WallstopStudios.UnityHelpers.Tests.Editor.asmdef.meta +7 -0
- package/Tests/Editor.meta +8 -0
- package/Tests/Runtime/DataStructures/BalancedKDTreeTests.cs +14 -0
- package/Tests/Runtime/DataStructures/BalancedKDTreeTests.cs.meta +11 -0
- package/Tests/Runtime/DataStructures/QuadTreeTests.cs +14 -0
- package/Tests/Runtime/DataStructures/QuadTreeTests.cs.meta +11 -0
- package/Tests/Runtime/DataStructures/SpatialTreeTests.cs +106 -0
- package/Tests/Runtime/DataStructures/SpatialTreeTests.cs.meta +11 -0
- package/Tests/Runtime/DataStructures/UnbalancedKDTreeTests.cs +14 -0
- package/Tests/Runtime/DataStructures/UnbalancedKDTreeTests.cs.meta +11 -0
- package/Tests/Runtime/DataStructures.meta +8 -0
- package/Tests/Runtime/Performance/KDTreePerformanceTests.cs +14 -0
- package/Tests/Runtime/Performance/KDTreePerformanceTests.cs.meta +11 -0
- package/Tests/Runtime/Performance/QuadTreePerformanceTests.cs +14 -0
- package/Tests/Runtime/Performance/QuadTreePerformanceTests.cs.meta +11 -0
- package/Tests/Runtime/Performance/RandomPerformanceTests.cs +76 -0
- package/Tests/Runtime/Performance/RandomPerformanceTests.cs.meta +11 -0
- package/Tests/Runtime/Performance/SpatialTreePerformanceTest.cs +141 -0
- package/Tests/Runtime/Performance/SpatialTreePerformanceTest.cs.meta +11 -0
- package/Tests/Runtime/Performance/UnbalancedKDTreeTests.cs +14 -0
- package/Tests/Runtime/Performance/UnbalancedKDTreeTests.cs.meta +11 -0
- package/Tests/Runtime/Performance.meta +8 -0
- package/Tests/Runtime/Random/PcgRandomTests.cs +9 -0
- package/Tests/Runtime/Random/PcgRandomTests.cs.meta +11 -0
- package/Tests/Runtime/Random/RandomTestBase.cs +117 -0
- package/Tests/Runtime/Random/RandomTestBase.cs.meta +11 -0
- package/Tests/Runtime/Random/SquirrelRandomTests.cs +9 -0
- package/Tests/Runtime/Random/SquirrelRandomTests.cs.meta +11 -0
- package/Tests/Runtime/Random/SystemRandomTests.cs +10 -0
- package/Tests/Runtime/Random/SystemRandomTests.cs.meta +11 -0
- package/Tests/Runtime/Random/UnityRandomTests.cs +9 -0
- package/Tests/Runtime/Random/UnityRandomTests.cs.meta +11 -0
- package/Tests/Runtime/Random/XorShiftRandomTests.cs +9 -0
- package/Tests/Runtime/Random/XorShiftRandomTests.cs.meta +11 -0
- package/Tests/Runtime/Random.meta +8 -0
- package/Tests/Runtime/WallstopStudios.UnityHelpers.Tests.Runtime.asmdef +22 -0
- package/Tests/Runtime/WallstopStudios.UnityHelpers.Tests.Runtime.asmdef.meta +7 -0
- package/Tests/Runtime.meta +8 -0
- package/Tests.meta +8 -0
- package/Third Party Notices.md +1 -0
- package/Third Party Notices.md.meta +7 -0
- package/package.json +35 -0
- package/package.json.meta +7 -0
|
@@ -0,0 +1,1092 @@
|
|
|
1
|
+
namespace UnityHelpers.Core.Helper
|
|
2
|
+
{
|
|
3
|
+
using Extension;
|
|
4
|
+
using System;
|
|
5
|
+
using System.Collections;
|
|
6
|
+
using System.Collections.Generic;
|
|
7
|
+
using System.Linq;
|
|
8
|
+
using System.Reflection;
|
|
9
|
+
using DataStructure.Adapters;
|
|
10
|
+
using Random;
|
|
11
|
+
#if UNITY_EDITOR
|
|
12
|
+
using UnityEditor;
|
|
13
|
+
using UnityEditor.SceneManagement;
|
|
14
|
+
#endif
|
|
15
|
+
using UnityEngine;
|
|
16
|
+
using UnityEngine.SceneManagement;
|
|
17
|
+
using Utils;
|
|
18
|
+
using Object = UnityEngine.Object;
|
|
19
|
+
|
|
20
|
+
public static class Helpers
|
|
21
|
+
{
|
|
22
|
+
private static readonly WaitForEndOfFrame WaitForEndOfFrame = new();
|
|
23
|
+
private static readonly Dictionary<Type, MethodInfo> AwakeMethodsByType = new();
|
|
24
|
+
private static readonly Object LogObject = new();
|
|
25
|
+
private static readonly Dictionary<string, Object> ObjectsByTag = new();
|
|
26
|
+
|
|
27
|
+
public static T Find<T>(this Object component, string tag, bool log = true) where T : Object
|
|
28
|
+
{
|
|
29
|
+
if (ObjectsByTag.TryGetValue(tag, out Object value))
|
|
30
|
+
{
|
|
31
|
+
if (value != null && value is T typed)
|
|
32
|
+
{
|
|
33
|
+
return typed;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
_ = ObjectsByTag.Remove(tag);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
GameObject gameObject = GameObject.FindGameObjectWithTag(tag);
|
|
40
|
+
if (gameObject == null)
|
|
41
|
+
{
|
|
42
|
+
if (log)
|
|
43
|
+
{
|
|
44
|
+
component.LogWarn("Could not find {0}.", tag);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return default;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (gameObject.TryGetComponent(out T instance))
|
|
51
|
+
{
|
|
52
|
+
ObjectsByTag[tag] = instance;
|
|
53
|
+
return instance;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (log)
|
|
57
|
+
{
|
|
58
|
+
component.LogWarn("Failed to find {0} on {1} (name: {2}), id [{3}].", typeof(T).Name, tag, gameObject.name, gameObject.GetInstanceID());
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return default;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public static T Find<T>(string tag, bool log = true) where T : MonoBehaviour
|
|
65
|
+
{
|
|
66
|
+
if (ObjectsByTag.TryGetValue(tag, out Object value))
|
|
67
|
+
{
|
|
68
|
+
if (value != null && value is T typed)
|
|
69
|
+
{
|
|
70
|
+
return typed;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
_ = ObjectsByTag.Remove(tag);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
GameObject gameObject = GameObject.FindGameObjectWithTag(tag);
|
|
77
|
+
if (gameObject == null)
|
|
78
|
+
{
|
|
79
|
+
if (log)
|
|
80
|
+
{
|
|
81
|
+
LogObject.Log($"Could not find {tag}.");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return default;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (gameObject.TryGetComponent(out T instance))
|
|
88
|
+
{
|
|
89
|
+
ObjectsByTag[tag] = instance;
|
|
90
|
+
return instance;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (log)
|
|
94
|
+
{
|
|
95
|
+
LogObject.Log($"Failed to find {typeof(T).Name} on {tag}");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return default;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
public static void SetInstance<T>(string tag, T instance) where T : MonoBehaviour
|
|
102
|
+
{
|
|
103
|
+
ObjectsByTag[tag] = instance;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public static void ClearInstance<T>(string tag, T instance) where T : MonoBehaviour
|
|
107
|
+
{
|
|
108
|
+
if (ObjectsByTag.TryGetValue(tag, out Object existing) && existing == instance)
|
|
109
|
+
{
|
|
110
|
+
_ = ObjectsByTag.Remove(tag);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
public static bool HasComponent<T>(this Object unityObject)
|
|
115
|
+
{
|
|
116
|
+
return (unityObject) switch
|
|
117
|
+
{
|
|
118
|
+
GameObject go => go.HasComponent<T>(),
|
|
119
|
+
Component component => component.HasComponent<T>(),
|
|
120
|
+
_ => false
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public static bool HasComponent<T>(this Component component)
|
|
125
|
+
{
|
|
126
|
+
return component.TryGetComponent<T>(out _);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
public static bool HasComponent<T>(this GameObject gameObject)
|
|
130
|
+
{
|
|
131
|
+
return gameObject.TryGetComponent<T>(out _);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
public static bool HasComponent(this GameObject gameObject, Type type)
|
|
135
|
+
{
|
|
136
|
+
return gameObject.TryGetComponent(type, out _);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
public static void LogNotAssigned(this Object component, string name)
|
|
140
|
+
{
|
|
141
|
+
component.LogWarn("{0} not found.", name);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
public static IEnumerable<GameObject> IterateOverChildGameObjects(this GameObject gameObject)
|
|
145
|
+
{
|
|
146
|
+
for (int i = 0; i < gameObject.transform.childCount; i++)
|
|
147
|
+
{
|
|
148
|
+
yield return gameObject.transform.GetChild(i).gameObject;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public static IEnumerable<GameObject> IterateOverChildGameObjectsRecursively(this GameObject gameObject)
|
|
153
|
+
{
|
|
154
|
+
for (int i = 0; i < gameObject.transform.childCount; i++)
|
|
155
|
+
{
|
|
156
|
+
GameObject child = gameObject.transform.GetChild(i).gameObject;
|
|
157
|
+
yield return child;
|
|
158
|
+
foreach (GameObject go in child.IterateOverChildGameObjectsRecursively())
|
|
159
|
+
{
|
|
160
|
+
yield return go;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
public static IEnumerable<GameObject> IterateOverChildGameObjectsRecursivelyIncludingSelf(this GameObject gameObject)
|
|
166
|
+
{
|
|
167
|
+
yield return gameObject;
|
|
168
|
+
|
|
169
|
+
for (int i = 0; i < gameObject.transform.childCount; ++i)
|
|
170
|
+
{
|
|
171
|
+
GameObject child = gameObject.transform.GetChild(i).gameObject;
|
|
172
|
+
foreach (GameObject c in child.IterateOverChildGameObjectsRecursivelyIncludingSelf())
|
|
173
|
+
{
|
|
174
|
+
yield return c;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
public static IEnumerable<GameObject> IterateOverParentGameObjects(this GameObject gameObject)
|
|
180
|
+
{
|
|
181
|
+
Transform currentTransform = gameObject.transform.parent;
|
|
182
|
+
while (currentTransform != null)
|
|
183
|
+
{
|
|
184
|
+
yield return currentTransform.gameObject;
|
|
185
|
+
currentTransform = currentTransform.parent;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
public static IEnumerable<GameObject> IterateOverParentGameObjectsRecursivelyIncludingSelf(this GameObject gameObject)
|
|
190
|
+
{
|
|
191
|
+
yield return gameObject;
|
|
192
|
+
|
|
193
|
+
foreach (GameObject parent in IterateOverParentGameObjects(gameObject))
|
|
194
|
+
{
|
|
195
|
+
yield return parent;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
public static void EnableRecursively<T>(this Component component, bool enabled, Func<T, bool> exclude = null)
|
|
200
|
+
where T : Behaviour
|
|
201
|
+
{
|
|
202
|
+
if (component == null)
|
|
203
|
+
{
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
foreach (T behaviour in component.GetComponents<T>())
|
|
208
|
+
{
|
|
209
|
+
if (behaviour != null && (exclude?.Invoke(behaviour) ?? true))
|
|
210
|
+
{
|
|
211
|
+
behaviour.enabled = enabled;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
Transform transform = (component as Transform) ?? component.transform;
|
|
216
|
+
if (transform == null)
|
|
217
|
+
{
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
for (int i = 0; i < transform.childCount; ++i)
|
|
222
|
+
{
|
|
223
|
+
Transform child = transform.GetChild(i);
|
|
224
|
+
EnableRecursively<T>(child, enabled);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
public static void EnableRendererRecursively<T>(this Component component, bool enabled,
|
|
229
|
+
Func<T, bool> exclude = null) where T : Renderer
|
|
230
|
+
{
|
|
231
|
+
if (component == null)
|
|
232
|
+
{
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
T behavior = component as T ?? component.GetComponent<T>();
|
|
237
|
+
if (behavior != null && (exclude?.Invoke(behavior) ?? true))
|
|
238
|
+
{
|
|
239
|
+
behavior.enabled = enabled;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
Transform transform = (component as Transform) ?? component.transform;
|
|
243
|
+
if (transform == null)
|
|
244
|
+
{
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
for (int i = 0; i < transform.childCount; ++i)
|
|
249
|
+
{
|
|
250
|
+
Transform child = transform.GetChild(i);
|
|
251
|
+
EnableRendererRecursively<T>(child, enabled);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
public static IEnumerable<T> IterateOverAllChildComponentsRecursively<T>(this Component component)
|
|
256
|
+
{
|
|
257
|
+
if (component == null)
|
|
258
|
+
{
|
|
259
|
+
yield break;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
foreach (T c in component.gameObject.GetComponents<T>())
|
|
263
|
+
{
|
|
264
|
+
yield return c;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
for (int i = 0; i < component.transform.childCount; ++i)
|
|
268
|
+
{
|
|
269
|
+
Transform child = component.transform.GetChild(i);
|
|
270
|
+
|
|
271
|
+
foreach (T c in child.IterateOverAllChildComponentsRecursively<T>())
|
|
272
|
+
{
|
|
273
|
+
yield return c;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
public static IEnumerable<Transform> IterateOverAllChildren(this Component component)
|
|
279
|
+
{
|
|
280
|
+
if (component == null)
|
|
281
|
+
{
|
|
282
|
+
yield break;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
for (int i = 0; i < component.transform.childCount; ++i)
|
|
286
|
+
{
|
|
287
|
+
yield return component.transform.GetChild(i);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
public static IEnumerable<Transform> IterateOverAllParents(this Component component)
|
|
292
|
+
{
|
|
293
|
+
if (component == null)
|
|
294
|
+
{
|
|
295
|
+
yield break;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
Transform transform = component.transform;
|
|
299
|
+
while (transform.parent != null)
|
|
300
|
+
{
|
|
301
|
+
yield return transform.parent;
|
|
302
|
+
transform = transform.parent;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
public static IEnumerable<Transform> IterateOverAllParentsIncludingSelf(this Component component)
|
|
307
|
+
{
|
|
308
|
+
if (component == null)
|
|
309
|
+
{
|
|
310
|
+
yield break;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
Transform transform = component.transform;
|
|
314
|
+
while (transform != null)
|
|
315
|
+
{
|
|
316
|
+
yield return transform;
|
|
317
|
+
transform = transform.parent;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
public static IEnumerable<Transform> IterateOverAllChildrenRecursively(this Component component)
|
|
322
|
+
{
|
|
323
|
+
if (component == null)
|
|
324
|
+
{
|
|
325
|
+
yield break;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
for (int i = 0; i < component.transform.childCount; ++i)
|
|
329
|
+
{
|
|
330
|
+
Transform childTransform = component.transform.GetChild(i);
|
|
331
|
+
yield return childTransform;
|
|
332
|
+
foreach (Transform childChildTransform in childTransform.IterateOverAllChildrenRecursively())
|
|
333
|
+
{
|
|
334
|
+
yield return childChildTransform;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
public static bool IsLeft(Vector2 a, Vector2 b, Vector2 point)
|
|
340
|
+
{
|
|
341
|
+
// http://alienryderflex.com/point_left_of_ray/
|
|
342
|
+
|
|
343
|
+
//check which side of line AB the point P is on
|
|
344
|
+
if ((b.x - a.x) * (point.y - a.y) - (point.x - a.x) * (b.y - a.y) > 0)
|
|
345
|
+
{
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return true;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
public static void DestroyAllChildrenGameObjects(this GameObject gameObject)
|
|
353
|
+
{
|
|
354
|
+
if (Application.isEditor)
|
|
355
|
+
{
|
|
356
|
+
EditorDestroyAllChildrenGameObjects(gameObject);
|
|
357
|
+
}
|
|
358
|
+
else
|
|
359
|
+
{
|
|
360
|
+
PlayDestroyAllChildrenGameObjects(gameObject);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
public static void DestroyAllComponentsOfType<T>(this GameObject gameObject) where T : Component
|
|
365
|
+
{
|
|
366
|
+
foreach (T component in gameObject.GetComponents<T>())
|
|
367
|
+
{
|
|
368
|
+
SmartDestroy(component);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
public static void SmartDestroy(this Object obj, float? afterTime = null)
|
|
373
|
+
{
|
|
374
|
+
if (Application.isEditor && !Application.isPlaying)
|
|
375
|
+
{
|
|
376
|
+
Object.DestroyImmediate(obj);
|
|
377
|
+
}
|
|
378
|
+
else
|
|
379
|
+
{
|
|
380
|
+
if (afterTime.HasValue)
|
|
381
|
+
{
|
|
382
|
+
Object.Destroy(obj, afterTime.Value);
|
|
383
|
+
}
|
|
384
|
+
else
|
|
385
|
+
{
|
|
386
|
+
Object.Destroy(obj);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
public static void DestroyAllChildrenGameObjectsImmediatelyConditionally(this GameObject gameObject,
|
|
392
|
+
Func<GameObject, bool> acceptancePredicate)
|
|
393
|
+
{
|
|
394
|
+
InternalDestroyAllChildrenGameObjects(gameObject, toDestroy =>
|
|
395
|
+
{
|
|
396
|
+
if (!acceptancePredicate(toDestroy))
|
|
397
|
+
{
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
Object.DestroyImmediate(toDestroy);
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
public static void DestroyAllChildGameObjectsConditionally(this GameObject gameObject,
|
|
406
|
+
Func<GameObject, bool> acceptancePredicate)
|
|
407
|
+
{
|
|
408
|
+
InternalDestroyAllChildrenGameObjects(gameObject, toDestroy =>
|
|
409
|
+
{
|
|
410
|
+
if (!acceptancePredicate(toDestroy))
|
|
411
|
+
{
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
toDestroy.Destroy();
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
public static void DestroyAllChildrenGameObjectsImmediately(this GameObject gameObject) =>
|
|
419
|
+
InternalDestroyAllChildrenGameObjects(gameObject, Object.DestroyImmediate);
|
|
420
|
+
|
|
421
|
+
public static void PlayDestroyAllChildrenGameObjects(this GameObject gameObject) =>
|
|
422
|
+
InternalDestroyAllChildrenGameObjects(gameObject, go => go.Destroy());
|
|
423
|
+
|
|
424
|
+
public static void EditorDestroyAllChildrenGameObjects(this GameObject gameObject) =>
|
|
425
|
+
InternalDestroyAllChildrenGameObjects(gameObject, go => go.Destroy());
|
|
426
|
+
|
|
427
|
+
private static void InternalDestroyAllChildrenGameObjects(this GameObject gameObject,
|
|
428
|
+
Action<GameObject> destroyFunction)
|
|
429
|
+
{
|
|
430
|
+
for (int i = gameObject.transform.childCount - 1; 0 <= i; --i)
|
|
431
|
+
{
|
|
432
|
+
destroyFunction(gameObject.transform.GetChild(i).gameObject);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
public static bool IsPrefab(this GameObject gameObject)
|
|
437
|
+
{
|
|
438
|
+
Scene scene = gameObject.scene;
|
|
439
|
+
#if UNITY_EDITOR
|
|
440
|
+
if (scene.rootCount == 1 && string.Equals(scene.name, gameObject.name))
|
|
441
|
+
{
|
|
442
|
+
return true;
|
|
443
|
+
}
|
|
444
|
+
#endif
|
|
445
|
+
return scene.rootCount == 0;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
public static bool IsPrefab(this Component component)
|
|
449
|
+
{
|
|
450
|
+
return IsPrefab(component.gameObject);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
public static Vector2 RadianToVector2(float radian)
|
|
454
|
+
{
|
|
455
|
+
return new Vector2(Mathf.Cos(radian), Mathf.Sin(radian));
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
public static Vector2 DegreeToVector2(float degree)
|
|
459
|
+
{
|
|
460
|
+
return RadianToVector2(degree * Mathf.Deg2Rad);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
public static T GetOrAddComponent<T>(this GameObject unityObject) where T : Component
|
|
464
|
+
{
|
|
465
|
+
if (!unityObject.TryGetComponent(out T instance))
|
|
466
|
+
{
|
|
467
|
+
instance = unityObject.AddComponent<T>();
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
return instance;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
public static Component GetOrAddComponent(this GameObject unityObject, Type componentType)
|
|
474
|
+
{
|
|
475
|
+
if (!unityObject.TryGetComponent(componentType, out Component instance))
|
|
476
|
+
{
|
|
477
|
+
instance = unityObject.AddComponent(componentType);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
return instance;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
public static void ModifyAndSavePrefab(GameObject prefab, Action<GameObject> modifyAction)
|
|
484
|
+
{
|
|
485
|
+
if (prefab == null)
|
|
486
|
+
{
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
#if UNITY_EDITOR
|
|
491
|
+
if (PrefabUtility.IsPartOfPrefabAsset(prefab))
|
|
492
|
+
{
|
|
493
|
+
string assetPath = AssetDatabase.GetAssetPath(prefab);
|
|
494
|
+
GameObject content = PrefabUtility.LoadPrefabContents(assetPath);
|
|
495
|
+
|
|
496
|
+
if (content == null)
|
|
497
|
+
{
|
|
498
|
+
Debug.LogError($"Unable to load {prefab} as a prefab");
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
modifyAction(content);
|
|
503
|
+
_ = PrefabUtility.SaveAsPrefabAsset(content, assetPath);
|
|
504
|
+
PrefabUtility.UnloadPrefabContents(content);
|
|
505
|
+
}
|
|
506
|
+
else
|
|
507
|
+
{
|
|
508
|
+
modifyAction(prefab);
|
|
509
|
+
PrefabStage stage = PrefabStageUtility.GetPrefabStage(prefab);
|
|
510
|
+
if (stage)
|
|
511
|
+
{
|
|
512
|
+
_ = EditorSceneManager.MarkSceneDirty(stage.scene);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
#endif
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// https://gamedevelopment.tutsplus.com/tutorials/unity-solution-for-hitting-moving-targets--cms-29633
|
|
519
|
+
public static Vector2 PredictCurrentTarget(this GameObject currentTarget, Vector2 launchLocation, float projectileSpeed, bool predictiveFiring, Vector2 targetVelocity)
|
|
520
|
+
{
|
|
521
|
+
Vector2 target = currentTarget.transform.position;
|
|
522
|
+
|
|
523
|
+
if (!predictiveFiring)
|
|
524
|
+
{
|
|
525
|
+
return target;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
if (projectileSpeed <= 0)
|
|
529
|
+
{
|
|
530
|
+
return target;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
float a = (targetVelocity.x * targetVelocity.x) + (targetVelocity.y * targetVelocity.y) - (projectileSpeed * projectileSpeed);
|
|
534
|
+
|
|
535
|
+
float b = 2 * (targetVelocity.x * (target.x - launchLocation.x) +
|
|
536
|
+
targetVelocity.y * (target.y - launchLocation.y));
|
|
537
|
+
|
|
538
|
+
float c =
|
|
539
|
+
((target.x - launchLocation.x) *
|
|
540
|
+
(target.x - launchLocation.x)) +
|
|
541
|
+
((target.y - launchLocation.y) *
|
|
542
|
+
(target.y - launchLocation.y));
|
|
543
|
+
|
|
544
|
+
float disc = b * b - (4 * a * c);
|
|
545
|
+
if (disc < 0)
|
|
546
|
+
{
|
|
547
|
+
return target;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
float t1 = (-1 * b + Mathf.Sqrt(disc)) / (2 * a);
|
|
551
|
+
float t2 = (-1 * b - Mathf.Sqrt(disc)) / (2 * a);
|
|
552
|
+
float t = Mathf.Max(t1, t2); // let us take the larger time value
|
|
553
|
+
|
|
554
|
+
float aimX = target.x + (targetVelocity.x * t);
|
|
555
|
+
float aimY = target.y + (targetVelocity.y * t);
|
|
556
|
+
|
|
557
|
+
if (float.IsNaN(aimX) || float.IsNaN(aimY))
|
|
558
|
+
{
|
|
559
|
+
return target;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
if (float.IsInfinity(aimX) || float.IsInfinity(aimY))
|
|
563
|
+
{
|
|
564
|
+
return target;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
return new Vector2(aimX, aimY);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
public static T GetComponent<T>(this Object target)
|
|
571
|
+
{
|
|
572
|
+
return target switch
|
|
573
|
+
{
|
|
574
|
+
GameObject go => go.GetComponent<T>(),
|
|
575
|
+
Component c => c.GetComponent<T>(),
|
|
576
|
+
_ => default,
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
public static T[] GetComponents<T>(this Object target)
|
|
581
|
+
{
|
|
582
|
+
return target switch
|
|
583
|
+
{
|
|
584
|
+
GameObject go => go.GetComponents<T>(),
|
|
585
|
+
Component c => c.GetComponents<T>(),
|
|
586
|
+
_ => default,
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
public static GameObject GetGameObject(this object target)
|
|
591
|
+
{
|
|
592
|
+
return target switch
|
|
593
|
+
{
|
|
594
|
+
GameObject go => go,
|
|
595
|
+
Component c => c.gameObject,
|
|
596
|
+
_ => default
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
public static bool TryGetComponent<T>(this Object target, out T component)
|
|
601
|
+
{
|
|
602
|
+
component = default;
|
|
603
|
+
return target switch
|
|
604
|
+
{
|
|
605
|
+
GameObject go => go.TryGetComponent(out component),
|
|
606
|
+
Component c => c.TryGetComponent(out component),
|
|
607
|
+
_ => false,
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
public static GameObject FindChildGameObjectWithTag(this GameObject gameObject, string tag)
|
|
612
|
+
{
|
|
613
|
+
return gameObject.IterateOverChildGameObjectsRecursivelyIncludingSelf().FirstOrDefault(child => child.CompareTag(tag));
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
public static bool HasLineOfSight(Vector2 initialLocation, Vector2 direction, Transform transform, float totalDistance, float delta)
|
|
617
|
+
{
|
|
618
|
+
int hits = Physics2D.RaycastNonAlloc(initialLocation, direction, Buffers.RaycastHits);
|
|
619
|
+
for (int i = 0; i < hits; ++i)
|
|
620
|
+
{
|
|
621
|
+
RaycastHit2D hit = Buffers.RaycastHits[i];
|
|
622
|
+
if (hit.transform != transform)
|
|
623
|
+
{
|
|
624
|
+
continue;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
float distanceToEdge = totalDistance - hit.distance;
|
|
628
|
+
if (delta <= distanceToEdge)
|
|
629
|
+
{
|
|
630
|
+
return false;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
return true;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
public static Coroutine StartFunctionAsCoroutine(this MonoBehaviour monoBehaviour, Action action, float updateRate, bool useJitter = false, bool waitBefore = false)
|
|
638
|
+
{
|
|
639
|
+
return monoBehaviour.StartCoroutine(FunctionAsCoroutine(action, updateRate, useJitter, waitBefore));
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
private static IEnumerator FunctionAsCoroutine(Action action, float updateRate, bool useJitter, bool waitBefore)
|
|
643
|
+
{
|
|
644
|
+
bool usedJitter = false;
|
|
645
|
+
WaitForSeconds wait = Buffers.WaitForSeconds.GetOrAdd(updateRate, time => new WaitForSeconds(time));
|
|
646
|
+
|
|
647
|
+
while (true)
|
|
648
|
+
{
|
|
649
|
+
if (waitBefore)
|
|
650
|
+
{
|
|
651
|
+
// Copy-pasta the code, no way to unify in a performant way without generating garbage
|
|
652
|
+
yield return wait;
|
|
653
|
+
if (useJitter && !usedJitter)
|
|
654
|
+
{
|
|
655
|
+
yield return new WaitForSeconds(PcgRandom.Instance.NextFloat(updateRate));
|
|
656
|
+
usedJitter = true;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
action();
|
|
661
|
+
if (!waitBefore)
|
|
662
|
+
{
|
|
663
|
+
yield return wait;
|
|
664
|
+
if (useJitter && !usedJitter)
|
|
665
|
+
{
|
|
666
|
+
yield return new WaitForSeconds(PcgRandom.Instance.NextFloat(updateRate));
|
|
667
|
+
usedJitter = true;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
public static Coroutine ExecuteFunctionAfterDelay(this MonoBehaviour monoBehaviour, Action action, float delay)
|
|
674
|
+
{
|
|
675
|
+
return monoBehaviour.StartCoroutine(FunctionDelayAsCoroutine(action, delay));
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
public static Coroutine ExecuteFunctionNextFrame(this MonoBehaviour monoBehaviour, Action action)
|
|
679
|
+
{
|
|
680
|
+
return monoBehaviour.ExecuteFunctionAfterDelay(action, 0f);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
public static Coroutine ExecuteFunctionAfterFrame(this MonoBehaviour monoBehaviour, Action action)
|
|
684
|
+
{
|
|
685
|
+
return monoBehaviour.StartCoroutine(FunctionAfterFrame(action));
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
public static IEnumerator ExecuteOverTime(Action action, int totalCount, float duration, bool delay = true)
|
|
689
|
+
{
|
|
690
|
+
if (action == null)
|
|
691
|
+
{
|
|
692
|
+
yield break;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
if (totalCount <= 0)
|
|
696
|
+
{
|
|
697
|
+
yield break;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
int totalExecuted = 0;
|
|
701
|
+
float startTime = Time.time;
|
|
702
|
+
while (!HasEnoughTimePassed(startTime, duration))
|
|
703
|
+
{
|
|
704
|
+
float percent = (Time.time - startTime) / duration;
|
|
705
|
+
// optional delay execution from happening on 0, 1, 2, ... n-1 to 1, 2, ... n
|
|
706
|
+
if (totalExecuted < totalCount && ((totalExecuted + (delay ? 1f : 0f)) / totalCount) <= percent)
|
|
707
|
+
{
|
|
708
|
+
action();
|
|
709
|
+
++totalExecuted;
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
yield return null;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
for (; totalExecuted < totalCount;)
|
|
716
|
+
{
|
|
717
|
+
action();
|
|
718
|
+
++totalExecuted;
|
|
719
|
+
yield return null;
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
private static IEnumerator FunctionDelayAsCoroutine(Action action, float delay)
|
|
724
|
+
{
|
|
725
|
+
yield return Buffers.WaitForSeconds.GetOrAdd(delay, time => new WaitForSeconds(time));
|
|
726
|
+
action();
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
private static IEnumerator FunctionAfterFrame(Action action)
|
|
730
|
+
{
|
|
731
|
+
yield return WaitForEndOfFrame;
|
|
732
|
+
action();
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
public static bool HasEnoughTimePassed(float timestamp, float desiredDuration) => timestamp + desiredDuration < Time.time;
|
|
736
|
+
|
|
737
|
+
public static Vector2 Opposite(this Vector2 vector)
|
|
738
|
+
{
|
|
739
|
+
return vector * -1;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
public static Vector3 Opposite(this Vector3 vector)
|
|
743
|
+
{
|
|
744
|
+
return vector * -1;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
public static IEnumerable<Vector3Int> IterateArea(this BoundsInt bounds)
|
|
748
|
+
{
|
|
749
|
+
foreach (Vector3Int position in bounds.allPositionsWithin)
|
|
750
|
+
{
|
|
751
|
+
yield return position;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
public static IEnumerable<Vector3Int> IterateBounds(this BoundsInt bounds, int padding = 1)
|
|
756
|
+
{
|
|
757
|
+
for (int x = bounds.xMin - padding; x <= bounds.xMax + padding; ++x)
|
|
758
|
+
{
|
|
759
|
+
for (int y = bounds.yMin; y <= bounds.yMax + padding; ++y)
|
|
760
|
+
{
|
|
761
|
+
yield return new Vector3Int(x, y, 0);
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
public static Vector3Int AsVector3Int(this (int x, int y, int z) vector)
|
|
767
|
+
{
|
|
768
|
+
return new Vector3Int(vector.x, vector.y, vector.z);
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
public static Vector3Int AsVector3Int(this (uint x, uint y, uint z) vector)
|
|
772
|
+
{
|
|
773
|
+
return new Vector3Int((int) vector.x, (int) vector.y, (int) vector.z);
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
public static Vector3Int AsVector3Int(this Vector3 vector)
|
|
777
|
+
{
|
|
778
|
+
return new Vector3Int((int) Math.Round(vector.x), (int) Math.Round(vector.y), (int) Math.Round(vector.z));
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
public static Vector3 AsVector3(this (uint x, uint y, uint z) vector)
|
|
782
|
+
{
|
|
783
|
+
return new Vector3(vector.x, vector.y, vector.z);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
public static Vector3 AsVector3(this Vector3Int vector)
|
|
787
|
+
{
|
|
788
|
+
return new Vector3(vector.x, vector.y, vector.z);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
public static Vector2 AsVector2(this Vector3Int vector)
|
|
792
|
+
{
|
|
793
|
+
return new Vector2(vector.x, vector.y);
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
public static Vector2Int AsVector2Int(this Vector3Int vector)
|
|
797
|
+
{
|
|
798
|
+
return new Vector2Int(vector.x, vector.y);
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
public static Vector3Int AsVector3Int(this Vector2Int vector)
|
|
802
|
+
{
|
|
803
|
+
return new Vector3Int(vector.x, vector.y);
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
public static T CopyTo<T>(this T original, GameObject destination) where T : Component
|
|
807
|
+
{
|
|
808
|
+
Type type = original.GetType();
|
|
809
|
+
T copied = destination.GetComponent(type) as T;
|
|
810
|
+
if (copied == null)
|
|
811
|
+
{
|
|
812
|
+
copied = destination.AddComponent(type) as T;
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
foreach (FieldInfo field in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
|
|
816
|
+
{
|
|
817
|
+
try
|
|
818
|
+
{
|
|
819
|
+
field.SetValue(copied, field.GetValue(original));
|
|
820
|
+
}
|
|
821
|
+
catch
|
|
822
|
+
{
|
|
823
|
+
original.LogWarn("Failed to copy public field {0}.", field.Name);
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
foreach (FieldInfo field in type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
|
|
828
|
+
.Where(field => Attribute.IsDefined(field, typeof(SerializeField))))
|
|
829
|
+
{
|
|
830
|
+
try
|
|
831
|
+
{
|
|
832
|
+
field.SetValue(copied, field.GetValue(original));
|
|
833
|
+
}
|
|
834
|
+
catch
|
|
835
|
+
{
|
|
836
|
+
original.LogWarn("Failed to copy non-public field {0}.", field.Name);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
foreach (PropertyInfo property in type.GetProperties())
|
|
841
|
+
{
|
|
842
|
+
if (!property.CanWrite || property.Name == nameof(Object.name))
|
|
843
|
+
{
|
|
844
|
+
continue;
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
try
|
|
848
|
+
{
|
|
849
|
+
property.SetValue(copied, property.GetValue(original));
|
|
850
|
+
}
|
|
851
|
+
catch
|
|
852
|
+
{
|
|
853
|
+
original.LogWarn("Failed to copy property {0}.", property.Name);
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
return copied;
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
public static Rect AsRect(this BoundsInt bounds)
|
|
861
|
+
{
|
|
862
|
+
return new Rect(bounds.x, bounds.y, bounds.size.x, bounds.size.y);
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
public static Vector3 GetRandomPointInCircle(Vector2 center, float radius, IRandom random = null)
|
|
866
|
+
{
|
|
867
|
+
random ??= PcgRandom.Instance;
|
|
868
|
+
float r = radius * Mathf.Sqrt(random.NextFloat());
|
|
869
|
+
float theta = random.NextFloat() * 2 * Mathf.PI;
|
|
870
|
+
return new Vector3(center.x + r * Mathf.Cos(theta), center.y + r * Mathf.Sin(theta), 0.0f);
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
public static GameObject GetPlayerObjectInChildHierarchy(this GameObject gameObject)
|
|
874
|
+
{
|
|
875
|
+
return gameObject.GetTagObjectInChildHierarchy("Player");
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
public static GameObject GetTagObjectInChildHierarchy(this GameObject gameObject, string tag)
|
|
879
|
+
{
|
|
880
|
+
return gameObject.IterateOverChildGameObjectsRecursivelyIncludingSelf().FirstOrDefault(go => go.CompareTag(tag));
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
//https://answers.unity.com/questions/722748/refreshing-the-polygon-collider-2d-upon-sprite-cha.html
|
|
884
|
+
public static void UpdateShapeToSprite(this Component component)
|
|
885
|
+
{
|
|
886
|
+
if (!component.TryGetComponent(out SpriteRenderer spriteRenderer) || component.TryGetComponent(out PolygonCollider2D collider))
|
|
887
|
+
{
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
UpdateShapeToSprite(spriteRenderer.sprite, collider);
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
public static void UpdateShapeToSprite(Sprite sprite, PolygonCollider2D collider)
|
|
895
|
+
{
|
|
896
|
+
if (sprite == null || collider == null)
|
|
897
|
+
{
|
|
898
|
+
return;
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
int pathCount = collider.pathCount = sprite.GetPhysicsShapeCount();
|
|
902
|
+
|
|
903
|
+
List<Vector2> path = new();
|
|
904
|
+
for (int i = 0; i < pathCount; ++i)
|
|
905
|
+
{
|
|
906
|
+
path.Clear();
|
|
907
|
+
_ = sprite.GetPhysicsShape(i, path);
|
|
908
|
+
collider.SetPath(i, path);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
public static Vector3Int Cross(this Vector3Int vector, Vector3Int other)
|
|
913
|
+
{
|
|
914
|
+
int x = vector.y * other.z - other.y * vector.z;
|
|
915
|
+
int y = (vector.x * other.z - other.x * vector.z) * -1;
|
|
916
|
+
int z = vector.x * other.y - other.x * vector.y;
|
|
917
|
+
|
|
918
|
+
return new Vector3Int(x, y, z);
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
public static GameObject TryGetClosestParentWithComponentIncludingSelf<T>(this GameObject current) where T : Component
|
|
922
|
+
{
|
|
923
|
+
while (current != null)
|
|
924
|
+
{
|
|
925
|
+
if (current.HasComponent<T>())
|
|
926
|
+
{
|
|
927
|
+
return current;
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
Transform parent = current.transform.parent;
|
|
931
|
+
current = parent != null ? parent.gameObject : null;
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
return null;
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
#if UNITY_EDITOR
|
|
938
|
+
public static IEnumerable<GameObject> EnumeratePrefabs(IEnumerable<string> assetPaths = null)
|
|
939
|
+
{
|
|
940
|
+
assetPaths ??= new[] {"Assets/Prefabs", "Assets/Resources"};
|
|
941
|
+
|
|
942
|
+
foreach (string assetGuid in AssetDatabase.FindAssets("t:prefab", assetPaths.ToArray()))
|
|
943
|
+
{
|
|
944
|
+
string path = AssetDatabase.GUIDToAssetPath(assetGuid);
|
|
945
|
+
GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(path);
|
|
946
|
+
if (go != null)
|
|
947
|
+
{
|
|
948
|
+
yield return go;
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
public static IEnumerable<T> EnumerateScriptableObjects<T>(string[] assetPaths = null) where T : ScriptableObject
|
|
954
|
+
{
|
|
955
|
+
assetPaths ??= new[] { "Assets/Prefabs", "Assets/Resources", "Assets/TileMaps" };
|
|
956
|
+
|
|
957
|
+
foreach (string assetGuid in AssetDatabase.FindAssets("t:" + typeof(T).Name, assetPaths))
|
|
958
|
+
{
|
|
959
|
+
string path = AssetDatabase.GUIDToAssetPath(assetGuid);
|
|
960
|
+
T so = AssetDatabase.LoadAssetAtPath<T>(path);
|
|
961
|
+
if (so != null)
|
|
962
|
+
{
|
|
963
|
+
yield return so;
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
#endif
|
|
968
|
+
|
|
969
|
+
public static bool NameEquals(Object lhs, Object rhs)
|
|
970
|
+
{
|
|
971
|
+
if (lhs == rhs)
|
|
972
|
+
{
|
|
973
|
+
return true;
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
if (lhs == null || rhs == null)
|
|
977
|
+
{
|
|
978
|
+
return false;
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
if (string.Equals(lhs.name, rhs.name))
|
|
982
|
+
{
|
|
983
|
+
return true;
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
if (lhs.name == null || rhs.name == null)
|
|
987
|
+
{
|
|
988
|
+
return false;
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
const string clone = "(Clone)";
|
|
992
|
+
string lhsName = lhs.name;
|
|
993
|
+
while (lhsName.EndsWith(clone))
|
|
994
|
+
{
|
|
995
|
+
lhsName = lhsName.Substring(lhsName.Length - clone.Length - 1);
|
|
996
|
+
lhsName = lhsName.Trim();
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
string rhsName = rhs.name;
|
|
1000
|
+
while (rhsName.EndsWith(clone))
|
|
1001
|
+
{
|
|
1002
|
+
rhsName = rhsName.Substring(rhsName.Length - clone.Length - 1);
|
|
1003
|
+
rhsName = rhsName.Trim();
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
return string.Equals(lhsName, rhsName);
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
public static Color ChangeColorBrightness(this Color color, float correctionFactor)
|
|
1010
|
+
{
|
|
1011
|
+
correctionFactor = Math.Clamp(correctionFactor, -1f, 1f);
|
|
1012
|
+
|
|
1013
|
+
float red = color.r;
|
|
1014
|
+
float green = color.g;
|
|
1015
|
+
float blue = color.b;
|
|
1016
|
+
if (correctionFactor < 0)
|
|
1017
|
+
{
|
|
1018
|
+
correctionFactor += 1;
|
|
1019
|
+
red *= correctionFactor;
|
|
1020
|
+
green *= correctionFactor;
|
|
1021
|
+
blue *= correctionFactor;
|
|
1022
|
+
}
|
|
1023
|
+
else
|
|
1024
|
+
{
|
|
1025
|
+
red = (1f - red) * correctionFactor + red;
|
|
1026
|
+
green = (1f - green) * correctionFactor + green;
|
|
1027
|
+
blue = (1f - blue) * correctionFactor + blue;
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
return new Color(red, green, blue, color.a);
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
public static void AwakeObject(this GameObject gameObject)
|
|
1034
|
+
{
|
|
1035
|
+
foreach (MonoBehaviour script in gameObject.GetComponentsInChildren<MonoBehaviour>())
|
|
1036
|
+
{
|
|
1037
|
+
MethodInfo awakeInfo = AwakeMethodsByType.GetOrAdd(script.GetType(), type => type.GetMethod("Awake", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));
|
|
1038
|
+
if (awakeInfo != null)
|
|
1039
|
+
{
|
|
1040
|
+
_ = awakeInfo.Invoke(script, null);
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
public static Vector2 GetAngleWithSpeed(Vector2 targetDirection, Vector2 currentDirection, float rotationSpeed)
|
|
1046
|
+
{
|
|
1047
|
+
if (targetDirection == Vector2.zero)
|
|
1048
|
+
{
|
|
1049
|
+
return currentDirection;
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
float turnRatePerFrame = rotationSpeed * Time.deltaTime;
|
|
1053
|
+
float angleDiscrepancy = Vector2.SignedAngle(currentDirection, targetDirection);
|
|
1054
|
+
float turnRateThisFrame;
|
|
1055
|
+
if (Math.Sign(angleDiscrepancy) < 0)
|
|
1056
|
+
{
|
|
1057
|
+
turnRateThisFrame = -1 * Math.Min(turnRatePerFrame, -angleDiscrepancy);
|
|
1058
|
+
}
|
|
1059
|
+
else
|
|
1060
|
+
{
|
|
1061
|
+
turnRateThisFrame = Math.Min(turnRatePerFrame, angleDiscrepancy);
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
float currentAngle = Vector2.SignedAngle(Vector2.right, currentDirection);
|
|
1065
|
+
currentAngle += turnRateThisFrame;
|
|
1066
|
+
return (Quaternion.AngleAxis(currentAngle, Vector3.forward) * Vector3.right).normalized;
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
public static void Extend2D(ref BoundsInt bounds, FastVector3Int position)
|
|
1070
|
+
{
|
|
1071
|
+
if (position.x < bounds.xMin)
|
|
1072
|
+
{
|
|
1073
|
+
bounds.xMin = position.x;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
if (bounds.xMax < position.x)
|
|
1077
|
+
{
|
|
1078
|
+
bounds.xMax = position.x;
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
if (position.y < bounds.yMin)
|
|
1082
|
+
{
|
|
1083
|
+
bounds.yMin = position.y;
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
if (bounds.yMax < position.y)
|
|
1087
|
+
{
|
|
1088
|
+
bounds.yMax = position.y;
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
}
|