首页 > 其他分享 >unity3d:ugui 每个字间隔间距

unity3d:ugui 每个字间隔间距

时间:2022-11-01 11:06:55浏览次数:73  
标签:unity3d 间距 int lines Length Line new ugui public


using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
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);
}
}
}
}
}


标签:unity3d,间距,int,lines,Length,Line,new,ugui,public
From: https://blog.51cto.com/u_15544328/5812301

相关文章

  • Unity3D :Mob SMSSDK 运行崩溃
    报错信息android.content.ActivityNotFoundException:Unabletofindexplicitactivityclass{com.shuiying.smsm09061/com.mob.tools.MobUIShell};haveyoudeclaredt......
  • unity3d:显示FPS
    usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassFPSShow:MonoBehaviour{privatevoidOnGUI(){stringte......
  • unity3d:编辑器脚本,替换选中物体的材质
    usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEditor;publicclassChangeMat:MonoBehaviour{[MenuItem("Tools/Chang......
  • unity3d:xlua 加载自定义Loader
    在xLua加自定义loader是很简单的,只涉及到一个接口:publicdelegatebyte[]CustomLoader(refstringfilepath);publicvoidLuaEnv.AddLoader(CustomLoaderloader)通过A......
  • unity3d:xlua hotfix 官方例子
    1.新建工程,xlua文件夹与Plugins文件夹放入assets,tools放assets同级目录2.添加宏:HOTFIX_ENABLE3.D:\WorkSoft\unity2017.2.0\Editor\Data\Managed3个文件Unity.Cecil.dll,U......
  • UGUI
    ImageSimple常用简单模式Sliced九宫格模式Tiled瓦片模式Filled填充模式Sliced九宫格模式选中图片。点击SpriteEditor打开精灵体编辑器(版本高的需要先到Win......
  • Unity UGUI
    一个UI控件是由多个组件组成的,每一个组件都是一个类TEXT(文本控件)Font:字体可以更改显示的字体样式也可以导入外部字体样式FontStyle:字体风格Normal正常默认风格......
  • echarts图表y轴数据设置为固定值,等间距
    如图将Y轴设置为固定的0%20%----100%yAxis:{type:'value',max:100,//最大值mi......
  • unity3D mirror网络游戏开发笔记
    最近想开发一款多人在线网络游戏,使用unity3d的mirror插件可以提高开发效率,并且该插件免费。但是由于使用插件开发效率太低,经过研究,ummorpg开发模板是居于该插件开发而来,使......
  • Unity3D移动控制
    目录Unity中移动脚本1.通过Transform组件移动物体1.1Transform.Position1.2Transform.Translate1.3Vector3.Lerp,Vector3.Slerp,Vector3.MoveTowardsVector3(插值)......