本文主要是介绍UGUI Text 重写Spacing字体间距,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
using UnityEngine; using UnityEngine.UI;[AddComponentMenu("UI/Effect/UGUITextSpacing")] [RequireComponent(typeof(UnityEngine.UI.Text))]//Text组件是必须的 public class UGUITextSpacing : BaseMeshEffect {public enum HorizontalAligmentType{Left,Center,Right}public float fSpacing = 1.0f;public override void ModifyMesh(VertexHelper vh){//一个文字有4个顶点 4个顶点组成两个三角面 如果顶点少于4个 说明text内容为空 就没必要去做字间距调整了if (!IsActive() || vh.currentVertCount < 4 || fSpacing == 0){return;}Text cText = transform.GetComponent<Text>();// 对齐方式,对于不同的对齐方式 对文字顶点的坐标偏移值计算方式不同HorizontalAligmentType alignment;if (cText.alignment == TextAnchor.LowerLeft || cText.alignment == TextAnchor.MiddleLeft || cText.alignment == TextAnchor.UpperLeft){alignment = HorizontalAligmentType.Left;}else if (cText.alignment == TextAnchor.LowerCenter || cText.alignment == TextAnchor.MiddleCenter || cText.alignment == TextAnchor.UpperCenter){alignment = HorizontalAligmentType.Center;}else{alignment = HorizontalAligmentType.Right;}int nWordCount = vh.currentVertCount / 4;//总字数//总的字间距偏移值float fTotalSpace = (nWordCount - 1) * fSpacing;float fOffsetX = 0.0f;UIVertex vertex = new UIVertex();for (int index = 0; index < nWordCount; index++){if (alignment == HorizontalAligmentType.Left){//左对齐的话 相当于对于每个字 坐标往右偏移对应字间距值 第一个字不用偏移 fOffsetX = index * fSpacing;}else if (alignment == HorizontalAligmentType.Right){//和左对齐类似fOffsetX = index * fSpacing - fTotalSpace;}else if (alignment == HorizontalAligmentType.Center){//居中对齐 类似左对齐之后 整体往左偏移总字间距的50% 那么在计算的时候直接减50%总字间距就好了fOffsetX = index * fSpacing - fTotalSpace / 2;}for (int pIndex = 0; pIndex < 4; pIndex++){int nVerticeIndex = index * 4 + pIndex;//对应索引vh.PopulateUIVertex(ref vertex, nVerticeIndex);//取出对应对应索引的顶点vertex.position += new Vector3(fOffsetX, 0, 0);//顶点偏移vh.SetUIVertex(vertex, nVerticeIndex);//更新下顶点坐标信息}}} }
这篇关于UGUI Text 重写Spacing字体间距的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!