UGUIText显示效果两边对齐

每个字符的显示由两个三角形网格组成,一共六个顶点构成一个字符

0-左上

1-右上

2-右下

3-右下

4-左下

5-左上

UI有四个点,顶点有六个点,做好对应关系

效果如下:

detailText.GetOrAddComponent<TextSpacing>();

using UnityEngine;

using UnityEngine.UI;

using System.Collections.Generic;

internal class Line

{

    //每行开始顶点索引

    private int startVertexIndex;

    public int StartVertexIndex

    {

        get

        {

            return startVertexIndex;

        }

    }

    //每行结束顶点索引

    private int endVertexIndex;

    public int EndVertexIndex

    {

        get

        {

            return endVertexIndex;

        }

    }

    //每行顶点总量

    private int countVertexIndex;

    public int CountVertexIndex

    {

        get

        {

            return countVertexIndex;

        }

    }

    public Line(int startVertexIndex)

    {

        this.startVertexIndex = startVertexIndex;

    }

    public void SetLineEndVertexIndex(int endVertexIndex)

    {

        this.endVertexIndex = endVertexIndex;

        this.countVertexIndex = endVertexIndex-startVertexIndex+1;

    }

}

public class TextSpacing : BaseMeshEffect

{

    public float spacing = 0;

    public override void ModifyMesh(VertexHelper vh)

    {

        Text text = GetComponent<Text>();

        if(text == null || text.text == null){

            return;

        }

        //有换行符的情况

        string[] lineTexts = text.text.Split('\n');

        int start = 0;

        List<int> lineStartIndex = new List<int>();

        for(int k = 0 ; k < lineTexts.Length ; k ++){

            if(k != 0){

                start = start + (lineTexts[k].Length+1)*6;

            }

            lineStartIndex.Add(start);

        }

        List<Line> line = new List<Line>();

        List<float> lastXList = new List<float>();

        List<UIVertex> vertexs = new List<UIVertex>();

        vh.GetUIVertexStream(vertexs);

        int countVertexIndex = vertexs.Count;

        for (int i = 0; i < vertexs.Count; i++)

        {

            if(i == 0){

                line.Add(new Line(0));

            }else if(i == vertexs.Count-1){

                line[line.Count-1].SetLineEndVertexIndex(i);

                lastXList.Add(vertexs[i-2].position.x);

            }else{

                if(i % 6 == 0){

                    if(vertexs[i].position.x < vertexs[i-6].position.x){

                        line[line.Count-1].SetLineEndVertexIndex(i-1);

                        line.Add(new Line(i));

                        lastXList.Add(vertexs[i-3].position.x);

                    }

                }

            }

        }

        // DDebug.ZMLog("=====line.Count======"+line.Count);

        // for (int i = 0 ; i < line.Count; i++){

        //      DDebug.ZMLog("====line======="+line[i].StartVertexIndex+"======="+line[i].EndVertexIndex);

        // }

        // for (int i = 0 ; i < lastXList.Count; i++){

        //      DDebug.ZMLog("====lastXList======="+lastXList[i]);

        // }

        int langIndex = GetLongIndex(lastXList);

        for (int i = 0; i < line.Count-1; i++)

        {

            if(langIndex == i){

                continue;

            }

            if(lineStartIndex.Contains(line[i].EndVertexIndex +1)){

                continue;

            }

            spacing = (lastXList[langIndex]-lastXList[i])/(line[i].CountVertexIndex/6);

            for (int k = line[i].StartVertexIndex; k <= line[i].EndVertexIndex; k++)

            {

                UIVertex vertex = vertexs[k];   

                vertex.position += new Vector3(spacing * ((k-line[i].StartVertexIndex) / 6), 0, 0);

                vertexs[k] = vertex;

                if (k % 6 <= 2)

                {

                    vh.SetUIVertex(vertex, (k / 6) * 4 + k % 6);

                }

                if (k % 6 == 4)

                {

                    vh.SetUIVertex(vertex, (k / 6) * 4 + k % 6 - 1);

                }

            }

        }

    }

    private int GetLongIndex(List<float> list)

    {

        int index = 0;

        for(int i = 1; i < list.Count ; i ++){

            if(list[i] > list[index]){

                index = i;

            }

        }

        return index;

    }

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值