TextSpacing.cs 6.1 KB

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