#unity向内弯曲文本
此脚本挂载在Text上
效果如图
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TextDotModel
{
public UIVertex lb = new UIVertex();
public UIVertex lt = new UIVertex();
public UIVertex rt = new UIVertex();
public UIVertex rb = new UIVertex();
}
public class CircleText : BaseMeshEffect
{
[SerializeField]
private float numPosx = 20;
[SerializeField]
private float numPosz = 10;
[SerializeField]
private float numRosY = 10;
[Range(-1, 1)]
[SerializeField]
private float sinA;
public override void ModifyMesh(VertexHelper vh)
{
if (!IsActive())
{
return;
}
TextDotModel textDotModel = new TextDotModel();
for (int i = vh.currentVertCount / 8 + 1; i < vh.currentVertCount / 4; i++)
{
PopulateUIVertex(vh, i * 4,ref textDotModel);
Vector3 pos = new Vector3((i - vh.currentVertCount / 8) * numPosx, 0, (i - vh.currentVertCount / 8) * numPosz);
Matrix4x4 place = Matrix4x4.TRS(pos, Quaternion.identity, Vector3.one);
SetPos(place, textDotModel);
textDotModel.lt.position.z += numRosY;
textDotModel.rt.position.z += numRosY;
//Debug.Log(lb.position + " " + lt.position + " " + rt.position + " " + rb.position);
SetUIVertex(vh, i * 4, textDotModel);
}
for (int i = vh.currentVertCount / 8 - 1; i >= 0; i--)
{
PopulateUIVertex(vh, i * 4,ref textDotModel);
Vector3 pos = new Vector3(-(vh.currentVertCount / 8 - i) * numPosx, 0, (vh.currentVertCount / 8 - i) * numPosz);
Matrix4x4 place = Matrix4x4.TRS(pos, Quaternion.identity, Vector3.one);
SetPos(place, textDotModel);
textDotModel.lb.position.z += numRosY;
textDotModel.rb.position.z += numRosY;
SetUIVertex(vh, i * 4, textDotModel);
}
}
void PopulateUIVertex(VertexHelper vh, int index,ref TextDotModel textDotModel)
{
vh.PopulateUIVertex(ref textDotModel.lb, index);
vh.PopulateUIVertex(ref textDotModel.lt, index + 1);
vh.PopulateUIVertex(ref textDotModel.rt, index + 2);
vh.PopulateUIVertex(ref textDotModel.rb, index + 3);
}
void SetPos(Matrix4x4 transform, TextDotModel textDotModel)
{
textDotModel.lb.position = transform.MultiplyPoint(textDotModel.lb.position);
textDotModel.lt.position = transform.MultiplyPoint(textDotModel.lt.position);
textDotModel.rt.position = transform.MultiplyPoint(textDotModel.rt.position);
textDotModel.rb.position = transform.MultiplyPoint(textDotModel.rb.position);
textDotModel.lb.position.z += numPosz * Mathf.Sin(sinA);
textDotModel.rb.position.z += numPosz * Mathf.Sin(sinA);
textDotModel.lt.position.z += numPosz * Mathf.Sin(sinA);
textDotModel.rt.position.z += numPosz * Mathf.Sin(sinA);
}
void SetUIVertex(VertexHelper vh, int index, TextDotModel textDotModel)
{
vh.SetUIVertex(textDotModel.lb, index);
vh.SetUIVertex(textDotModel.lt, index + 1);
vh.SetUIVertex(textDotModel.rt, index + 2);
vh.SetUIVertex(textDotModel.rb, index + 3);
}
}