TextSpacing.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // An highlighted block
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. namespace TextSpacing
  8. {
  9. public class Line
  10. {
  11. private int _startVertexIndex = 0;
  12. /// <summary>
  13. /// 起点索引
  14. /// </summary>
  15. public int StartVertexIndex
  16. {
  17. get
  18. {
  19. return _startVertexIndex;
  20. }
  21. }
  22. private int _endVertexIndex = 0;
  23. /// <summary>
  24. /// 终点索引
  25. /// </summary>
  26. public int EndVertexIndex
  27. {
  28. get
  29. {
  30. return _endVertexIndex;
  31. }
  32. }
  33. private int _vertexCount = 0;
  34. /// <summary>
  35. /// 该行占的点数目
  36. /// </summary>
  37. public int VertexCount
  38. {
  39. get
  40. {
  41. return _vertexCount;
  42. }
  43. }
  44. public Line(int startVertexIndex, int length)
  45. {
  46. _startVertexIndex = startVertexIndex;
  47. _endVertexIndex = length * 6 - 1 + startVertexIndex;
  48. _vertexCount = length * 6;
  49. }
  50. }
  51. [AddComponentMenu("UI/Effects/TextSpacing")]
  52. public class TextSpacing : BaseMeshEffect
  53. {
  54. public float _textSpacing = 1f;
  55. public override void ModifyMesh(VertexHelper vh)
  56. {
  57. if (!IsActive() || vh.currentVertCount == 0)
  58. {
  59. return;
  60. }
  61. Text text = GetComponent<Text>();
  62. if (text == null)
  63. {
  64. Debug.LogError("Missing Text component");
  65. return;
  66. }
  67. List<UIVertex> vertexs = new List<UIVertex>();
  68. vh.GetUIVertexStream(vertexs);
  69. int indexCount = vh.currentIndexCount;
  70. string[] lineTexts = text.text.Split('\n');
  71. Line[] lines = new Line[lineTexts.Length];
  72. //根据lines数组中各个元素的长度计算每一行中第一个点的索引,每个字、字母、空母均占6个点
  73. for (int i = 0; i < lines.Length; i++)
  74. {
  75. //除最后一行外,vertexs对于前面几行都有回车符占了6个点
  76. if (i == 0)
  77. {
  78. lines[i] = new Line(0, lineTexts[i].Length + 1);
  79. }
  80. else if (i > 0 && i < lines.Length - 1)
  81. {
  82. lines[i] = new Line(lines[i - 1].EndVertexIndex + 1, lineTexts[i].Length + 1);
  83. }
  84. else
  85. {
  86. lines[i] = new Line(lines[i - 1].EndVertexIndex + 1, lineTexts[i].Length);
  87. }
  88. }
  89. UIVertex vt;
  90. for (int i = 0; i < lines.Length; i++)
  91. {
  92. Vector3 startPos = Vector3.zero;
  93. Vector3 endPos = Vector3.zero;
  94. Vector3 defaultStartPos = Vector3.zero;
  95. Vector3 defaultEndPos = Vector3.zero;
  96. for (int j = lines[i].StartVertexIndex; j <= lines[i].EndVertexIndex; j++)
  97. {
  98. if (j < 0 || j >= vertexs.Count)
  99. {
  100. continue;
  101. }
  102. vt = vertexs[j];
  103. if (defaultStartPos == Vector3.zero)
  104. {
  105. defaultStartPos = new Vector3(vt.position.x, vt.position.y, vt.position.z);
  106. }
  107. defaultEndPos = new Vector3(vt.position.x, vt.position.y, vt.position.z);
  108. if (j != 0)
  109. {
  110. vt.position += new Vector3(_textSpacing * ((j - lines[i].StartVertexIndex) / 6), 0, 0);
  111. }
  112. if (startPos == Vector3.zero)
  113. {
  114. startPos = new Vector3(vt.position.x, vt.position.y, vt.position.z);
  115. }
  116. endPos = new Vector3(vt.position.x, vt.position.y, vt.position.z);
  117. vertexs[j] = vt;
  118. //以下注意点与索引的对应关系
  119. if (j % 6 <= 2)
  120. {
  121. vh.SetUIVertex(vt, (j / 6) * 4 + j % 6);
  122. }
  123. if (j % 6 == 4)
  124. {
  125. vh.SetUIVertex(vt, (j / 6) * 4 + j % 6 - 1);
  126. }
  127. }
  128. if (text.alignment == TextAnchor.MiddleCenter || text.alignment == TextAnchor.UpperCenter || text.alignment == TextAnchor.LowerCenter)
  129. {
  130. Vector3 defaultCenterPos = defaultStartPos + (defaultEndPos - defaultStartPos) / 2;
  131. Vector3 centerPos = startPos + (endPos - startPos) / 2;
  132. for (int j = lines[i].StartVertexIndex; j <= lines[i].EndVertexIndex; j++)
  133. {
  134. if (j < 0 || j >= vertexs.Count)
  135. {
  136. continue;
  137. }
  138. vt = vertexs[j];
  139. vt.position = vt.position + defaultCenterPos - centerPos;
  140. vertexs[j] = vt;
  141. //以下注意点与索引的对应关系
  142. if (j % 6 <= 2)
  143. {
  144. vh.SetUIVertex(vt, (j / 6) * 4 + j % 6);
  145. }
  146. if (j % 6 == 4)
  147. {
  148. vh.SetUIVertex(vt, (j / 6) * 4 + j % 6 - 1);
  149. }
  150. }
  151. }
  152. if (text.alignment == TextAnchor.MiddleRight || text.alignment == TextAnchor.UpperRight || text.alignment == TextAnchor.LowerRight)
  153. {
  154. Vector3 defaultRightPos = defaultEndPos;
  155. Vector3 rightPos = endPos;
  156. for (int j = lines[i].StartVertexIndex; j <= lines[i].EndVertexIndex; j++)
  157. {
  158. if (j < 0 || j >= vertexs.Count)
  159. {
  160. continue;
  161. }
  162. vt = vertexs[j];
  163. vt.position = vt.position + defaultRightPos - rightPos;
  164. vertexs[j] = vt;
  165. //以下注意点与索引的对应关系
  166. if (j % 6 <= 2)
  167. {
  168. vh.SetUIVertex(vt, (j / 6) * 4 + j % 6);
  169. }
  170. if (j % 6 == 4)
  171. {
  172. vh.SetUIVertex(vt, (j / 6) * 4 + j % 6 - 1);
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }