forked from Deadcows/MyBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyExtensions.cs
252 lines (209 loc) · 7.33 KB
/
MyExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Object = UnityEngine.Object;
namespace MyBox
{
public static class MyExtensions
{
/// <summary>
/// Swap two elements in array
/// </summary>
public static void Swap<T>(this T[] array, int a, int b) => (array[a], array[b]) = (array[b], array[a]);
public static bool IsWorldPointInViewport(this Camera camera, Vector3 point)
{
var position = camera.WorldToViewportPoint(point);
return position.x > 0 && position.y > 0;
}
/// <summary>
/// Gets a point with the same screen point as the source point,
/// but at the specified distance from camera.
/// </summary>
public static Vector3 WorldPointOffsetByDepth(this Camera camera,
Vector3 source,
float distanceFromCamera,
Camera.MonoOrStereoscopicEye eye = Camera.MonoOrStereoscopicEye.Mono)
{
var screenPoint = camera.WorldToScreenPoint(source, eye);
return camera.ScreenToWorldPoint(screenPoint.SetZ(distanceFromCamera),
eye);
}
/// <summary>
/// Set position to Vector3.zero
/// </summary>
public static void ResetPosition(this Transform transform) => transform.position = Vector3.zero;
/// <summary>
/// Sets the lossy scale of the source Transform.
/// </summary>
public static Transform SetLossyScale(this Transform source,
Vector3 targetLossyScale)
{
source.localScale = source.lossyScale.Pow(-1).ScaleBy(targetLossyScale)
.ScaleBy(source.localScale);
return source;
}
/// <summary>
/// Sets a layer to the source's attached GameObject and all of its children
/// in the hierarchy.
/// </summary>
public static T SetLayerRecursively<T>(this T source, string layerName)
where T : Component
{
source.gameObject.SetLayerRecursively(LayerMask.NameToLayer(layerName));
return source;
}
/// <summary>
/// Sets a layer to the source's attached GameObject and all of its children
/// in the hierarchy.
/// </summary>
public static T SetLayerRecursively<T>(this T source, int layer)
where T : Component
{
source.gameObject.SetLayerRecursively(layer);
return source;
}
/// <summary>
/// Sets a layer to the source GameObject and all of its children in the
/// hierarchy.
/// </summary>
public static GameObject SetLayerRecursively(this GameObject source,
string layerName)
{
source.SetLayerRecursively(LayerMask.NameToLayer(layerName));
return source;
}
/// <summary>
/// Sets a layer to the source GameObject and all of its children in the
/// hierarchy.
/// </summary>
public static GameObject SetLayerRecursively(this GameObject source, int layer)
{
var allTransforms = source.GetComponentsInChildren<Transform>(true);
foreach (var tf in allTransforms) tf.gameObject.layer = layer;
return source;
}
public static T GetOrAddComponent<T>(this GameObject gameObject) where T : Component
{
var toGet = gameObject.GetComponent<T>();
if (toGet != null) return toGet;
return gameObject.AddComponent<T>();
}
public static T GetOrAddComponent<T>(this Component component) where T : Component
=> GetOrAddComponent<T>(component.gameObject);
public static bool HasComponent<T>(this GameObject gameObject) => gameObject.GetComponent<T>() != null;
public static bool HasComponent<T>(this Component component) => component.GetComponent<T>() != null;
/// <summary>
/// Recursively get childs that match specific predicate
/// </summary>
public static List<Transform> GetChildsWhere(this Transform transform, Predicate<Transform> match)
{
List<Transform> list = new List<Transform>();
RecursiveCheck(transform);
return list;
void RecursiveCheck(Transform parent)
{
foreach (Transform t in parent)
{
RecursiveCheck(t);
if (match.Invoke(t)) list.Add(t);
}
}
}
/// <summary>
/// Get all components of specified Layer in childs
/// </summary>
public static List<Transform> GetObjectsOfLayerInChilds(this GameObject gameObject, int layer)
=> GetChildsWhere(gameObject.transform, t => t.gameObject.layer == layer);
/// <summary>
/// Get all components of specified Layer in childs
/// </summary>
public static List<Transform> GetObjectsOfLayerInChilds(this GameObject gameObject, string layer)
=> gameObject.GetObjectsOfLayerInChilds(LayerMask.NameToLayer(layer));
/// <summary>
/// Get all components of specified Layer in childs
/// </summary>
public static List<Transform> GetObjectsOfLayerInChilds(this Component component, string layer)
=> component.GetObjectsOfLayerInChilds(LayerMask.NameToLayer(layer));
/// <summary>
/// Get all components of specified Layer in childs
/// </summary>
public static List<Transform> GetObjectsOfLayerInChilds(this Component component, int layer)
=> component.gameObject.GetObjectsOfLayerInChilds(layer);
#if UNITY_PHYSICS_ENABLED
/// <summary>
/// Swap Rigidbody IsKinematic and DetectCollisions
/// </summary>
/// <param name="body"></param>
/// <param name="state"></param>
public static void SetBodyState(this Rigidbody body, bool state)
{
body.isKinematic = !state;
body.detectCollisions = state;
}
#endif
/// <summary>
/// Find all Components of specified interface
/// </summary>
public static T[] FindObjectsOfInterface<T>() where T : class
{
var monoBehaviours = Object.FindObjectsOfType<Transform>();
return monoBehaviours.Select(behaviour => behaviour.GetComponent(typeof(T))).OfType<T>().ToArray();
}
/// <summary>
/// Find all Components of specified interface along with Component itself
/// </summary>
public static ComponentOfInterface<T>[] FindObjectsOfInterfaceAsComponents<T>() where T : class
{
return Object.FindObjectsOfType<Component>()
.Where(c => c is T)
.Select(c => new ComponentOfInterface<T>(c, c as T)).ToArray();
}
public struct ComponentOfInterface<T>
{
public readonly Component Component;
public readonly T Interface;
public ComponentOfInterface(Component component, T @interface)
{
Component = component;
Interface = @interface;
}
}
#region One Per Instance
/// <summary>
/// Get components with unique Instance ID
/// </summary>
public static T[] OnePerInstance<T>(this T[] components) where T : Component
{
if (components == null || components.Length == 0) return null;
return components.GroupBy(h => h.transform.GetInstanceID()).Select(g => g.First()).ToArray();
}
#if UNITY_PHYSICS2D_ENABLED
/// <summary>
/// Get hits with unique owner Instance ID
/// </summary>
public static RaycastHit2D[] OneHitPerInstance(this RaycastHit2D[] hits)
{
if (hits == null || hits.Length == 0) return null;
return hits.GroupBy(h => h.transform.GetInstanceID()).Select(g => g.First()).ToArray();
}
/// <summary>
/// Get colliders with unique owner Instance ID
/// </summary>
public static Collider2D[] OneHitPerInstance(this Collider2D[] hits)
{
if (hits == null || hits.Length == 0) return null;
return hits.GroupBy(h => h.transform.GetInstanceID()).Select(g => g.First()).ToArray();
}
/// <summary>
/// Get colliders with unique owner Instance ID
/// </summary>
public static List<Collider2D> OneHitPerInstanceList(this Collider2D[] hits)
{
if (hits == null || hits.Length == 0) return null;
return hits.GroupBy(h => h.transform.GetInstanceID()).Select(g => g.First()).ToList();
}
#endif
#endregion
}
}