Unity drag and rotate effect (rotation image)

Table of contents

 

3D rotation effect

3D rotation effect code part

UI rotation effect


3D rotation effect

 3D rotation effect code part

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using DG.Tweening;

//3D轮转图
public class RotationChart : MonoBehaviour, IDragHandler,IEndDragHandler
{
    int num = 6;
    List<GameObject> players = new List<GameObject>();
    List<Transform> sortlist = new List<Transform>();
    float distance = 0;
    float speed = 1;
    float r;
    public Transform img;
    GameObject game;
    public void OnDrag(PointerEventData eventData)
    {
        distance -= eventData.delta.x/Screen.width;
        Move();
    }
    public void OnEndDrag(PointerEventData eventData)
    {
        float time = Mathf.Abs(eventData.delta.x) / speed;
        time = time > 2 ? 2 : time;
        DOTween.To((float a) =>
        {
            distance -= a / Screen.width;
            Move();
        }, eventData.delta.x, 0, time).SetEase(Ease.InOutQuad).OnComplete(() =>
        {
            sortlist.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 moveAng = Mathf.Asin(sortlist[0].position.x / r);
            float distance2 = (moveAng / (Mathf.PI * 2)) * num;
            float time2= Mathf.Abs(distance2) / speed;
            DOTween.To((float d) =>
            {
                distance = d;
                Move();
            }, distance, distance + distance2, time2).OnComplete(() =>
            {
              // img.GetComponent<RedarChart>().SetAllDirty();//刷新雷达图数据
            });
        });
    }
    void Start()
    {
        Move();
    }

    public void Move()
    {
        float ang = Mathf.PI * 2 / num;
        r = num / (Mathf.PI * 2);
        for (int i = 0; i < num; i++)
        {
            float x = Mathf.Sin(ang * i + distance) * r;
            float z = Mathf.Cos(ang * i + distance) * r;
            if (players.Count <= i)
            {
                players.Add(Instantiate(Resources.Load<GameObject>("huan" + (i+1)), transform));
                sortlist.Add(players[i].transform);
            }
            players[i].transform.position = new Vector3(x, 0, z);
        }
    }
}

UI rotation effect

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using DG.Tweening;

//2D轮转图
public class RotationChart2D : MonoBehaviour, IDragHandler, IEndDragHandler
{
    public GameObject prefab;//图片预制体
    public GameObject can;//Canvas
    int num = 13;//数量
    float spacing = 10;//间距
    float spacingMax = 1;//缩放最大值
    float spacingMin = 0.5f;//缩放最小值
    float distance = 0;//转动距离
    float ang;//弧度
    float l;//周长
    float r;//半径
    float speed = 10;//速度
    List<GameObject> imglist = new List<GameObject>();//图片集合
    List<Transform> sortimglist = new List<Transform>();//排序后的图片集合
    public Button yg;//老虎机摇杆
    public Text text;//数据显示
    int www = 0;//id
    //拖拽
    public void OnDrag(PointerEventData eventData)
    {
        //拖动距离
        distance -= eventData.delta.y;
        //调用移动方法
        Move();
    }
    //拖拽结束
    public void OnEndDrag(PointerEventData eventData)
    {
        //结束时间
        float time = Mathf.Abs(eventData.delta.x) / speed;
        //时间限制
        time = time > 2 ? 2 : time;
        //移动惯性
        DOTween.To((float a) =>
        {
            //改变移动距离
            distance -= a;
            //调用移动方法
            Move();
        }, eventData.delta.y, 0, time).SetEase(Ease.InOutQuad).OnComplete(() =>
        {
            //移动结束后找到距离相机最近的图片id
            //int index = sortimglist[sortimglist.Count - 1].GetComponent<RotationCall>().id;
            调用对齐方法
            //Aalide(index);
        });
    }
    //对齐
    public void Aalide(int id)
    {
        //当前最前方图片id
        int b = 1;// sortimglist[sortimglist.Count - 1].GetComponent<RotationCall>().id;
        //计算当前最前方图片与传过来的图片id的差值
        int c = b - id;
        //计算另外一条线路移动距离
        int d = num - Mathf.Abs(c);
        //得出最终移动方向和距离
        int e = d > Mathf.Abs(c) ? c : (c > 0 ? -d : d);
        //计算移动弧度
        float moveAng = Mathf.Asin(sortimglist[sortimglist.Count - 1].localPosition.y / r) + (e * ang);
        //计算移动距离
        float distance2 = (moveAng / (Mathf.PI * 2)) * l;
        //计算移动时间
        float time2 = Mathf.Abs(distance2) / speed;
        //对齐
        //DOTween.To((float d) =>
        //{
        //    //改变移动距离
        //    distance = d;
        //    //调用移动方法
        //    Move();
        //}, distance, distance + distance2, 1).OnComplete(() =>
        //{
        //    //操作选中的玩家数据
            
        //});
    }
    // Start is called before the first frame update
    void Start()
    {
        //计算弧度
        ang = (2 * Mathf.PI) / num;
        //计算周长
        l = (prefab.GetComponent<RectTransform>().sizeDelta.x + spacing) * num;
        //计算半径
        r = l / (2 * Mathf.PI);
        //调用移动方法
        Move();
        //摇杆监听
        //yg.onClick.AddListener(() =>
        //{
        //    //获取随机数
        //    int n = UnityEngine.Random.Range(0, 13);
        //    Debug.Log(n + 1);
        //    //调用对齐方法
        //    Aalide(n);
        //});
    }
    public void Move()
    {
        //计算移动弧度
        float moveAng = (distance / l) * (2 * Mathf.PI);
        //循环图片个数
        for (int i = 0; i < num; i++)
        {
            //计算点的坐标位置
            float y = Mathf.Sin(i * ang + moveAng) * r;
            float z = Mathf.Cos(i * ang + moveAng) * r;
            //判断并创建图片
            if (imglist.Count <= i)
            {
                //创建图片添加到图片集合
                imglist.Add(Instantiate(prefab, transform));
                //添加脚本给id赋值
              //  imglist[i].AddComponent<RotationCall>().id = i;
                //赋值图片
                imglist[i].GetComponent<Image>().sprite = Resources.Load<Sprite>("纸牌资源/card_1_" + (i + 1));
                //添加到排序集合
                sortimglist.Add(imglist[i].transform);
            }
            //给图片赋值位置
            imglist[i].transform.localPosition = new Vector3(0, y, 0);
            //计算图片缩放
            float scalingRatio = 1 - (z + r) / (2 * r);
            //赋值图片的缩放
            imglist[i].transform.localScale = Vector3.one * ((spacingMax - spacingMin) * scalingRatio + spacingMin);
        }
        //排序
        sortimglist.Sort((a, b) =>
        {
            //大于返回1
            if (a.localScale.x > b.localScale.x)
            {
                return 1;
            }//等于返回0
            else if (a.localScale.x == b.localScale.x)
            {
                return 0;
            }//小于返回-1
            else
            {
                return -1;
            }
        });
        //循环获取id
        for (int i = 0; i < sortimglist.Count; i++)
        {
            //获取节点id
            sortimglist[i].SetSiblingIndex(i);
        }
    }
}

Guess you like

Origin blog.csdn.net/q1295006114/article/details/131313718