• UGUI之修改Text之间的间距


    using UnityEngine;
    using System.Collections;
    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, int countVertexIndex)
        {
            this.startVertexIndex = startVertexIndex;
            this.countVertexIndex = countVertexIndex;
            this.endVertexIndex = this.startVertexIndex + countVertexIndex - 1;
    
        }
    }
    
    /// <summary>
    /// 这是设置字体移动的核心类
    /// 执行多重行移动的核心算法是:将多重行分开依次进行处理,每一行的处理都是前面对单行处理的子操作
    /// 但是由vh是记录一个文本中所有的字的顶点,所以说需要分清楚每行开始,每行结束,以及行的字个数,
    /// 如此需要创建一个行的数据结构,以保存这些信息
    /// </summary>
    [AddComponentMenu("UI/Effects/TextSpacing")]
    public class TextSpacing : BaseMeshEffect
    {
        public float spacing = 0;
        public override void ModifyMesh(VertexHelper vh)
        {
            Text text = GetComponent<Text>();
            string[] ls = text.text.Split('
    ');
            int length = ls.Length;
            bool isNewLine = false;
            Line[] line;
            if (string.IsNullOrEmpty(ls[ls.Length - 1]) == true)
            {
                line = new Line[length - 1];
                isNewLine = true;
            }
            else
            {
                line = new Line[length];
    
            }
            //Debug.Log("ls长度" + ls.Length);
            for (int i = 0; i < line.Length; i++)
            {
                if (i == 0 && line.Length == 1 && isNewLine == false)//解决单行时没有换行符的情况
                {
                    line[i] = new Line(0, ls[i].Length * 6);
                    break;
                }
                if (i == 0 && line.Length >= 1)//解决单行时有换行符的情况,以及多行时i为0的情况
                {
                    line[i] = new Line(0, (ls[i].Length + 1) * 6);
                }
                else
                {
                    if (i < line.Length - 1)
                    {
                        line[i] = new Line(line[i - 1].EndVertexIndex + 1, (ls[i].Length + 1) * 6);
                    }
                    else
                    {
                        if (isNewLine == true)//解决多行时,最后一行末尾有换行符的情况
                        {
                            line[i] = new Line(line[i - 1].EndVertexIndex + 1, (ls[i].Length + 1) * 6);
                        }
                        else
                        {
                            line[i] = new Line(line[i - 1].EndVertexIndex + 1, ls[i].Length * 6);
                        }
                    }
                }
            }
    
    
            List<UIVertex> vertexs = new List<UIVertex>();
            vh.GetUIVertexStream(vertexs);
            int countVertexIndex = vertexs.Count;
            //Debug.Log("顶点总量" + vertexs.Count);
            for (int i = 0; i < line.Length; i++)
            {
                if (line[i].CountVertexIndex == 6) { continue; }
                for (int k = line[i].StartVertexIndex + 6; k <= line[i].EndVertexIndex; k++)
                {
                    UIVertex vertex = vertexs[k];
                    vertex.position += new Vector3(spacing * ((k - line[i].StartVertexIndex) / 6), 0, 0);
                    //Debug.Log("执行");
                    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);
                    }
                }
    
            }
    
    
        }
    }
    
  • 相关阅读:
    tensorflow 2.0 学习 (十) 拟合与过拟合问题
    tensorflow 2.0 学习 (九) tensorboard可视化功能认识
    tensorflow 2.0 学习 (八) keras模块的认识
    tensorflow 2.0 学习 (七) 反向传播代码逐步实现
    tensorflow 2.0 学习 (六) Himmelblua函数求极值
    tensorflow 2.0 学习 (五)MPG全连接网络训练与测试
    arp协议简单介绍
    Pthread spinlock自旋锁
    线程和进程状态
    内核态(内核空间)和用户态(用户空间)的区别和联系·
  • 原文地址:https://www.cnblogs.com/weiqiangwaideshijie/p/10593519.html
Copyright © 2020-2023  润新知