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 7.4KB

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