绘制轮转图

1,先生成环状预制体,然后用Mathf.Sin和Mathf.Cos对预制体Cube的LocalPosition进行赋值,在另一个脚本里求出鼠标拖动的差值传到第一个脚本里,在第一个脚本遍历预制体集合同样对Mathf.Sin和Mathf.Cos预制体Cube的LocalPosition进行赋值。

2,创建一个 Transform集合,对集合中的position,z进行排序.,计算出排序后对齐第一个的Asin值和对应对齐的时间,Dotween.To进行缓慢对齐

3,创建一个脚本,对VertexHelper的AddVert和AddTriangle方法进行赋值,然后对mesh赋值,

                                                 

代码如下:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CreateCubes1 : MonoBehaviour
{
    public float c;
    public float r;
    public int n;
    float ang;
    public float slow = 500f;
    List<GameObject> list = new List<GameObject>();
    List<Transform> sort = new List<Transform>();
    // Start is called before the first frame update
    void Start()
    {
        ang = 2 * Mathf.PI / n;
        Move();
       
    }
    float allang = 0;
    public void OnMoves(float des)
    {
        float moveing = des / r;
        allang -= moveing;
        Move();
    }
    public void Move()
    {
        for (int i = 0; i < n; i++)
        {
           
            float x = Mathf.Sin(i * ang + allang) * r;
            float y = Mathf.Cos(i * ang + allang) * r;
            if (list.Count<=i)
            {
                GameObject go = Instantiate(Resources.Load<GameObject>("Cube"), transform);
                go.transform.parent = transform;

                go.GetComponent<Cubes1>().create = this;
                list.Add(go);
                sort.Add(go.transform);
            }
           
            list[i].transform.localPosition = new Vector3(x, 0, y);
            list[i].transform.localEulerAngles = Vector3.up * (i * ang + allang)*Mathf.Rad2Deg;
        }
    }
    public void Force(float des)
    {
        float time = Mathf.Abs(des / slow) ;
        DT.To((a) =>
        {
            OnMoves(a);
        }, des, 0, time).OnComplete(()=>
        {
            sort.Sort((a, b) =>
            {
                if (a.position.z < b.position.z)
                {
                    return -1;
                }
                else if (a.position.z == b.position.z)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
            });
            float algin = Mathf.Asin(sort[0].position.x / r);
            float algintime =Mathf.Abs(algin * r / des);
            DT.To((a) =>
            {
                allang = a;
                Move();
            }, allang, allang + algin, algintime);
        });
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CreateCamper : MonoBehaviour
{
    float l;
    float n = 15;
    float r;
    // Start is called before the first frame update
    void Start()
    {
        l = 150 * n;
        r = l / (2 * Mathf.PI);
        float ang = 2 * Mathf.PI / n;
        float sunang = 2 * Mathf.PI / (n * 10);
        VertexHelper vh = new VertexHelper();
        for (int i = -5; i <= 5; i++)
        {
            float x = Mathf.Sin(i * sunang) * r- Mathf.Sin(0) * r;
            float z = Mathf.Cos(i * sunang) * r- Mathf.Cos(0) * r;
            float y = 113;
            float y0 = -113;
            float uvx = (float)(i * 5) / 10;
            vh.AddVert(new Vector3(x, y, z), Color.white, new Vector2(uvx, 1));
            vh.AddVert(new Vector3(x, y0, z), Color.white, new Vector2(uvx, 0));
            if(i<5)
            {
                int j = i + 5;
                vh.AddTriangle(j * 2 + 1, j * 2, (j + 1) * 2);
                vh.AddTriangle(j * 2 + 1, (j+1) * 2, (j + 1) * 2+1);
            }
        }
        Mesh mesh = new Mesh();
        vh.FillMesh(mesh);
        GetComponent<MeshFilter>().mesh = mesh;
        GetComponent<MeshCollider>().sharedMesh = mesh;
      
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值