using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.Collections.Generic;
[AddComponentMenu("UI/Effects/TextSpacing")]
public class TextSpacing : BaseMeshEffect
{
public float _textSpacing = 1f;
int fontSize = 6;
float dis = 0f;
int count = 0;
int lineIndex = 0;
Text text;
enum Alignment
{
Center,
Left,
Right
};
Alignment textAlignment;
Dictionary> dic = new Dictionary>();
public override void ModifyMesh(VertexHelper vh)
{
if (!this.IsActive())
return;
if (text == null)
{
text = this.GetComponent();
}
switch (text.alignment)
{
case TextAnchor.LowerLeft:
case TextAnchor.MiddleLeft:
case TextAnchor.UpperLeft:
textAlignment = Alignment.Left;
break;
case TextAnchor.LowerCenter:
case TextAnchor.MiddleCenter:
case TextAnchor.UpperCenter:
textAlignment = Alignment.Center;
break;
case TextAnchor.LowerRight:
case TextAnchor.MiddleRight:
case TextAnchor.UpperRight:
textAlignment = Alignment.Right;
break;
}
count = 0;
lineIndex = 0;
dic.Clear();
List vertexList = new List();
vh.GetUIVertexStream(vertexList);
ModifyVertices(vertexList);
vh.Clear();
vh.AddUIVertexTriangleStream(vertexList);
}
public void ModifyVertices(List vertexList)
{
count = vertexList.Count;
if (!IsActive() || count <= 0)
{
return;
}
GenerateDic(vertexList);
Modify(vertexList);
}
///
/// 将顶点按行分类
///
///
void GenerateDic(List vertexList)
{
float topY = 0f, bottomY = 0f, curTopY = 0f, curBottomY = 0f;
for (int i = 0; i < count;)
{
List temp = new List();
for (int k = 0; k < fontSize; k++)
{
temp.Add(vertexList[k + i]);
}
if (CheckIfCanInsert(temp))
{
FindMostValue(temp, ref curTopY, ref curBottomY);
if (curTopY < bottomY)
{
lineIndex += 1;
topY = curTopY;
bottomY = curBottomY;
}
if (topY == 0f && bottomY == 0f)
{
topY = curTopY;
bottomY = curBottomY;
}
List t;
if (!dic.TryGetValue(lineIndex, out t))
{
dic.Add(lineIndex, new List());
dic.TryGetValue(lineIndex, out t);
}
for (int k = 0; k < fontSize; k++)
{
t.Add(k + i);
}
}
i += fontSize;
}
}
///
/// 修改顶点
///
///
void Modify(List vertexList)
{
foreach (var t in dic)
{
List value = t.Value;
count = value.Count;
dis = 0f;
for (int i = 0; i < count; i++)
{
int index = value[i];
UIVertex uivertex = vertexList[index];
Vector3 pos = uivertex.position;
if (textAlignment == Alignment.Right)
{
pos.x -= _textSpacing * ((count - 1 - i) / fontSize);
}
else
{
if (textAlignment == Alignment.Center)
{
dis = _textSpacing * (count / fontSize - 1) * 0.5f;
}
pos.x += _textSpacing * (i / fontSize) - dis;
}
uivertex.position = pos;
vertexList[index] = uivertex;
}
}
}
///
/// 判断当前坐标点是否有效(在text渲染的时候\n的渲染有点不一样)
///
///
///
bool CheckIfCanInsert(List vertexList)
{
bool isCan = true;
Vector3 pos = vertexList[0].position;
int num = 0;
for (int i = 1; i < vertexList.Count; i++)
{
Vector3 p = vertexList[i].position;
if (p.x == pos.x && p.y == pos.y)
{
num++;
}
}
isCan = (num != (vertexList.Count-1));
return isCan;
}
///
/// 查找最值
///
///
///
///
///
void FindMostValue(List vertexList,ref float topY,ref float bottomY, int startIndex=0)
{
List t = new List();
topY = vertexList[startIndex].position.y;
bottomY = topY;
for (int i = startIndex; i < vertexList.Count; i++)
{
float y = vertexList[i].position.y;
if (y > topY)
{
topY = y;
}
else if (y < bottomY)
{
bottomY = y;
}
}
}
}