UITextSpacing.cs 3.5 KB

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