?一、问题
在3D场景中常见文字,通过调整字体大小和锚点放大文字和清晰度,但是无法调整字间距,同时网上资料可以调整字间距,但是换行前面会出现空白。
?二、解决办法
通过重新ModifyMesh函数到达调整字间距,同时同调试发现需要给一定误差范围来解决换行前面出现空白问题。
?
?
?TextSpacing.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
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;
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, 6);
float min_row_left = sub_vertexs.Min(v => v.position.x);
int vertex_count = mVertexList.Count;
for (int i = 6; i < vertex_count;)
{
if (i % 6 == 0)
{
sub_vertexs = mVertexList.GetRange(i, 6);
float tem_row_left = sub_vertexs.Min(v => v.position.x);
Debug.LogError("min_row_left=" + min_row_left.ToString() + " tem_row_left=" + tem_row_left.ToString() + " tem_row_left <=min_row_left= " + (tem_row_left <= min_row_left).ToString());
if (min_row_left-tem_row_left >=-10 )
{
min_row_left = tem_row_left;
++row;
column = 1;
//continue;
}
}
for (int j = 0; j < 6; 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);
}
}
|