[Unity学习教程] Unity 工具控件 之 Text 文本字间距调解(老版本的Unity编写工具控件/新版本

[复制链接]
查看1147 | 回复0 | 2023-8-16 16:30:17 | 显示全部楼层 |阅读模式 来自 中国北京
Unity 工具控件 之 Text 文本字间距调解(老版本的Unity编写工具控件/新版本Unity使用TMP)


目次
Unity 工具控件 之 Text 文本字间距调解(老版本的Unity编写工具控件/新版本Unity使用TMP)
一、简单先容
二、老版本 Unity Text 使用工具控件调解行间距
三、新版本 Unity Text(TMP)自带调解行间距
附录:
新版本 Unity Text(TMP) 中笔墨体支持(先容天生中笔墨体资源的方法之一)


一、简单先容

Unity 工具控件类,本身整理的一些游戏开辟大概用到的模块,单独独立使用,方便游戏开辟。
本节先容,在原始的 Unity Text 中,只能调解文本的行间,字间距确实默认的值,要调解的本身额外的编写工具空间来调解;假如使用新版的 Unity Text(TMP)组件,内里就有现成的设置了,这里简单说明,假如你有更好的方法,接待留言互换。
(大概两种方法都存在使用范围性,大概根据须要使用)
 
二、老版本 Unity Text 使用工具控件调解行间距

1、正常的字间距结果

 
 2、可调解的字间距结果

 3、参考的代码 TextSpacing
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. [AddComponentMenu("UI/Effects/TextSpacing")]
  6. public class TextSpacing : BaseMeshEffect
  7. {
  8.     [SerializeField]
  9.     private float spacing_x;
  10.     //[SerializeField]
  11.     private float spacing_y=0;
  12.     private const int VERTEXT_RANGE = 6;
  13.     private List<UIVertex> mVertexList;
  14.     public override void ModifyMesh(VertexHelper vh)
  15.     {
  16.         if (spacing_x == 0 && spacing_y == 0) { return; }
  17.         if (!IsActive()) { return; }
  18.         int count = vh.currentVertCount;
  19.         if (count == 0) { return; }
  20.         if (mVertexList == null) { mVertexList = new List<UIVertex>(); }
  21.         vh.GetUIVertexStream(mVertexList);
  22.         int row = 1;
  23.         int column = 2;
  24.         List<UIVertex> sub_vertexs = mVertexList.GetRange(0, VERTEXT_RANGE);
  25.         float min_row_left = sub_vertexs.Min(v => v.position.x);
  26.         int vertex_count = mVertexList.Count;
  27.         for (int i = VERTEXT_RANGE; i < vertex_count;)
  28.         {
  29.             if (i % VERTEXT_RANGE == 0)
  30.             {
  31.                 sub_vertexs = mVertexList.GetRange(i, VERTEXT_RANGE);
  32.                 float tem_row_left = sub_vertexs.Min(v => v.position.x);
  33.                 if (min_row_left - tem_row_left >= -10)
  34.                 {
  35.                     min_row_left = tem_row_left;
  36.                     ++row;
  37.                     column = 1;
  38.                     //continue;
  39.                 }
  40.             }
  41.             for (int j = 0; j < VERTEXT_RANGE; j++)
  42.             {
  43.                 UIVertex vertex = mVertexList[i];
  44.                 vertex.position += Vector3.right * (column - 1) * spacing_x;
  45.                 vertex.position += Vector3.down * (row - 1) * spacing_y;
  46.                 mVertexList[i] = vertex;
  47.                 ++i;
  48.             }
  49.             ++column;
  50.         }
  51.         vh.Clear();
  52.         vh.AddUIVertexTriangleStream(mVertexList);
  53.     }
  54. }
复制代码
4、参考的代码 TextSpacing1
[code]using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;[AddComponentMenu("UI/Effects/TextSpacing1")]public class TextSpacing1 : BaseMeshEffect{    public float TextHorizontalSpacing = 1f;    public override void ModifyMesh(VertexHelper vh)    {        if (!IsActive() || vh.currentVertCount == 0)        {            return;        }        Text text = GetComponent();        if (text == null)        {            Debug.LogError("Missing Text component");            return;        }        List vertexs = new List();        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 = new Line(0, lineTexts.Length + 1);            }            else if (i > 0 && i < lines.Length - 1)            {                lines = new Line(lines[i - 1].EndVertexIndex + 1, lineTexts.Length + 1);            }            else            {                lines = new Line(lines[i - 1].EndVertexIndex + 1, lineTexts.Length);            }        }        UIVertex vt;        for (int i = 0; i < lines.Length; i++)        {            Vector3 startPos = Vector3.zero;            Vector3 endPos = Vector3.zero;            Vector3 defaultStartPos = Vector3.zero;            Vector3 defaultEndPos = Vector3.zero;            for (int j = lines.StartVertexIndex; j = vertexs.Count)                {                    continue;                }                vt = vertexs[j];                if (defaultStartPos == Vector3.zero)                {                    defaultStartPos = new Vector3(vt.position.x, vt.position.y, vt.position.z);                }                defaultEndPos = new Vector3(vt.position.x, vt.position.y, vt.position.z);                if (j != 0)                {                    vt.position += new Vector3(TextHorizontalSpacing * ((j - lines.StartVertexIndex) / 6), 0, 0);                }                if (startPos == Vector3.zero)                {                    startPos = new Vector3(vt.position.x, vt.position.y, vt.position.z);                }                endPos = new Vector3(vt.position.x, vt.position.y, vt.position.z);                vertexs[j] = vt;                //以下注意点与索引的对应关系                if (j % 6

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则