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

CMS系统
0 1013
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无广告纯净版 2679
Android QQ音乐 v11.3.7 定制版解锁免费听歌无广告版 3184
Android 草图大师SKP v1.5 解锁会员版 8576
Android OmoFun追漫神器 v4.2.1 无广告绿色版 5580
AdobeGenP Adobe全家桶系列激活工具 v3.5.0/3.4.14.1 绿色版 6168
Android 七猫免费听书 v1.4 解锁高级版 4627
超良心的抢票神器——Bypass1.16 4274
Android 玲珑加速器 v6.10.9.40 解锁无限时间 2981
Android 大师兄影视 v3.3.8 去广告会员版 2101
热门帖子
OpenClaw-小龙虾 超级体实操教程:多系统安装+云部署+技能创造,从0到1打造你的AI超级体 108
靠 AI 自动写文章,单日利润最高800+,长期打法,新手也能上手,电脑搬砖副业 59
零成本做拼多多虚拟,不用手动发货,不用聊客服,轻松月入五位数! 54
OpenClaw实战营:本地+云端部署+Skill开发+多平台自动化,7 天打造 AI 员工团队,实现内容量产 50
今日头条2026最新蓝海玩法,AI无脑抄书,批量生成,绝对的信息差,简单粗暴,小白轻松上手 44
AI卖货最新教学,每天十几分钟,单账号轻松日入500+ 43
OpenClaw 全网首发手册:5万字+持续更新,从部署到玩法,一站式掌握 AI 工具实操全流程 41
快手荧光短剧,全自动代发,免费项目单号月200-600收益 40
爆火全网【OpenClaw 小龙虾】一键部署教程:从0到1搭建自托管AI助理,多系统部署+多通道对接 40