unity3dUGUI的Text实现字体间距的调整

将脚本TextSpacing 直接挂在到Text组件上,然后调整数值即可

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.Collections.Generic;

[AddComponentMenu( "UI/Effects/TextSpacing" )]
public class TextSpacing : BaseMeshEffect {
    public float _textSpacing = 1f;

    public override void ModifyMesh( VertexHelper vh ) {
        if ( !IsActive() || vh.currentVertCount == 0 ) {
            return;
        }
        List<UIVertex> vertexs = new List<UIVertex>();
        vh.GetUIVertexStream( vertexs );
        int indexCount = vh.currentIndexCount;
        UIVertex vt;
        for ( int i = 6; i < indexCount; i++ ) {
            //第一个字不用改变位置
            vt = vertexs[i];
            vt.position += new Vector3( _textSpacing * ( i / 6 ), 0, 0 );
            vertexs[i] = vt;
            //以下注意点与索引的对应关系
            if ( i % 6 <= 2 ) {
                vh.SetUIVertex( vt, ( i / 6 ) * 4 + i % 6 );
            }
            if ( i % 6 == 4 ) {
                vh.SetUIVertex( vt, ( i / 6 ) * 4 + i % 6 - 1 );
            }
        }
    }
}

第二版

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;

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;
    /// <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/TextSpacing")]
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);
                }
            }
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,919评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,810评论 19 139
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,853评论 0 17
  • 老实说决心写这个话题确实是因为最近的女作家自杀事件的影响,但其实这个话题在我脑海里已经盘旋许久了。据我所知大部分女...
    白小蝉阅读 5,443评论 4 7
  • 十月间隙 那天下了一整天的雨 上山途中沿路而行,有山,有水,有溪流,有小瀑布,还有路边的野梨,树上的板栗,还有一排...
    我不爱起名字阅读 6,837评论 0 0

友情链接更多精彩内容