A game about forced loneliness, made by TACStudios
at master 296 lines 8.7 kB view raw
1using UnityEngine; 2using System.Text; 3using System.Collections; 4using System.Collections.Generic; 5 6namespace TMPro 7{ 8 public static class TMPro_ExtensionMethods 9 { 10 11 /// <summary> 12 /// Converts a string of 4 ascii characters to an int. 13 /// </summary> 14 /// <param name="s">String comprised of 4 ascii characters.</param> 15 /// <returns>The integer value for the string.</returns> 16 internal static int TagToInt(this string s) 17 { 18 if (string.IsNullOrEmpty(s)) 19 return 0; 20 21 return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]; 22 } 23 24 25 public static int[] ToIntArray(this string text) 26 { 27 int[] intArray = new int[text.Length]; 28 29 for (int i = 0; i < text.Length; i++) 30 { 31 intArray[i] = text[i]; 32 } 33 34 return intArray; 35 } 36 37 public static string ArrayToString(this char[] chars) 38 { 39 string s = string.Empty; 40 41 for (int i = 0; i < chars.Length && chars[i] != 0; i++) 42 { 43 s += chars[i]; 44 } 45 46 return s; 47 } 48 49 public static string IntToString(this int[] unicodes) 50 { 51 char[] chars = new char[unicodes.Length]; 52 53 for (int i = 0; i < unicodes.Length; i++) 54 { 55 chars[i] = (char)unicodes[i]; 56 } 57 58 return new string(chars); 59 } 60 61 internal static string UintToString(this List<uint> unicodes) 62 { 63 char[] chars = new char[unicodes.Count]; 64 65 for (int i = 0; i < unicodes.Count; i++) 66 { 67 chars[i] = (char)unicodes[i]; 68 } 69 70 return new string(chars); 71 } 72 73 public static string IntToString(this int[] unicodes, int start, int length) 74 { 75 if (start > unicodes.Length) 76 return string.Empty; 77 78 int end = Mathf.Min(start + length, unicodes.Length); 79 80 char[] chars = new char[end - start]; 81 82 int writeIndex = 0; 83 84 for (int i = start; i < end; i++) 85 { 86 chars[writeIndex++] = (char)unicodes[i]; 87 } 88 89 return new string(chars); 90 } 91 92 93 public static int FindInstanceID <T> (this List<T> list, T target) where T : Object 94 { 95 int targetID = target.GetInstanceID(); 96 97 for (int i = 0; i < list.Count; i++) 98 { 99 if (list[i].GetInstanceID() == targetID) 100 return i; 101 } 102 return -1; 103 } 104 105 106 public static bool Compare(this Color32 a, Color32 b) 107 { 108 return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a; 109 } 110 111 public static bool CompareRGB(this Color32 a, Color32 b) 112 { 113 return a.r == b.r && a.g == b.g && a.b == b.b; 114 } 115 116 public static bool Compare(this Color a, Color b) 117 { 118 return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a; 119 } 120 121 122 public static bool CompareRGB(this Color a, Color b) 123 { 124 return a.r == b.r && a.g == b.g && a.b == b.b; 125 } 126 127 128 public static Color32 Multiply (this Color32 c1, Color32 c2) 129 { 130 byte r = (byte)((c1.r / 255f) * (c2.r / 255f) * 255); 131 byte g = (byte)((c1.g / 255f) * (c2.g / 255f) * 255); 132 byte b = (byte)((c1.b / 255f) * (c2.b / 255f) * 255); 133 byte a = (byte)((c1.a / 255f) * (c2.a / 255f) * 255); 134 135 return new Color32(r, g, b, a); 136 } 137 138 139 public static Color32 Tint (this Color32 c1, Color32 c2) 140 { 141 byte r = (byte)((c1.r / 255f) * (c2.r / 255f) * 255); 142 byte g = (byte)((c1.g / 255f) * (c2.g / 255f) * 255); 143 byte b = (byte)((c1.b / 255f) * (c2.b / 255f) * 255); 144 byte a = (byte)((c1.a / 255f) * (c2.a / 255f) * 255); 145 146 return new Color32(r, g, b, a); 147 } 148 149 public static Color32 Tint(this Color32 c1, float tint) 150 { 151 byte r = (byte)(Mathf.Clamp(c1.r / 255f * tint * 255, 0, 255)); 152 byte g = (byte)(Mathf.Clamp(c1.g / 255f * tint * 255, 0, 255)); 153 byte b = (byte)(Mathf.Clamp(c1.b / 255f * tint * 255, 0, 255)); 154 byte a = (byte)(Mathf.Clamp(c1.a / 255f * tint * 255, 0, 255)); 155 156 return new Color32(r, g, b, a); 157 } 158 159 internal static Color32 GammaToLinear(this Color32 c) 160 { 161 return new Color32(GammaToLinear(c.r), GammaToLinear(c.g), GammaToLinear(c.b), c.a); 162 } 163 164 static byte GammaToLinear(byte value) 165 { 166 float v = value / 255f; 167 168 if (v <= 0.04045f) 169 return (byte)(v / 12.92f * 255f); 170 171 if (v < 1.0f) 172 return (byte)(Mathf.Pow((v + 0.055f) / 1.055f, 2.4f) * 255); 173 174 if (v == 1.0f) 175 return 255; 176 177 return (byte)(Mathf.Pow(v, 2.2f) * 255); 178 } 179 180 public static Color MinAlpha(this Color c1, Color c2) 181 { 182 float a = c1.a < c2.a ? c1.a : c2.a; 183 184 return new Color(c1.r, c1.g, c1.b, a); 185 } 186 187 188 public static bool Compare(this Vector3 v1, Vector3 v2, int accuracy) 189 { 190 bool x = (int)(v1.x * accuracy) == (int)(v2.x * accuracy); 191 bool y = (int)(v1.y * accuracy) == (int)(v2.y * accuracy); 192 bool z = (int)(v1.z * accuracy) == (int)(v2.z * accuracy); 193 194 return x && y && z; 195 } 196 197 public static bool Compare(this Quaternion q1, Quaternion q2, int accuracy) 198 { 199 bool x = (int)(q1.x * accuracy) == (int)(q2.x * accuracy); 200 bool y = (int)(q1.y * accuracy) == (int)(q2.y * accuracy); 201 bool z = (int)(q1.z * accuracy) == (int)(q2.z * accuracy); 202 bool w = (int)(q1.w * accuracy) == (int)(q2.w * accuracy); 203 204 return x && y && z && w; 205 } 206 207 //public static void AddElementAtIndex<T>(this T[] array, int writeIndex, T item) 208 //{ 209 // if (writeIndex >= array.Length) 210 // System.Array.Resize(ref array, Mathf.NextPowerOfTwo(writeIndex + 1)); 211 212 // array[writeIndex] = item; 213 //} 214 215 /// <summary> 216 /// Insert item into array at index. 217 /// </summary> 218 /// <typeparam name="T"></typeparam> 219 /// <param name="array"></param> 220 /// <param name="index"></param> 221 /// <param name="item"></param> 222 //public static void Insert<T>(this T[] array, int index, T item) 223 //{ 224 // if (index > array.Length - 1) return; 225 226 // T savedItem = item; 227 228 // for (int i = index; i < array.Length; i++) 229 // { 230 // savedItem = array[i]; 231 232 // array[i] = item; 233 234 // item = savedItem; 235 // } 236 //} 237 238 /// <summary> 239 /// Insert item into array at index. 240 /// </summary> 241 /// <typeparam name="T"></typeparam> 242 /// <param name="array"></param> 243 /// <param name="index"></param> 244 /// <param name="item"></param> 245 //public static void Insert<T>(this T[] array, int index, T[] items) 246 //{ 247 // if (index > array.Length - 1) return; 248 249 // System.Array.Resize(ref array, array.Length + items.Length); 250 251 // int sourceIndex = 0; 252 253 // T savedItem = items[sourceIndex]; 254 255 // for (int i = index; i < array.Length; i++) 256 // { 257 // savedItem = array[i]; 258 259 // array[i] = items[sourceIndex]; 260 261 // items[sourceIndex] = savedItem; 262 263 // if (sourceIndex < items.Length - 1) 264 // sourceIndex += 1; 265 // else 266 // sourceIndex = 0; 267 // } 268 //} 269 270 } 271 272 public static class TMP_Math 273 { 274 public const float FLOAT_MAX = 32767; 275 public const float FLOAT_MIN = -32767; 276 public const int INT_MAX = 2147483647; 277 public const int INT_MIN = -2147483647; 278 279 public const float FLOAT_UNSET = -32767; 280 public const int INT_UNSET = -32767; 281 282 public static Vector2 MAX_16BIT = new Vector2(FLOAT_MAX, FLOAT_MAX); 283 public static Vector2 MIN_16BIT = new Vector2(FLOAT_MIN, FLOAT_MIN); 284 285 public static bool Approximately(float a, float b) 286 { 287 return (b - 0.0001f) < a && a < (b + 0.0001f); 288 } 289 290 public static int Mod(int a, int b) 291 { 292 int r = a % b; 293 return r < 0 ? r + b : r; 294 } 295 } 296}