using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.UI;
using System.Collections.Generic;
[ExecuteInEditMode]
public class VirticalText : BaseMeshEffect
{
public class VirticalText : BaseMeshEffect
{
[Tooltip("字和字之间的距离")]
public float spacing = 1;
[Tooltip("作者所在列")]
public int WriteLine;
[Tooltip("作者所在列字和字之间的距离")]
public float writeSpacing;
[Tooltip("作者所在列的列距")]
public float WriteLinespacing=1;
private int ConcGram;
private float WriteTextSpacing;
private float WritetextLinespacing;
private float lineSpacing = 1;
private float textSpacing = 1;
private float xOffset = 0;
private float yOffset = 0;
public override void ModifyMesh(VertexHelper helper)
{
if (!IsActive())
return;
public float spacing = 1;
[Tooltip("作者所在列")]
public int WriteLine;
[Tooltip("作者所在列字和字之间的距离")]
public float writeSpacing;
[Tooltip("作者所在列的列距")]
public float WriteLinespacing=1;
private int ConcGram;
private float WriteTextSpacing;
private float WritetextLinespacing;
private float lineSpacing = 1;
private float textSpacing = 1;
private float xOffset = 0;
private float yOffset = 0;
public override void ModifyMesh(VertexHelper helper)
{
if (!IsActive())
return;
List<UIVertex> verts = new List<UIVertex>();
helper.GetUIVertexStream(verts);
Text text = GetComponent<Text>();
TextGenerator tg = text.cachedTextGenerator;
lineSpacing = text.fontSize * text.lineSpacing;
textSpacing = text.fontSize * spacing;
WriteTextSpacing = text.fontSize * writeSpacing; // 改变作者所在列行距;
WritetextLinespacing= text.fontSize * WriteLinespacing; //改变作者所在列列距;
xOffset = text.rectTransform.sizeDelta.x / 2 - text.fontSize / 2;
yOffset = text.rectTransform.sizeDelta.y / 2 - text.fontSize / 2;
string content = text.text;
List<UILineInfo> lines = new List<UILineInfo>();
tg.GetLines(lines);
bool needSkip = false;
int sumCount = 0;
for (int i = 0; i < lines.Count; i++)
{
UILineInfo line = lines[i];
helper.GetUIVertexStream(verts);
Text text = GetComponent<Text>();
TextGenerator tg = text.cachedTextGenerator;
lineSpacing = text.fontSize * text.lineSpacing;
textSpacing = text.fontSize * spacing;
WriteTextSpacing = text.fontSize * writeSpacing; // 改变作者所在列行距;
WritetextLinespacing= text.fontSize * WriteLinespacing; //改变作者所在列列距;
xOffset = text.rectTransform.sizeDelta.x / 2 - text.fontSize / 2;
yOffset = text.rectTransform.sizeDelta.y / 2 - text.fontSize / 2;
string content = text.text;
List<UILineInfo> lines = new List<UILineInfo>();
tg.GetLines(lines);
bool needSkip = false;
int sumCount = 0;
for (int i = 0; i < lines.Count; i++)
{
UILineInfo line = lines[i];
int step = i;
ConcGram = i + 1;
if (i + 1 < lines.Count)
{
UILineInfo line2 = lines[i + 1];
ConcGram = i + 1;
if (i + 1 < lines.Count)
{
UILineInfo line2 = lines[i + 1];
int current = 0;
int realCurrent = 0;
needSkip = false;
for (int j = line.startCharIdx; j < line2.startCharIdx - 1; j++)
{
if (content[sumCount+current] == '<')
{
needSkip = true;
}else if (content[sumCount+current] == '>')
{
needSkip = false;
}else
{
if(!needSkip)
{
modifyText(helper, j, realCurrent, step);
realCurrent++;
}
}
current++;
}
sumCount += (current-1+2);//回车键是两个字符,所以要再加2;
}
else if (i + 1 == lines.Count)
{
int current = 0;
int realCurrent = 0;
needSkip = false;
for (int j = line.startCharIdx; j < tg.characterCountVisible; j++)
{
if (content[sumCount+current] == '<')
{
needSkip = true;
}
else if (content[sumCount+current] == '>')
{
needSkip = false;
}
else
{
if (!needSkip)
{
modifyText(helper, j, realCurrent, step);
realCurrent++;
}
}
current++;
}
}
}
}
int realCurrent = 0;
needSkip = false;
for (int j = line.startCharIdx; j < line2.startCharIdx - 1; j++)
{
if (content[sumCount+current] == '<')
{
needSkip = true;
}else if (content[sumCount+current] == '>')
{
needSkip = false;
}else
{
if(!needSkip)
{
modifyText(helper, j, realCurrent, step);
realCurrent++;
}
}
current++;
}
sumCount += (current-1+2);//回车键是两个字符,所以要再加2;
}
else if (i + 1 == lines.Count)
{
int current = 0;
int realCurrent = 0;
needSkip = false;
for (int j = line.startCharIdx; j < tg.characterCountVisible; j++)
{
if (content[sumCount+current] == '<')
{
needSkip = true;
}
else if (content[sumCount+current] == '>')
{
needSkip = false;
}
else
{
if (!needSkip)
{
modifyText(helper, j, realCurrent, step);
realCurrent++;
}
}
current++;
}
}
}
}
void modifyText(VertexHelper helper, int i, int charYPos, int charXPos)
{
UIVertex lb = new UIVertex();
helper.PopulateUIVertex(ref lb, i * 4);
{
UIVertex lb = new UIVertex();
helper.PopulateUIVertex(ref lb, i * 4);
UIVertex lt = new UIVertex();
helper.PopulateUIVertex(ref lt, i * 4 + 1);
helper.PopulateUIVertex(ref lt, i * 4 + 1);
UIVertex rt = new UIVertex();
helper.PopulateUIVertex(ref rt, i * 4 + 2);
helper.PopulateUIVertex(ref rt, i * 4 + 2);
UIVertex rb = new UIVertex();
helper.PopulateUIVertex(ref rb, i * 4 + 3);
helper.PopulateUIVertex(ref rb, i * 4 + 3);
Vector3 center = Vector3.Lerp(lb.position, rt.position, 0.5f);
Matrix4x4 move = Matrix4x4.TRS(-center, Quaternion.identity, Vector3.one);
Matrix4x4 move = Matrix4x4.TRS(-center, Quaternion.identity, Vector3.one);
float x;
float y;
if (ConcGram>= WriteLine) //作者所在列
{
y = -charYPos * WriteTextSpacing + yOffset;
x = -charXPos * WritetextLinespacing + xOffset;
}
else
{
y = -charYPos * textSpacing + yOffset;
x = -charXPos * lineSpacing + xOffset;
}
Vector3 pos = new Vector3(x, y, 0);
Matrix4x4 place = Matrix4x4.TRS(pos, Quaternion.identity, Vector3.one);
Matrix4x4 transform = place * move;
float y;
if (ConcGram>= WriteLine) //作者所在列
{
y = -charYPos * WriteTextSpacing + yOffset;
x = -charXPos * WritetextLinespacing + xOffset;
}
else
{
y = -charYPos * textSpacing + yOffset;
x = -charXPos * lineSpacing + xOffset;
}
Vector3 pos = new Vector3(x, y, 0);
Matrix4x4 place = Matrix4x4.TRS(pos, Quaternion.identity, Vector3.one);
Matrix4x4 transform = place * move;
lb.position = transform.MultiplyPoint(lb.position);
lt.position = transform.MultiplyPoint(lt.position);
rt.position = transform.MultiplyPoint(rt.position);
rb.position = transform.MultiplyPoint(rb.position);
lt.position = transform.MultiplyPoint(lt.position);
rt.position = transform.MultiplyPoint(rt.position);
rb.position = transform.MultiplyPoint(rb.position);
helper.SetUIVertex(lb, i * 4);
helper.SetUIVertex(lt, i * 4 + 1);
helper.SetUIVertex(rt, i * 4 + 2);
helper.SetUIVertex(rb, i * 4 + 3);
}
}
helper.SetUIVertex(lt, i * 4 + 1);
helper.SetUIVertex(rt, i * 4 + 2);
helper.SetUIVertex(rb, i * 4 + 3);
}
}