TMPro_ExtensionMethods.cs 7.1 KB

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