短视频商城源码Unity改变text字间距

CMS系统
0 834
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.Collections.Generic;
namespace FrameWork
{
    /// <summary>
    /// 字体间距
    /// </summary>
    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;
            }
 
            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 + 6; j <= lines[i].EndVertexIndex; j++)
                {
                    if (j < 0 || j >= vertexs.Count)
                    {
                        continue;
                    }
                    vt = vertexs[j];
                    vt.position += new Vector3(_textSpacing * ((j - lines[i].StartVertexIndex) / 6), 0, 0);
                    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);
                    }
                }
            }
        }
    }
}
public class Line
{
 
    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;
 
 
    public Line(int startVertexIndex, int length)
    {
        _startVertexIndex = startVertexIndex;
        _endVertexIndex = length * 6 - 1 + startVertexIndex;
        _vertexCount = length * 6;
    }
}


精选帖子
Android 可可影视 v3.3.6无广告纯净版 1916
Android QQ音乐 v11.3.7 定制版解锁免费听歌无广告版 2370
Android 草图大师SKP v1.5 解锁会员版 7806
Android OmoFun追漫神器 v4.2.1 无广告绿色版 4808
AdobeGenP Adobe全家桶系列激活工具 v3.5.0/3.4.14.1 绿色版 5387
Android 七猫免费听书 v1.4 解锁高级版 3829
超良心的抢票神器——Bypass1.16 3421
Android 玲珑加速器 v6.10.9.40 解锁无限时间 2230
Android 大师兄影视 v3.3.8 去广告会员版 1335
热门帖子
三天AI智能体创业营,全程没虚的,全是拿来就能用的招,老板们想省劲搞增长,这课直接对标结果 48
AI电影制作大师班:ChatGPT编剧、Suno配乐、Veo生成,Nano Banana教程,中英字幕|1080P 46
全网独家淘宝闪购采集挂机项目 单窗口轻松日入40+ 45
WiFi万能钥匙破解版 去广告最新版 一键免费“蹭网”WIFI热点APP软件 44
2025年AI智能体(Agent)与自动化工作流开发全栈实战课,掌握从搭建到部署落地完整开发能力 43
1Gallery 一款专注于加密隐藏私人照片视频第三方安卓相册应用高级付费版 42
动漫卡通头像生成器安卓版 inMelo Ai图片生成器高级破解版 40
OrionTV一个全新的TV电视播放器 支持Apple 电视TV 和 Android TV盒子 36
百度智能体网盘拉新项目,轻松躺赚收益(手把手教程) 36