源战役客户端
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

145 wiersze
4.4 KiB

1 miesiąc temu
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. [AddComponentMenu("UI/Effects/TextSpacing")]
  5. public class TextSpacing : BaseMeshEffect
  6. {
  7. #region Struct
  8. public enum HorizontalAligmentType
  9. {
  10. Left,
  11. Center,
  12. Right
  13. }
  14. public class Line
  15. {
  16. // 起点索引
  17. public int StartVertexIndex { get { return _startVertexIndex; } }
  18. private int _startVertexIndex = 0;
  19. // 终点索引
  20. public int EndVertexIndex { get { return _endVertexIndex; } }
  21. private int _endVertexIndex = 0;
  22. // 该行占的点数目
  23. public int VertexCount { get { return _vertexCount; } }
  24. private int _vertexCount = 0;
  25. public Line(int startVertexIndex, int length)
  26. {
  27. _startVertexIndex = startVertexIndex;
  28. _endVertexIndex = length * 6 - 1 + startVertexIndex;
  29. _vertexCount = length * 6;
  30. }
  31. }
  32. #endregion
  33. public float Spacing = 1f;
  34. public override void ModifyMesh(VertexHelper vh)
  35. {
  36. if (!IsActive() || vh.currentVertCount == 0)
  37. {
  38. return;
  39. }
  40. var text = GetComponent<Text>();
  41. if (text == null)
  42. {
  43. Debug.LogError("Missing Text component");
  44. return;
  45. }
  46. // 水平对齐方式
  47. HorizontalAligmentType alignment;
  48. if (text.alignment == TextAnchor.LowerLeft || text.alignment == TextAnchor.MiddleLeft || text.alignment == TextAnchor.UpperLeft)
  49. {
  50. alignment = HorizontalAligmentType.Left;
  51. }
  52. else if (text.alignment == TextAnchor.LowerCenter || text.alignment == TextAnchor.MiddleCenter || text.alignment == TextAnchor.UpperCenter)
  53. {
  54. alignment = HorizontalAligmentType.Center;
  55. }
  56. else
  57. {
  58. alignment = HorizontalAligmentType.Right;
  59. }
  60. var vertexs = new List<UIVertex>();
  61. vh.GetUIVertexStream(vertexs);
  62. // var indexCount = vh.currentIndexCount;
  63. var lineTexts = text.text.Split('\n');
  64. var lines = new Line[lineTexts.Length];
  65. // 根据lines数组中各个元素的长度计算每一行中第一个点的索引,每个字、字母、空母均占6个点
  66. for (var i = 0; i < lines.Length; i++)
  67. {
  68. // 除最后一行外,vertexs对于前面几行都有回车符占了6个点
  69. if (i == 0)
  70. {
  71. lines[i] = new Line(0, lineTexts[i].Length + 1);
  72. }
  73. else if (i > 0 && i < lines.Length - 1)
  74. {
  75. lines[i] = new Line(lines[i - 1].EndVertexIndex + 1, lineTexts[i].Length + 1);
  76. }
  77. else
  78. {
  79. lines[i] = new Line(lines[i - 1].EndVertexIndex + 1, lineTexts[i].Length);
  80. }
  81. }
  82. UIVertex vt;
  83. for (var i = 0; i < lines.Length; i++)
  84. {
  85. for (var j = lines[i].StartVertexIndex; j <= lines[i].EndVertexIndex; j++)
  86. {
  87. if (j < 0 || j >= vertexs.Count)
  88. {
  89. continue;
  90. }
  91. vt = vertexs[j];
  92. var charCount = lines[i].EndVertexIndex - lines[i].StartVertexIndex;
  93. if (i == lines.Length - 1)
  94. {
  95. charCount += 6;
  96. }
  97. if (alignment == HorizontalAligmentType.Left)
  98. {
  99. vt.position += new Vector3(Spacing * ((j - lines[i].StartVertexIndex) / 6), 0, 0);
  100. }
  101. else if (alignment == HorizontalAligmentType.Right)
  102. {
  103. vt.position += new Vector3(Spacing * (-(charCount - j + lines[i].StartVertexIndex) / 6 + 1), 0, 0);
  104. }
  105. else if (alignment == HorizontalAligmentType.Center)
  106. {
  107. var offset = (charCount / 6) % 2 == 0 ? 0.5f : 0f;
  108. vt.position += new Vector3(Spacing * ((j - lines[i].StartVertexIndex) / 6 - charCount / 12 + offset), 0, 0);
  109. }
  110. vertexs[j] = vt;
  111. // 以下注意点与索引的对应关系
  112. if (j % 6 <= 2)
  113. {
  114. vh.SetUIVertex(vt, (j / 6) * 4 + j % 6);
  115. }
  116. if (j % 6 == 4)
  117. {
  118. vh.SetUIVertex(vt, (j / 6) * 4 + j % 6 - 1);
  119. }
  120. }
  121. }
  122. }
  123. }