No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TMPro_ExtensionMethods.cs 8.5KB

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