| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 | using UnityEngine;using System.Collections;using UnityEngine.UI;using System;using System.Collections.Generic;public struct Line{    private int _startVertexIndex ;    /// <summary>    /// 起点索引    /// </summary>    public int StartVertexIndex    {        get { return _startVertexIndex; }    }    private int _endVertexIndex;    /// <summary>    /// 终点索引    /// </summary>    public int EndVertexIndex    {        get { return _endVertexIndex; }    }    private int _vertexCount;    /// <summary>    /// 该行占的点数目    /// </summary>    public int VertexCount    {        get { return _vertexCount; }    }    public Line(int startVertexIndex, int length)    {        _startVertexIndex = startVertexIndex;        _endVertexIndex = length * 6 - 1 + startVertexIndex;        _vertexCount = length * 6;    }}[AddComponentMenu("UI/Effects/UITextSpacing")]public class UITextSpacing : BaseMeshEffect{    public float _textSpacing = 1f;    private UIVertex vt;    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;        }        // 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);            }        }               for (int i = 4; i < vh.currentVertCount; i++)        {            vh.PopulateUIVertex(ref vt, i);            vt.position += new Vector3(_textSpacing * (i / 4 + 1), 0, 0);            vh.SetUIVertex(vt, i);        }        // for (int i = 0; i < lines.Length; i++)        // {        //     for (int j = lines[i].StartVertexIndex + 6; j <= lines[i].EndVertexIndex; j++)        //     {        //         if (j < 0 || j >= vh.currentVertCount)        //         {        //             continue;        //         }        //        //         int index = 0;        //         if (j % 6 <= 2)        //         {        //             index = (j / 6) * 4 + j % 6;        //                   //         }        //         if (j % 6 == 4)        //         {        //             index = (j / 6) * 4 + j % 6 - 1;        //                //         }        //         vh.PopulateUIVertex(ref vt,index);        //         vt.position += new Vector3(_textSpacing * ((j - lines[i].StartVertexIndex) / 6), 0, 0);        //               //         //以下注意点与索引的对应关系        //         vh.SetUIVertex(vt, index);        //     }        // }    }}
 |