TextPro.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. [AddComponentMenu("UI/Extensions/TextPro")]
  7. /// <summary>
  8. /// 扩展Text 这几个标签不要在同一行使用多个
  9. /// 1.增强RichText 支持<align="right">Right <align="center">Center <align="left"> Left
  10. /// </summary>
  11. public class TextPro : Text
  12. {
  13. protected override void OnPopulateMesh(VertexHelper toFill)
  14. {
  15. RichTextAlignDataSupport(toFill);
  16. }
  17. private void RemoveSameAlignData(List<AlignData> alignDatas)
  18. {
  19. IList<UILineInfo> lines = cachedTextGenerator.lines;
  20. List<AlignData> removeAlignDatas = new List<AlignData>();
  21. for (int i = 0; i < alignDatas.Count; i++)
  22. {
  23. AlignData alignData = alignDatas[i];
  24. for (int j = 0; j < lines.Count; j++)
  25. {
  26. UILineInfo lInfo = lines[j];
  27. if (lInfo.startCharIdx <= alignData.startCharIndex)
  28. {
  29. alignData.lineIndex = j;
  30. alignData.lineStartCharIndex = lInfo.startCharIdx;
  31. if (j == lines.Count - 1)
  32. {
  33. if (alignData.startCharIndex > cachedTextGenerator.characterCountVisible)
  34. {
  35. removeAlignDatas.Add(alignData);
  36. }
  37. else
  38. alignData.lineEndCharIndex = cachedTextGenerator.characterCountVisible;
  39. }
  40. }
  41. else
  42. {
  43. alignData.lineEndCharIndex = lInfo.startCharIdx - 1;
  44. break;
  45. }
  46. }
  47. alignDatas[i] = alignData;
  48. }
  49. List<int> lineList = new List<int>();
  50. for (int i = 0; i < alignDatas.Count; i++)
  51. {
  52. AlignData alignData = alignDatas[i];
  53. //Debug.Log("alignData.lineIndex :" + alignData.lineIndex);
  54. if (lineList.Contains(alignData.lineIndex))
  55. {
  56. removeAlignDatas.Add(alignData);
  57. continue;
  58. }
  59. else
  60. {
  61. lineList.Add(alignData.lineIndex);
  62. }
  63. }
  64. for (int i = 0; i < removeAlignDatas.Count; i++)
  65. {
  66. alignDatas.Remove(removeAlignDatas[i]);
  67. }
  68. }
  69. private void RichTextAlignDataSupport(VertexHelper toFill)
  70. {
  71. if (!supportRichText)
  72. {
  73. base.OnPopulateMesh(toFill);
  74. return;
  75. }
  76. List<AlignData> alignDatas=new List<AlignData>();
  77. //string changedText = "";
  78. var orignText = m_Text;
  79. m_Text = DealWithTextContent(m_Text,ref alignDatas);
  80. //changedText = m_Text;
  81. base.OnPopulateMesh(toFill);
  82. m_Text = orignText;
  83. RectTransform rectTransform = GetComponent<RectTransform>();
  84. float rangeWith =rectTransform.sizeDelta.x;
  85. IList<UICharInfo> characters = cachedTextGenerator.characters;
  86. Rect rectExtents = cachedTextGenerator.rectExtents;
  87. List<UIVertex> stream = new List<UIVertex>();
  88. toFill.GetUIVertexStream(stream);
  89. toFill.Clear();
  90. RemoveSameAlignData(alignDatas);
  91. for (int i = 0; i < alignDatas.Count; i++)
  92. {
  93. AlignData alignData = alignDatas[i];
  94. //Debug.Log("characters.Count :" + characters.Count + " stream.Count:" + stream.Count + " alignData.lineEndCharIndex:" + alignData.lineEndCharIndex);
  95. if (alignData.lineEndCharIndex >= characters.Count)
  96. continue;
  97. if (alignData.lineStartCharIndex >= characters.Count)
  98. continue;
  99. if ((alignData.lineEndCharIndex * 6) > stream.Count)
  100. continue;
  101. if (alignData.lineStartCharIndex * 6 >= stream.Count)
  102. continue;
  103. int indexEnd = alignData.lineEndCharIndex * 6 - 3;
  104. int indexStart = alignData.lineStartCharIndex * 6;
  105. if (alignData.alignType == AlignType.Right)
  106. {
  107. float detaMove = rangeWith / 2 - stream[indexEnd].position.x;
  108. for (int v = alignData.lineStartCharIndex * 6; v < alignData.lineEndCharIndex * 6; v++)
  109. {
  110. UIVertex ver = stream[v];
  111. //if (v >= ((alignData.lineEndCharIndex - 1) * 6) && v <= (alignData.lineEndCharIndex * 6))
  112. //Debug.Log("Pos :" + ver.position);
  113. ver.position += new Vector3(detaMove, 0, 0);
  114. // ver.color = Color.red;
  115. stream[v] = ver;
  116. }
  117. }
  118. else if (alignData.alignType == AlignType.Left)
  119. {
  120. float detaMove = (-rangeWith / 2) - stream[indexStart].position.x;
  121. //Debug.Log("LLeft alignData.lineStartCharIndex:" + alignData.lineStartCharIndex + " alignData.lineEndCharIndex:" + alignData.lineEndCharIndex);
  122. for (int v = alignData.lineStartCharIndex * 6; v < alignData.lineEndCharIndex * 6; v++)
  123. {
  124. UIVertex ver = stream[v];
  125. ver.position += new Vector3(detaMove, 0, 0);
  126. //if(v== (alignData.lineEndCharIndex * 6-1))
  127. //ver.color = Color.green;
  128. stream[v] = ver;
  129. }
  130. }
  131. else if (alignData.alignType == AlignType.Center)
  132. {
  133. float lineCharLenth = Mathf.Abs(stream[indexEnd].position.x) + Mathf.Abs(stream[indexStart].position.x);
  134. float detaMove = (lineCharLenth) / 2 - stream[indexEnd].position.x;
  135. for (int v = alignData.lineStartCharIndex * 6; v < alignData.lineEndCharIndex * 6; v++)
  136. {
  137. UIVertex ver = stream[v];
  138. ver.position += new Vector3(detaMove, 0, 0);
  139. //ver.color = Color.blue;
  140. stream[v] = ver;
  141. }
  142. }
  143. }
  144. toFill.AddUIVertexTriangleStream(stream);
  145. }
  146. private string DealWithTextContent(string content,ref List<AlignData> alignDatas)
  147. {
  148. if (alignDatas == null)
  149. alignDatas = new List<AlignData>();
  150. alignDatas.Clear();
  151. var temp = content;
  152. while (true)
  153. {
  154. for (int i = 0; i < temp.Length; i++)
  155. {
  156. if (temp[i] == '<')
  157. {
  158. int surplusLenth = temp.Length - i - 1;
  159. string testStr = "";
  160. bool isResult = false;
  161. if (surplusLenth >= alignRightString.Length)
  162. {
  163. testStr = temp.Substring(i, alignRightString.Length);
  164. if (testStr == alignRightString)
  165. {
  166. isResult = true;
  167. AlignData align = new AlignData();
  168. align.alignType = AlignType.Right;
  169. align.startCharIndex = i;
  170. alignDatas.Add(align);
  171. }
  172. }
  173. if (!isResult)
  174. {
  175. if (surplusLenth >= alignLeftString.Length)
  176. {
  177. testStr = temp.Substring(i, alignLeftString.Length);
  178. if (testStr == alignLeftString)
  179. {
  180. isResult = true;
  181. AlignData align = new AlignData();
  182. align.alignType = AlignType.Left;
  183. align.startCharIndex = i;
  184. alignDatas.Add(align);
  185. }
  186. }
  187. }
  188. if (!isResult)
  189. {
  190. if (surplusLenth >= alignCenterString.Length)
  191. {
  192. testStr = temp.Substring(i, alignCenterString.Length);
  193. if (testStr == alignCenterString)
  194. {
  195. isResult = true;
  196. AlignData align = new AlignData();
  197. align.alignType = AlignType.Center;
  198. align.startCharIndex = i;
  199. alignDatas.Add(align);
  200. }
  201. }
  202. }
  203. if (isResult)
  204. {
  205. temp = temp.Remove(i, testStr.Length);
  206. i = 0;
  207. }
  208. }
  209. }
  210. break;
  211. }
  212. //Debug.Log("alignDatas :" + alignDatas.Count);
  213. return temp;
  214. }
  215. private const string alignRightString = "<align=\"right\">";
  216. private const string alignLeftString = "<align=\"left\">";
  217. private const string alignCenterString = "<align=\"center\">";
  218. }
  219. public struct AlignData
  220. {
  221. public AlignType alignType;
  222. public int startCharIndex;
  223. public int lineEndCharIndex;
  224. public int lineStartCharIndex;
  225. public int lineIndex;
  226. }
  227. public enum AlignType
  228. {
  229. Right,
  230. Left,
  231. Center,
  232. }