Tools.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections.Generic;
  4. namespace WXB
  5. {
  6. static public partial class Tools
  7. {
  8. public const int s_dyn_default_speed = 320;
  9. public const int s_offset_default_speed = 320;
  10. private static readonly VertexHelper s_VertexHelper = new VertexHelper();
  11. public static VertexHelper vertexHelper
  12. {
  13. get { return s_VertexHelper; }
  14. }
  15. // 左下角(Y轴朝下)坐标系转换为左上角(Y轴朝上)坐标系
  16. public static void LB2LT(ref Vector2 pos, float height)
  17. {
  18. pos.y = height - pos.y;
  19. }
  20. public static void LT2LB(ref Vector2 pos, float height)
  21. {
  22. pos.y += height;
  23. }
  24. static public Font GetFont(string name)
  25. {
  26. if (s_get_font == null)
  27. {
  28. Font f = SymbolTextInit.GetFont(name);
  29. if (f == null)
  30. return DefaultFont;
  31. }
  32. return s_get_font(name);
  33. }
  34. static Font DefaultFont;
  35. static public Font GetDefaultFont()
  36. {
  37. if (DefaultFont == null)
  38. DefaultFont = Object.FindObjectOfType<Font>();
  39. return DefaultFont;
  40. }
  41. // 得到精灵的接口
  42. public static System.Func<string, Sprite> s_get_sprite = null;
  43. // 得到字体的接口
  44. public static System.Func<string, Font> s_get_font = null;
  45. static public Sprite GetSprite(string name)
  46. {
  47. if (s_get_sprite == null)
  48. return SymbolTextInit.GetSprite(name);
  49. return s_get_sprite(name);
  50. }
  51. public static System.Func<string, Cartoon> s_get_cartoon = null;
  52. static public Cartoon GetCartoon(string name)
  53. {
  54. if (s_get_cartoon == null)
  55. return SymbolTextInit.GetCartoon(name);
  56. return s_get_cartoon(name);
  57. }
  58. public static System.Action<List<Cartoon>> s_get_cartoons = null;
  59. static public void GetAllCartoons(List<Cartoon> cartoons)
  60. {
  61. if (s_get_cartoons == null)
  62. {
  63. SymbolTextInit.GetCartoons(cartoons);
  64. return;
  65. }
  66. s_get_cartoons(cartoons);
  67. }
  68. public static void AddLine(VertexHelper vh, Vector2 leftPos, Vector2 uv, float width, float height, Color color)
  69. {
  70. // 有下划线
  71. Vector2 leftTop = new Vector2(leftPos.x, leftPos.y);
  72. int count = vh.currentVertCount;
  73. vh.AddVert(leftTop, color, uv);
  74. vh.AddVert(new Vector3(leftTop.x + width, leftTop.y, 0f), color, uv);
  75. vh.AddVert(new Vector3(leftTop.x + width, leftTop.y - height, 0f), color, uv);
  76. vh.AddVert(new Vector3(leftTop.x, leftTop.y - height, 0f), color, uv);
  77. vh.AddTriangle(count, count + 1, count + 2);
  78. vh.AddTriangle(count + 2, count + 3, count);
  79. }
  80. public static bool IsHexadecimal(char c)
  81. {
  82. if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
  83. return true;
  84. return false;
  85. }
  86. public static float stringToFloat(string str, float df)
  87. {
  88. float value = 0;
  89. if (float.TryParse(str, out value) == false)
  90. return df;
  91. return value;
  92. }
  93. public static int stringToInt(string str, int df)
  94. {
  95. int value = 0;
  96. if (int.TryParse(str, out value) == false)
  97. return df;
  98. return value;
  99. }
  100. public static Font ParserFontName(string text, ref int startpos)
  101. {
  102. int lenght = text.Length;
  103. string fontName = "";
  104. while (lenght > startpos && fontName.Length < 10)
  105. {
  106. fontName += text[startpos];
  107. startpos++;
  108. }
  109. Font font = null;
  110. while (fontName.Length != 0)
  111. {
  112. if ((font = GetFont(fontName)) != null)
  113. {
  114. break;
  115. }
  116. else
  117. {
  118. fontName = fontName.Remove(fontName.Length - 1, 1);
  119. startpos--;
  120. }
  121. }
  122. return font;
  123. }
  124. public static bool ScreenPointToWorldPointInRectangle(RectTransform rectTrans, Vector2 screenPoint, Camera cam, out Vector2 worldPoint)
  125. {
  126. if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTrans, screenPoint, cam, out worldPoint))
  127. return false;
  128. LB2LT(ref worldPoint, rectTrans.rect.height);
  129. return true;
  130. // worldPoint = Vector2.zero;
  131. // Ray ray = RectTransformUtility.ScreenPointToRay(cam, screenPoint);
  132. // float enter;
  133. // if (!new Plane(rectTrans.rotation * Vector3.back, rectTrans.position).Raycast(ray, out enter))
  134. // return false;
  135. // worldPoint = ray.GetPoint(enter);
  136. // worldPoint = rectTrans.worldToLocalMatrix.MultiplyPoint(worldPoint);
  137. }
  138. static public Color ParseColor(string text, int offset, Color defc)
  139. {
  140. Color c;
  141. if (ParseColor(text, offset, out c))
  142. return c;
  143. return defc;
  144. }
  145. static public bool ParseColor(string text, int offset, out Color color)
  146. {
  147. color = Color.white;
  148. int l = text.Length - offset;
  149. /*if (l >= 8)
  150. {
  151. color = ParseColor32(text, offset);
  152. return true;
  153. }
  154. else if (l >= 6)
  155. {
  156. color = ParseColor24(text, offset);
  157. return true;
  158. }*/
  159. if (l >= 6)
  160. {
  161. color = ParseColor24(text, offset);
  162. return true;
  163. }
  164. return false;
  165. }
  166. static public Color ParseColor24(string text, int offset)
  167. {
  168. int r = (HexToDecimal(text[offset]) << 4) | HexToDecimal(text[offset + 1]);
  169. int g = (HexToDecimal(text[offset + 2]) << 4) | HexToDecimal(text[offset + 3]);
  170. int b = (HexToDecimal(text[offset + 4]) << 4) | HexToDecimal(text[offset + 5]);
  171. float f = 1f / 255f;
  172. return new Color(f * r, f * g, f * b);
  173. }
  174. static public Color ParseColor32(string text, int offset)
  175. {
  176. int r = (HexToDecimal(text[offset]) << 4) | HexToDecimal(text[offset + 1]);
  177. int g = (HexToDecimal(text[offset + 2]) << 4) | HexToDecimal(text[offset + 3]);
  178. int b = (HexToDecimal(text[offset + 4]) << 4) | HexToDecimal(text[offset + 5]);
  179. int a = (HexToDecimal(text[offset + 6]) << 4) | HexToDecimal(text[offset + 7]);
  180. float f = 1f / 255f;
  181. return new Color(f * r, f * g, f * b, f * a);
  182. }
  183. static public int HexToDecimal(char ch)
  184. {
  185. switch (ch)
  186. {
  187. case '0': return 0x0;
  188. case '1': return 0x1;
  189. case '2': return 0x2;
  190. case '3': return 0x3;
  191. case '4': return 0x4;
  192. case '5': return 0x5;
  193. case '6': return 0x6;
  194. case '7': return 0x7;
  195. case '8': return 0x8;
  196. case '9': return 0x9;
  197. case 'a':
  198. case 'A': return 0xA;
  199. case 'b':
  200. case 'B': return 0xB;
  201. case 'c':
  202. case 'C': return 0xC;
  203. case 'd':
  204. case 'D': return 0xD;
  205. case 'e':
  206. case 'E': return 0xE;
  207. case 'f':
  208. case 'F': return 0xF;
  209. }
  210. return 0xF;
  211. }
  212. static public Color ParserColorName(string text, Color dc)
  213. {
  214. int startpos = 0;
  215. return ParserColorName(text, ref startpos, dc);
  216. }
  217. static public Color ParserColorName(string text, ref int startpos, Color dc)
  218. {
  219. if (startpos >= text.Length - 1)
  220. return dc;
  221. if (text[startpos + 1] == '[')
  222. {
  223. int endpos = text.IndexOf(']', startpos + 1);
  224. if (endpos != -1)
  225. {
  226. string name = text.Substring(startpos + 2, endpos - startpos - 2);
  227. startpos = endpos+1;
  228. return ColorConst.Get(name, dc);
  229. }
  230. ++startpos;
  231. return dc;
  232. }
  233. else
  234. {
  235. // c,后面接的字符定义为字体的颜色,如果后面字符不是数字,则颜色恢复为默认的颜色,最多六个数字
  236. int start_color = ++startpos;
  237. int color_lenght = 0;
  238. int lenght = text.Length;
  239. while (lenght > (startpos + color_lenght) && IsHexadecimal(text[startpos + color_lenght]) && color_lenght < 6)
  240. {
  241. ++color_lenght;
  242. }
  243. Color newCol;
  244. if (ParseColor(text.Substring(start_color, color_lenght), 0, out newCol))
  245. {
  246. startpos += color_lenght;
  247. return newCol;
  248. }
  249. else
  250. {
  251. startpos += color_lenght;
  252. return dc;
  253. }
  254. }
  255. }
  256. public static T AddChild<T>(this GameObject go) where T : MonoBehaviour
  257. {
  258. GameObject child = new GameObject();
  259. var t = child.AddComponent<T>();
  260. AddChild(go, child);
  261. return t;
  262. }
  263. public static void AddChild(this GameObject go, GameObject child)
  264. {
  265. Transform ctf = child.transform;
  266. ctf.SetParent(go.transform);
  267. ctf.localScale = Vector3.one;
  268. ctf.localPosition = Vector3.zero;
  269. ctf.localEulerAngles = Vector3.zero;
  270. child.layer = go.layer;
  271. }
  272. static public void Destroy(GameObject go)
  273. {
  274. #if UNITY_EDITOR
  275. if (Application.isPlaying)
  276. {
  277. Object.Destroy(go);
  278. //DelayDestory.Destroy(go);
  279. }
  280. else
  281. {
  282. Object.DestroyImmediate(go);
  283. //DelayDestory.DestroyImmediate(go);
  284. }
  285. #else
  286. Object.Destroy(go);
  287. //DelayDestory.Destroy(go);
  288. #endif
  289. }
  290. public static void UpdateRect(RectTransform child, Vector2 offset)
  291. {
  292. RectTransform parent = child.parent as RectTransform;
  293. if (parent == null)
  294. return;
  295. child.pivot = parent.pivot;
  296. child.anchorMin = Vector2.zero;
  297. child.anchorMax = Vector2.one;
  298. child.offsetMax = Vector2.zero;
  299. child.offsetMin = Vector2.zero;
  300. child.localPosition = offset;
  301. child.localScale = Vector3.one;
  302. child.localEulerAngles = Vector3.zero;
  303. }
  304. }
  305. }