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以前版本:优化代码

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<Triangle>
    {
        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<UIVertex> verts = new List<UIVertex>();
    List<Triangle> tris = new List<Triangle>();

    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<UIVertex> 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<UIVertex> verts = new List<UIVertex>();

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


    void OptimizeVert(ref List<UIVertex> vertices)
    {
        List<Triangle> tris = new List<Triangle>();
        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();
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值