3d轮转图

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


public class cyclogram : MonoBehaviour
{
    public GameObject prefb;
    public int num;
    //半径
    public  float r;
    //减速度
    public float dec = 0.8f;
    List<GameObject>list=new List<GameObject>();//存所有
    float angle;
    List<Transform>sort=new List<Transform>();
    public Texture[] textures;
    void Start()
    {
        
       
        angle = 2 * Mathf.PI / num;
        OnMove();


    }
    float allang = 0;
    public void OnDrag(float dis)
    {
        //
        float moveang = dis / r;//移动弧度
        allang-=moveang;//存的角度加移动的弧度 
        OnMove();
    }
    /// <summary>
    /// 移动
    /// </summary>
    /// <param name=""></param>
    /// 
    public void OnMove()
    {
        for (int i = 0; i < num; i++)
        {

            float x = Mathf.Sin(i * angle + allang) * r;
            float z = Mathf.Cos(i * angle + allang) * r;
            if (list.Count <= i)//创建
            {
                GameObject spher = Instantiate(prefb);
                spher.transform.parent = transform;
                spher.GetComponent<cyclogramitem>().cyclogram = this;
                list.Add(spher);
                sort.Add(spher.transform);
              
              
                spher.GetComponent<MeshRenderer>().material.mainTexture = textures[i];
             

            }
            list[i].transform.localPosition = new Vector3(x, 0, z);
            ///当前弧度转换成角度
            list[i].transform.localEulerAngles = Vector3.up * ((i * angle + allang) * Mathf.Rad2Deg);//性能优化,一遍即可
        }
    }
    public void Inertia(float dis)//惯性
    {
        float time =Mathf.Abs( dis / dec);
        DT.To((a) =>
        {
            OnDrag(a);
        }, dis, 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 alinging = Mathf.Asin(sort[0].localPosition.x / r);
        float alingtime = Mathf.Abs(alinging * r / dec);
        DT.To((a) =>
        {
            allang = a;
            OnMove();
         
        },allang,allang+alinging,alingtime);
          
    }
  
}

 dotween效果

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

public class DT : MonoBehaviour
{
    public Action<float> action;
    public float begin;
    public float end;
    public float time;
    public Action complete;
    float nowtime;
    public static DT To(Action<float> action,float begin,float end,float time)
    {
        GameObject dt = new GameObject("DT");
        DT dotween= dt.AddComponent<DT>();
        dotween.action = action;
        dotween.begin = begin;
        dotween.end = end;
        dotween.nowtime =Time.time;
      
        return dotween; 

    }
  public void OnComplete(Action complete)
    {
        this.complete = complete;
    }

    // Update is called once per frame
    void Update()
    {
        if (Time.time - nowtime < time)//现在时间-初始化时间小于传的time
        {
            float t = Time.time - nowtime;
            float p = t / time;
            float a = begin * (1 - p) + end * p;
            action(a);
        }
        else
        {
            action(end);
            if (complete!=null)
            {
                complete();
            }
            Destroy(gameObject);
        }
           
        }
    
}

预制体脚本

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

public class cyclogramitem : MonoBehaviour
{
     public cyclogram cyclogram;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
     }
    private void OnMouseDrag()
    {
        Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
        Vector3 next = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, pos.z  )); 
        //距离-移动
       float dis=next.x-transform.position.x;
        cyclogram.OnDrag(dis);
    }
    //鼠标抬起
    private void OnMouseUp()
    {
        Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
        Vector3 next = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, pos.z));
        //距离-移动
        float dis = next.x - transform.position.x;
        // cyclogram.OnDrag(dis);
        cyclogram.Inertia(dis); 
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CreatLZ : MonoBehaviour
{
    float n = 15;
    float L;
   public    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 sunagn = 2 * Mathf.PI / (n * 10);//弯曲的角度
        VertexHelper vh = new VertexHelper();
        for (int i = -5; i <= 5; i++)
        {
            float x = Mathf.Sin(i * sunagn) * r-Mathf.Sin  (0) *  r;//减去圆心为0的坐标
            float z = Mathf.Cos(i * sunagn) * r - Mathf.Cos(0) * r;
            //高226
            float y = 113;
            float y0 = -113;
            float uvx = (float)(i + 5) / 10;//!!1!
            vh.AddVert(new Vector3(x, y, z), Color.blue, new Vector2(uvx, 1));//开始//!!
            vh.AddVert(new Vector3(x, y0, z), Color.blue, new Vector2(uvx, 0));//结束 //!!1
            if (i<5)
            { 
                int j = i + 5;
                vh.AddTriangle(j * 2 + 1, j * 2, (j + 1) * 2);//绘制顺序反,y,z换位置
                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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值