TMPro_ExtensionMethods.cs
6.75 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
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
using UnityEngine;
using System.Text;
using System.Collections;
using System.Collections.Generic;
namespace TMPro
{
public static class TMPro_ExtensionMethods
{
public static string ArrayToString(this char[] chars)
{
string s = string.Empty;
for (int i = 0; i < chars.Length && chars[i] != 0; i++)
{
s += chars[i];
}
return s;
}
public static string IntToString(this int[] unicodes)
{
char[] chars = new char[unicodes.Length];
for (int i = 0; i < unicodes.Length; i++)
{
chars[i] = (char)unicodes[i];
}
return new string(chars);
}
public static string IntToString(this int[] unicodes, int start, int length)
{
if (start > unicodes.Length)
return string.Empty;
int end = Mathf.Min(start + length, unicodes.Length);
char[] chars = new char[end - start];
int writeIndex = 0;
for (int i = start; i < end; i++)
{
chars[writeIndex++] = (char)unicodes[i];
}
return new string(chars);
}
public static int FindInstanceID <T> (this List<T> list, T target) where T : Object
{
int targetID = target.GetInstanceID();
for (int i = 0; i < list.Count; i++)
{
if (list[i].GetInstanceID() == targetID)
return i;
}
return -1;
}
public static bool Compare(this Color32 a, Color32 b)
{
return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
}
public static bool CompareRGB(this Color32 a, Color32 b)
{
return a.r == b.r && a.g == b.g && a.b == b.b;
}
public static bool Compare(this Color a, Color b)
{
return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
}
public static bool CompareRGB(this Color a, Color b)
{
return a.r == b.r && a.g == b.g && a.b == b.b;
}
public static Color32 Multiply (this Color32 c1, Color32 c2)
{
byte r = (byte)((c1.r / 255f) * (c2.r / 255f) * 255);
byte g = (byte)((c1.g / 255f) * (c2.g / 255f) * 255);
byte b = (byte)((c1.b / 255f) * (c2.b / 255f) * 255);
byte a = (byte)((c1.a / 255f) * (c2.a / 255f) * 255);
return new Color32(r, g, b, a);
}
public static Color32 Tint (this Color32 c1, Color32 c2)
{
byte r = (byte)((c1.r / 255f) * (c2.r / 255f) * 255);
byte g = (byte)((c1.g / 255f) * (c2.g / 255f) * 255);
byte b = (byte)((c1.b / 255f) * (c2.b / 255f) * 255);
byte a = (byte)((c1.a / 255f) * (c2.a / 255f) * 255);
return new Color32(r, g, b, a);
}
public static Color32 Tint(this Color32 c1, float tint)
{
byte r = (byte)(Mathf.Clamp(c1.r / 255f * tint * 255, 0, 255));
byte g = (byte)(Mathf.Clamp(c1.g / 255f * tint * 255, 0, 255));
byte b = (byte)(Mathf.Clamp(c1.b / 255f * tint * 255, 0, 255));
byte a = (byte)(Mathf.Clamp(c1.a / 255f * tint * 255, 0, 255));
return new Color32(r, g, b, a);
}
public static bool Compare(this Vector3 v1, Vector3 v2, int accuracy)
{
bool x = (int)(v1.x * accuracy) == (int)(v2.x * accuracy);
bool y = (int)(v1.y * accuracy) == (int)(v2.y * accuracy);
bool z = (int)(v1.z * accuracy) == (int)(v2.z * accuracy);
return x && y && z;
}
public static bool Compare(this Quaternion q1, Quaternion q2, int accuracy)
{
bool x = (int)(q1.x * accuracy) == (int)(q2.x * accuracy);
bool y = (int)(q1.y * accuracy) == (int)(q2.y * accuracy);
bool z = (int)(q1.z * accuracy) == (int)(q2.z * accuracy);
bool w = (int)(q1.w * accuracy) == (int)(q2.w * accuracy);
return x && y && z && w;
}
//public static void AddElementAtIndex<T>(this T[] array, int writeIndex, T item)
//{
// if (writeIndex >= array.Length)
// System.Array.Resize(ref array, Mathf.NextPowerOfTwo(writeIndex + 1));
// array[writeIndex] = item;
//}
/// <summary>
/// Insert item into array at index.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="index"></param>
/// <param name="item"></param>
//public static void Insert<T>(this T[] array, int index, T item)
//{
// if (index > array.Length - 1) return;
// T savedItem = item;
// for (int i = index; i < array.Length; i++)
// {
// savedItem = array[i];
// array[i] = item;
// item = savedItem;
// }
//}
/// <summary>
/// Insert item into array at index.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="index"></param>
/// <param name="item"></param>
//public static void Insert<T>(this T[] array, int index, T[] items)
//{
// if (index > array.Length - 1) return;
// System.Array.Resize(ref array, array.Length + items.Length);
// int sourceIndex = 0;
// T savedItem = items[sourceIndex];
// for (int i = index; i < array.Length; i++)
// {
// savedItem = array[i];
// array[i] = items[sourceIndex];
// items[sourceIndex] = savedItem;
// if (sourceIndex < items.Length - 1)
// sourceIndex += 1;
// else
// sourceIndex = 0;
// }
//}
}
public static class TMP_Math
{
public const float FLOAT_MAX = 32767;
public const float FLOAT_MIN = -32767;
public const int INT_MAX = 2147483647;
public const int INT_MIN = -2147483647;
public const float FLOAT_UNSET = -32767;
public const int INT_UNSET = -32767;
public static Vector2 MAX_16BIT = new Vector2(FLOAT_MAX, FLOAT_MAX);
public static Vector2 MIN_16BIT = new Vector2(FLOAT_MIN, FLOAT_MIN);
public static bool Approximately(float a, float b)
{
return (b - 0.0001f) < a && a < (b + 0.0001f);
}
}
}