Unity UGUI Text间隔

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.Collections.Generic;
public enum HorizontalAligmentType
{
    Left,
    Center,
    Right
}
public class Line
{
    private int _midVertexIndex = 0;
    /// <summary>
    /// 中点索引
    /// </summary>
    public int MidVertexIndex
    {
        get
        {
            return _midVertexIndex;
        }
    }
    private int _startVertexIndex = 0;
    /// <summary>
    /// 起点索引
    /// </summary>
    public int StartVertexIndex
    {
        get
        {
            return _startVertexIndex;
        }
    }
    private int _endVertexIndex = 0;
    /// <summary>
    /// 终点索引
    /// </summary>
    public int EndVertexIndex
    {
        get
        {
            return _endVertexIndex;
        }
    }
    private int _vertexCount = 0;
    /// <summary>
    /// 该行占的点数目
    /// </summary>
    public int VertexCount
    {
        get
        {
            return _vertexCount;
        }
    }
    public Line(int startVertexIndex, int length)
    {
        _startVertexIndex = startVertexIndex;
        _endVertexIndex = length * 6 - 1 + startVertexIndex;
        _midVertexIndex = EndVertexIndex / 2;
        //if (_midVertexIndex % 6 != 0) {
        //    int temp = _midVertexIndex % 6;
        //    _midVertexIndex = (_midVertexIndex - temp)+1;
        //}
        _vertexCount = length * 6;
    }
}
public class TextSpacing : BaseMeshEffect
{
    public float _textSpacing = 1f;
    public override void ModifyMesh(VertexHelper vh)
    {
        if (!IsActive() || vh.currentVertCount == 0)
        {
            return;
        }
        Text text = GetComponent<Text>();
        if (text == null)
        {
            Debug.LogError("Missing Text component");
            return;
        }
        HorizontalAligmentType alignment = HorizontalAligmentType.Left;
        switch (text.alignment)
        {
            case TextAnchor.UpperLeft:
            case TextAnchor.MiddleLeft:
            case TextAnchor.LowerLeft:
                alignment = HorizontalAligmentType.Left;
                break;
            case TextAnchor.UpperCenter:
            case TextAnchor.LowerCenter:
            case TextAnchor.MiddleCenter:
                alignment = HorizontalAligmentType.Center;
                break;
            case TextAnchor.UpperRight:
            case TextAnchor.MiddleRight:
            case TextAnchor.LowerRight:
                alignment = HorizontalAligmentType.Right;
                break;
            default:
                break;
        }
        List<UIVertex> vertexs = new List<UIVertex>();
        vh.GetUIVertexStream(vertexs);
    //    int indexCount = vh.currentIndexCount;
        string[] lineTexts = text.text.Split('\n');
        Line[] lines = new Line[lineTexts.Length];
        //根据lines数组中各个元素的长度计算每一行中第一个点的索引,每个字、字母、空母均占6个点
        for (int i = 0; i < lines.Length; i++)
        {
            //除最后一行外,vertexs对于前面几行都有回车符占了6个点
            if (i == 0)
            {
                lines[i] = new Line(0, lineTexts[i].Length + 1);
            }
            else if (i > 0 && i < lines.Length - 1)
            {
                lines[i] = new Line(lines[i - 1].EndVertexIndex + 1, lineTexts[i].Length + 1);
            }
            else
            {
                lines[i] = new Line(lines[i - 1].EndVertexIndex + 1, lineTexts[i].Length);
            }
        }
        UIVertex vt;
        for (int i = 0; i < lines.Length; i++)
        {
            for (int j = lines[i].StartVertexIndex ; j <= lines[i].EndVertexIndex; j++)
            {
                if (j < 0 || j >= vertexs.Count)
                {
                    continue;
                }
                vt = vertexs[j];
                int indexCount = lines[i].EndVertexIndex - lines[i].StartVertexIndex;
                switch (alignment)
                {
                    case HorizontalAligmentType.Left:
                        vt.position += new Vector3(_textSpacing * ((j - lines[i].StartVertexIndex) / 6), 0, 0);
                        break;
                    case HorizontalAligmentType.Center:
                        if (i == lines.Length - 1)
                        {
                            indexCount += 6;
                        }
                        var offset = (indexCount / 6) % 2 == 0 ? 0.5f : 0f;
                        vt.position += new Vector3(_textSpacing * ((j - lines[i].StartVertexIndex) / 6 - indexCount / 12 + offset), 0, 0);
                        break;
                    case HorizontalAligmentType.Right:
                       
                        if (lines.Length >=2&& i == lines.Length - 1)
                        {
                            indexCount += 6;
                        }
                        vt.position += new Vector3(_textSpacing * (-(indexCount - j + lines[i].StartVertexIndex) / 6 + 1), 0, 0);
                        break;
                    default:
                        break;
                }
                vertexs[j] = vt;
                // 以下注意点与索引的对应关系
                if (j % 6 <= 2)
                {
                    vh.SetUIVertex(vt, (j / 6) * 4 + j % 6);
                }
                if (j % 6 == 4)
                {
                    vh.SetUIVertex(vt, (j / 6) * 4 + j % 6 - 1);
                }
            }
        }
    }
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值