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
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections.Generic;
- using System.Linq;
- [AddComponentMenu("UI/Effects/TextSpacing")]
- public class TextSpacing : BaseMeshEffect
- {
- [SerializeField]
- private float spacing_x;
- //[SerializeField]
- private float spacing_y=0;
- private const int VERTEXT_RANGE = 6;
- private List<UIVertex> mVertexList;
- public override void ModifyMesh(VertexHelper vh)
- {
- if (spacing_x == 0 && spacing_y == 0) { return; }
- if (!IsActive()) { return; }
- int count = vh.currentVertCount;
- if (count == 0) { return; }
- if (mVertexList == null) { mVertexList = new List<UIVertex>(); }
- vh.GetUIVertexStream(mVertexList);
- int row = 1;
- int column = 2;
- List<UIVertex> sub_vertexs = mVertexList.GetRange(0, VERTEXT_RANGE);
- float min_row_left = sub_vertexs.Min(v => v.position.x);
- int vertex_count = mVertexList.Count;
- for (int i = VERTEXT_RANGE; i < vertex_count;)
- {
- if (i % VERTEXT_RANGE == 0)
- {
- sub_vertexs = mVertexList.GetRange(i, VERTEXT_RANGE);
- float tem_row_left = sub_vertexs.Min(v => v.position.x);
- if (min_row_left - tem_row_left >= -10)
- {
- min_row_left = tem_row_left;
- ++row;
- column = 1;
- //continue;
- }
- }
- for (int j = 0; j < VERTEXT_RANGE; j++)
- {
- UIVertex vertex = mVertexList[i];
- vertex.position += Vector3.right * (column - 1) * spacing_x;
- vertex.position += Vector3.down * (row - 1) * spacing_y;
- mVertexList[i] = vertex;
- ++i;
- }
- ++column;
- }
- vh.Clear();
- vh.AddUIVertexTriangleStream(mVertexList);
- }
- }
复制代码 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 |