1. 程式人生 > 其它 >Unity 弧形文字元件

Unity 弧形文字元件

技術標籤:unity3dc#

弧形Text元件


自己寫的一個Unity3D製作弧形文字的元件,喜自取,同時希望有大佬可以優化
下面展示一些 內聯程式碼片

// A code block
var foo = 'bar';
// An highlighted block
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DotModel
{
    public UIVertex lb = new UIVertex();
    public
UIVertex lt = new UIVertex(); public UIVertex rt = new UIVertex(); public UIVertex rb = new UIVertex(); } [AddComponentMenu("UI/Effects/Extensions/CurvedText")] public class CurvedText : BaseMeshEffect { public float radius = 600; [Range(0, 360)] [SerializeField] public Double singleAngle =
5; private float lenghOfWord = 0; public override void ModifyMesh(VertexHelper vh) { if (!IsActive()) { return; } DotModel dotModel = new DotModel(); int dotNum = vh.currentVertCount / 4; float midNum = (dotNum - 1) / 2; for
(int i = 0; i < dotNum; i++) { PopulateUIVertex(vh, i * 4, ref dotModel);//初始化了點集對應的點 UIVertex lb = new UIVertex(); //lb 左下 lt左上 rt 右上 ,rb右下 vh.PopulateUIVertex(ref lb, i * 4); UIVertex lt = new UIVertex(); vh.PopulateUIVertex(ref lt, i * 4 + 1); UIVertex rt = new UIVertex(); vh.PopulateUIVertex(ref rt, i * 4 + 2); UIVertex rb = new UIVertex(); vh.PopulateUIVertex(ref rb, i * 4 + 3); //計算平移量 Double angle = (midNum - i) * singleAngle * Math.PI / 180.0; Double distance = Math.Sin(angle / 2) * radius * 2; float x = -Convert.ToInt16(Math.Cos(angle/2) * distance) + (midNum - i)* lenghOfWord; float y = -Math.Abs(Convert.ToInt16(Math.Sin(angle / 2) * distance)); //對點進行平移 lb.position.x += x; lt.position.x += x; rt.position.x += x; rb.position.x += x; lb.position.y += y; lt.position.y += y; rt.position.y += y; rb.position.y += y; //計算旋轉量 Vector3 center = Vector3.Lerp(lb.position, rt.position, 0.5f); //文字的中心點 float width = Vector3.Distance(lb.position, center); //對角線長度一半 Double distance2 = Math.Abs(Math.Sin(angle / 2) * width * 2); float s = Convert.ToSingle(Math.Abs(Math.Cos(angle / 2) * distance2)); float b = Convert.ToSingle(Math.Abs(Math.Sin(angle / 2) * distance2)); //Debug.Log("width:"+ width + " distance2:"+ distance2 + " s:" + s + " b:" + b); if ((i - midNum) < 0 ){ lb.position.x += b; lt.position.x -= s; rt.position.x -= b; rb.position.x += s; lb.position.y -= s; lt.position.y -= b; rt.position.y += s; rb.position.y += b; } else { lb.position.x += s; lt.position.x -= b; rt.position.x -= s; rb.position.x += b; lb.position.y -= b; lt.position.y -= s; rt.position.y += b; rb.position.y += s; } //重新整理4個頂點 vh.SetUIVertex(lb, i * 4); vh.SetUIVertex(lt, i * 4 + 1); vh.SetUIVertex(rt, i * 4 + 2); vh.SetUIVertex(rb, i * 4 + 3); } //Debug.Log("lenghOfWord:" + lenghOfWord); } void PopulateUIVertex(VertexHelper vh, int index, ref DotModel dotModel) { vh.PopulateUIVertex(ref dotModel.lb, index); vh.PopulateUIVertex(ref dotModel.lt, index + 1); vh.PopulateUIVertex(ref dotModel.rt, index + 2); vh.PopulateUIVertex(ref dotModel.rb, index + 3); lenghOfWord = Math.Abs(dotModel.rb.position.x - dotModel.rt.position.x); } }

效果圖
在這裡插入圖片描述