unity向内弯曲文本Text

13 篇文章 0 订阅

#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);
    }
}

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值