I2Utils.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. namespace I2.Loc
  6. {
  7. public static class I2Utils
  8. {
  9. public static string ReverseText(string source)
  10. {
  11. int length = source.Length;
  12. char[] array = new char[length];
  13. for (int i = 0; i < length; i++)
  14. {
  15. array[length - 1 - i] = source[i];
  16. }
  17. return new string(array);
  18. }
  19. public static string RemoveNonASCII(string text, bool allowCategory = false)
  20. {
  21. if (string.IsNullOrEmpty(text))
  22. {
  23. return text;
  24. }
  25. return new string((from c in text.ToCharArray()
  26. select (!char.IsControl(c) && (c != '\\' || allowCategory)) ? c : ' ').ToArray<char>());
  27. }
  28. public static string SplitLine(string line, int maxCharacters)
  29. {
  30. if (maxCharacters <= 0 || line.Length < maxCharacters)
  31. {
  32. return line;
  33. }
  34. char[] array = line.ToCharArray();
  35. bool flag = true;
  36. bool flag2 = false;
  37. int i = 0;
  38. int num = 0;
  39. while (i < array.Length)
  40. {
  41. if (flag)
  42. {
  43. num++;
  44. if (array[i] == '\n')
  45. {
  46. num = 0;
  47. }
  48. if (num >= maxCharacters && char.IsWhiteSpace(array[i]))
  49. {
  50. array[i] = '\n';
  51. flag = false;
  52. flag2 = false;
  53. }
  54. }
  55. else if (!char.IsWhiteSpace(array[i]))
  56. {
  57. flag = true;
  58. num = 0;
  59. }
  60. else if (array[i] != '\n')
  61. {
  62. array[i] = '\0';
  63. }
  64. else
  65. {
  66. if (!flag2)
  67. {
  68. array[i] = '\0';
  69. }
  70. flag2 = true;
  71. }
  72. i++;
  73. }
  74. return new string((from c in array
  75. where c != '\0'
  76. select c).ToArray<char>());
  77. }
  78. public static bool FindNextTag(string line, int iStart, out int tagStart, out int tagEnd)
  79. {
  80. tagStart = -1;
  81. tagEnd = -1;
  82. int length = line.Length;
  83. for (tagStart = iStart; tagStart < length; tagStart++)
  84. {
  85. if (line[tagStart] == '[' || line[tagStart] == '(' || line[tagStart] == '{')
  86. {
  87. break;
  88. }
  89. }
  90. if (tagStart == length)
  91. {
  92. return false;
  93. }
  94. bool flag = false;
  95. for (tagEnd = tagStart + 1; tagEnd < length; tagEnd++)
  96. {
  97. char c = line[tagEnd];
  98. if (c == ']' || c == ')' || c == '}')
  99. {
  100. return !flag || I2Utils.FindNextTag(line, tagEnd + 1, out tagStart, out tagEnd);
  101. }
  102. if (c > 'ÿ')
  103. {
  104. flag = true;
  105. }
  106. }
  107. return false;
  108. }
  109. public static bool IsPlaying()
  110. {
  111. return Application.isPlaying;
  112. }
  113. public static string GetPath(this Transform tr)
  114. {
  115. Transform parent = tr.parent;
  116. if (tr == null)
  117. {
  118. return tr.name;
  119. }
  120. return parent.GetPath() + "/" + tr.name;
  121. }
  122. public static Transform FindObject(string objectPath)
  123. {
  124. return I2Utils.FindObject(SceneManager.GetActiveScene(), objectPath);
  125. }
  126. public static Transform FindObject(Scene scene, string objectPath)
  127. {
  128. GameObject[] rootGameObjects = scene.GetRootGameObjects();
  129. for (int i = 0; i < rootGameObjects.Length; i++)
  130. {
  131. Transform transform = rootGameObjects[i].transform;
  132. if (transform.name == objectPath)
  133. {
  134. return transform;
  135. }
  136. if (objectPath.StartsWith(transform.name + "/"))
  137. {
  138. return I2Utils.FindObject(transform, objectPath.Substring(transform.name.Length + 1));
  139. }
  140. }
  141. return null;
  142. }
  143. public static Transform FindObject(Transform root, string objectPath)
  144. {
  145. for (int i = 0; i < root.childCount; i++)
  146. {
  147. Transform child = root.GetChild(i);
  148. if (child.name == objectPath)
  149. {
  150. return child;
  151. }
  152. if (objectPath.StartsWith(child.name + "/"))
  153. {
  154. return I2Utils.FindObject(child, objectPath.Substring(child.name.Length + 1));
  155. }
  156. }
  157. return null;
  158. }
  159. public static H FindInParents<H>(Transform tr) where H : Component
  160. {
  161. if (!tr)
  162. {
  163. return (H)((object)null);
  164. }
  165. H component = tr.GetComponent<H>();
  166. while (!component && tr)
  167. {
  168. component = tr.GetComponent<H>();
  169. tr = tr.parent;
  170. }
  171. return component;
  172. }
  173. }
  174. }