当前位置:   article > 正文

Unity 富文本顶点数优化_unity 富文本顶点优化

unity 富文本顶点优化

UGUI的Text组件勾选Rich Text复选框后支持富文本,当文本中的富文本标签非常多时,会发现三角面和顶点数会非常多,而实际显示的文字会很少,顶点数有时会多到报错。

原因:一个字符会生成6个顶点,6个顶点构成2个三角面,text会把富文本标签也进行计算,所以我们需要把Text组件下,将重复的三角面过滤掉

这是正常状态下text文本:

使用大量富文本的text文本:<size=30><b><color=#000000ff>1</color></b></size>

优化后的text文本,不过有点要注意的:

Unity5.5版本以前还存在foreach迭代操作问题,会产生堆内存分配。不过5.5说明中说已经修复了该问题。

Unity5.5以前版本:优化代码

  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class UIVertexOptimize : BaseMeshEffect
  6. {
  7. struct Triangle
  8. {
  9. public UIVertex v1;
  10. public UIVertex v2;
  11. public UIVertex v3;
  12. }
  13. class TriangleCompare : IEqualityComparer<Triangle>
  14. {
  15. public bool Equals(Triangle x, Triangle y)
  16. {
  17. return UIVertexEquals(x.v1, y.v1) && UIVertexEquals(x.v2, y.v2) && UIVertexEquals(x.v3, y.v3);
  18. }
  19. public int GetHashCode(Triangle obj)
  20. {
  21. return GetUIVertexHashCode(obj.v1)
  22. ^ GetUIVertexHashCode(obj.v2)
  23. ^ GetUIVertexHashCode(obj.v3);
  24. }
  25. int GetUIVertexHashCode(UIVertex vertex)
  26. {
  27. return vertex.color.a.GetHashCode()
  28. ^ vertex.color.b.GetHashCode()
  29. ^ vertex.color.g.GetHashCode()
  30. ^ vertex.color.r.GetHashCode()
  31. ^ vertex.normal.GetHashCode()
  32. ^ vertex.position.GetHashCode()
  33. ^ vertex.tangent.GetHashCode()
  34. ^ vertex.uv0.GetHashCode()
  35. ^ vertex.uv1.GetHashCode();
  36. }
  37. bool UIVertexEquals(UIVertex x, UIVertex y)
  38. {
  39. return x.color.a == y.color.a
  40. && x.color.b == y.color.b
  41. && x.color.g == y.color.g
  42. && x.color.r == y.color.r
  43. && x.normal == y.normal
  44. && x.position == y.position
  45. && x.tangent == y.tangent
  46. && x.uv1 == y.uv1
  47. && x.uv0 == y.uv0;
  48. }
  49. }
  50. List<UIVertex> verts = new List<UIVertex>();
  51. List<Triangle> tris = new List<Triangle>();
  52. public override void ModifyMesh(VertexHelper vh)
  53. {
  54. vh.GetUIVertexStream(verts);
  55. Debug.Log(verts.Count);
  56. OptimizeVert(ref verts);
  57. Debug.Log(verts.Count);
  58. vh.Clear();
  59. vh.AddUIVertexTriangleStream(verts);
  60. }
  61. void OptimizeVert(ref List<UIVertex> vertices)
  62. {
  63. if (tris.Capacity < vertices.Count / 3)
  64. {
  65. tris.Capacity = vertices.Count;
  66. }
  67. for (int i = 0; i <= vertices.Count - 3; i += 3)
  68. {
  69. tris.Add(new Triangle() { v1 = vertices[i], v2 = vertices[i + 1], v3 = vertices[i + 2] });
  70. }
  71. vertices.Clear();
  72. vertices.AddRange(tris.Distinct(new TriangleCompare()).SelectMany(t => new[]
  73. {
  74. t.v1,
  75. t.v2,
  76. t.v3
  77. }));
  78. tris.Clear();
  79. }
  80. }

Unity5.5及之后版本:优化Text

  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class UIVertexOptimize : BaseMeshEffect
  6. {
  7. struct Triangle
  8. {
  9. public UIVertex v1;
  10. public UIVertex v2;
  11. public UIVertex v3;
  12. }
  13. List<UIVertex> verts = new List<UIVertex>();
  14. public override void ModifyMesh(VertexHelper vh)
  15. {
  16. vh.GetUIVertexStream(verts);
  17. OptimizeVert(ref verts);
  18. vh.Clear();
  19. vh.AddUIVertexTriangleStream(verts);
  20. }
  21. void OptimizeVert(ref List<UIVertex> vertices)
  22. {
  23. List<Triangle> tris = new List<Triangle>();
  24. for (int i = 0; i < vertices.Count - 3; i += 3)
  25. {
  26. tris.Add(new Triangle() { v1 = vertices[i], v2 = vertices[i + 1], v3 = vertices[i + 2] });
  27. }
  28. vertices = tris.Distinct().SelectMany(tri =>
  29. new[]{
  30. tri.v1,
  31. tri.v2,
  32. tri.v3
  33. }).ToList();
  34. }
  35. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/112676
推荐阅读
相关标签
  

闽ICP备14008679号