TMPro_ExtensionMethods.cs 6.5 KB

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