TextSpacing.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. [AddComponentMenu("UI/Effects/TextSpacing")]
  6. public class TextSpacing : BaseMeshEffect
  7. {
  8. [SerializeField]
  9. private float spacing_x;
  10. //[SerializeField]
  11. private float spacing_y = 0;
  12. private const int VERTEXT_RANGE = 6;
  13. private List<UIVertex> mVertexList;
  14. public override void ModifyMesh(VertexHelper vh)
  15. {
  16. if (spacing_x == 0 && spacing_y == 0) { return; }
  17. if (!IsActive()) { return; }
  18. int count = vh.currentVertCount;
  19. if (count == 0) { return; }
  20. if (mVertexList == null) { mVertexList = new List<UIVertex>(); }
  21. vh.GetUIVertexStream(mVertexList);
  22. int row = 1;
  23. int column = 2;
  24. List<UIVertex> sub_vertexs = mVertexList.GetRange(0, VERTEXT_RANGE);
  25. float min_row_left = sub_vertexs.Min(v => v.position.x);
  26. int vertex_count = mVertexList.Count;
  27. for (int i = VERTEXT_RANGE; i < vertex_count;)
  28. {
  29. if (i % VERTEXT_RANGE == 0)
  30. {
  31. sub_vertexs = mVertexList.GetRange(i, VERTEXT_RANGE);
  32. float tem_row_left = sub_vertexs.Min(v => v.position.x);
  33. if (min_row_left - tem_row_left >= -10)
  34. {
  35. min_row_left = tem_row_left;
  36. ++row;
  37. column = 1;
  38. //continue;
  39. }
  40. }
  41. for (int j = 0; j < VERTEXT_RANGE; j++)
  42. {
  43. UIVertex vertex = mVertexList[i];
  44. vertex.position += Vector3.right * (column - 1) * spacing_x;
  45. vertex.position += Vector3.down * (row - 1) * spacing_y;
  46. mVertexList[i] = vertex;
  47. ++i;
  48. }
  49. ++column;
  50. }
  51. vh.Clear();
  52. vh.AddUIVertexTriangleStream(mVertexList);
  53. }
  54. }