Unity 富文本顶点数优化

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

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

这是正常状态下text文本:

Unity 富文本顶点数优化_第1张图片

使用大量富文本的text文本:1

Unity 富文本顶点数优化_第2张图片

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

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

Unity 富文本顶点数优化_第3张图片

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

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class UIVertexOptimize : BaseMeshEffect
{
    struct Triangle
    {
        public UIVertex v1;
        public UIVertex v2;
        public UIVertex v3;
    }

    class TriangleCompare : IEqualityComparer
    {
        public bool Equals(Triangle x, Triangle y)
        {
            return UIVertexEquals(x.v1, y.v1) && UIVertexEquals(x.v2, y.v2) && UIVertexEquals(x.v3, y.v3);
        }

        public int GetHashCode(Triangle obj)
        {
            return GetUIVertexHashCode(obj.v1)
                   ^ GetUIVertexHashCode(obj.v2)
                   ^ GetUIVertexHashCode(obj.v3);
        }

        int GetUIVertexHashCode(UIVertex vertex)
        {
            return vertex.color.a.GetHashCode()
                ^ vertex.color.b.GetHashCode()
                ^ vertex.color.g.GetHashCode()
                ^ vertex.color.r.GetHashCode()
                   ^ vertex.normal.GetHashCode()
                   ^ vertex.position.GetHashCode()
                   ^ vertex.tangent.GetHashCode()
                   ^ vertex.uv0.GetHashCode()
                   ^ vertex.uv1.GetHashCode();
        }

        bool UIVertexEquals(UIVertex x, UIVertex y)
        {
            return x.color.a == y.color.a
                   && x.color.b == y.color.b
                   && x.color.g == y.color.g
                   && x.color.r == y.color.r
                   && x.normal == y.normal
                   && x.position == y.position
                   && x.tangent == y.tangent
                   && x.uv1 == y.uv1
                   && x.uv0 == y.uv0;
        }
    }

    List verts = new List();
    List tris = new List();

    public override void ModifyMesh(VertexHelper vh)
    {
        vh.GetUIVertexStream(verts);
        Debug.Log(verts.Count);

        OptimizeVert(ref verts);
        Debug.Log(verts.Count);
        vh.Clear();
        vh.AddUIVertexTriangleStream(verts);
    }

    void OptimizeVert(ref List vertices)
    {
        if (tris.Capacity < vertices.Count / 3)
        {
            tris.Capacity = vertices.Count;
        }
        for (int i = 0; i <= vertices.Count - 3; i += 3)
        {
            tris.Add(new Triangle() { v1 = vertices[i], v2 = vertices[i + 1], v3 = vertices[i + 2] });
        }
        vertices.Clear();

        vertices.AddRange(tris.Distinct(new TriangleCompare()).SelectMany(t => new[]
        {
            t.v1,
            t.v2,
            t.v3
        }));
        tris.Clear();
    }
}

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

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class UIVertexOptimize : BaseMeshEffect
{
    struct Triangle
    {
        public UIVertex v1;
        public UIVertex v2;
        public UIVertex v3;
    }

    List verts = new List();

    public override void ModifyMesh(VertexHelper vh)
    {
        vh.GetUIVertexStream(verts);
        OptimizeVert(ref verts);
        vh.Clear();
        vh.AddUIVertexTriangleStream(verts);
    }


    void OptimizeVert(ref List vertices)
    {
        List tris = new List();
        for (int i = 0; i < vertices.Count - 3; i += 3)
        {
            tris.Add(new Triangle() { v1 = vertices[i], v2 = vertices[i + 1], v3 = vertices[i + 2] });
        }
        vertices = tris.Distinct().SelectMany(tri =>
            new[]{
                tri.v1,
                tri.v2,
                tri.v3
            }).ToList();
    }
}

 

你可能感兴趣的:(unity3d)