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.

218 lines
6.4 KiB

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