This repository was archived by the owner on Jan 24, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathUnityExtensions.cs
More file actions
111 lines (95 loc) · 3.85 KB
/
Copy pathUnityExtensions.cs
File metadata and controls
111 lines (95 loc) · 3.85 KB
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
using System;
using UnityEngine;
namespace ReMod.Core.Unity
{
public static class UnityExtensions
{
public static string GetPath(this Transform current)
{
if (current.parent == null)
return "/" + current.name;
return current.parent.GetPath() + "/" + current.name;
}
public static T[] GetComponentsInDirectChildren<T>(this GameObject gameObject)
{
var indexer = 0;
foreach (var child in gameObject.transform)
{
var transform = child.Cast<Transform>();
if (transform.GetComponent<T>() != null)
{
indexer++;
}
}
var returnArray = new T[indexer];
indexer = 0;
foreach (var child in gameObject.transform)
{
var transform = child.Cast<Transform>();
if (transform.GetComponent<T>() != null)
{
returnArray[indexer++] = transform.GetComponent<T>();
}
}
return returnArray;
}
public static bool IsAbsurd(this float f)
{
return !(f > MaxAllowedValueBottom && f < MaxAllowedValueTop);
}
public static bool IsBad(this Vector3 v3)
{
return float.IsNaN(v3.x) || float.IsNaN(v3.y) || float.IsNaN(v3.z) ||
float.IsInfinity(v3.x) || float.IsInfinity(v3.y) || float.IsInfinity(v3.z);
}
public const float MaxAllowedValueTop = 3.402823E+7f;
public const float MaxAllowedValueBottom = -3.402823E+7f;
public static bool IsAbsurd(this Vector3 v3)
{
return !(v3.x > MaxAllowedValueBottom && v3.x < MaxAllowedValueTop) ||
!(v3.y > MaxAllowedValueBottom && v3.y < MaxAllowedValueTop) ||
!(v3.z > MaxAllowedValueBottom && v3.z < MaxAllowedValueTop);
}
public static void Clamp(this Vector3 v3)
{
v3.x = Mathf.Clamp(v3.x, -512000f, 512000f);
v3.y = Mathf.Clamp(v3.y, -512000f, 512000f);
v3.z = Mathf.Clamp(v3.z, -512000f, 512000f);
}
public static void Clamp(this Quaternion v3)
{
v3.x = Mathf.Clamp(v3.x, -512000f, 512000f);
v3.y = Mathf.Clamp(v3.y, -512000f, 512000f);
v3.z = Mathf.Clamp(v3.z, -512000f, 512000f);
v3.w = Mathf.Clamp(v3.w, -512000f, 512000f);
}
public static string ToCleanString(this Vector3 v3, string format="F4")
{
return v3.ToString(format).Replace(" ", string.Empty).Trim('(', ')');
}
/// <summary>
/// Returns a copy of the float rounded to the given number.
/// </summary>
/// <param name="nearestFactor">The number the float should be rounded to</param>
public static float RoundAmount(this float i, float nearestFactor)
{
return (float)Math.Round(i / nearestFactor) * nearestFactor;
}
/// <summary>
/// Returns a copy of the vector rounded to the given number.
/// </summary>
/// <param name="nearestFactor">The number the vector should be rounded to</param>
public static Vector3 RoundAmount(this Vector3 i, float nearestFactor)
{
return new Vector3(i.x.RoundAmount(nearestFactor), i.y.RoundAmount(nearestFactor), i.z.RoundAmount(nearestFactor));
}
/// <summary>
/// Returns a copy of the vector rounded to the given number.
/// </summary>
/// <param name="nearestFactor">The number the vector should be rounded to</param>
public static Vector2 RoundAmount(this Vector2 i, float nearestFactor)
{
return new Vector2(i.x.RoundAmount(nearestFactor), i.y.RoundAmount(nearestFactor));
}
}
}