Unity中的 2D轮转图(有回正效果,以及指定到达某处位置)

using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;


public class Lun2D : MonoBehaviour, IDragHandler, IEndDragHandler
{
    //份数
    int count2d=5;
    //半径
    float r2d = 300;
    //周长
    float zc2d;
    //每份的弧度
    float hu2d;
    //改变的弧度
    float movehu2d;
    //要生成的图片对象
    public Image Image;
    //最大缩放
    float max2d = 1;
    //最小缩放
    float min2d = 0.3f;
    //图片间距
    public int space2d = 200;
    List<GameObject> gameObjects2d = new List<GameObject>();
    List<Transform> transforms2d = new List<Transform>();
    private void Awake()
    {
        hu2d = 2 * Mathf.PI / count2d;
        zc2d = (Image.rectTransform.sizeDelta.x + space2d) * count2d;//周长
        r2d = zc2d / (2 * Mathf.PI);//半径
        CreateImage();
    }
    //指定去某个位置
    public void ChangeImage(int n)
    {
        //获取当前下标
        int now = gameObjects2d.IndexOf(transforms2d[count2d - 1].gameObject);
        float angel_move = 0;
        angel_move = (now - n) * hu2d;
        DOTween.To(() => movehu2d, t =>
        {
            movehu2d = t;
            CreateImage();
        }, movehu2d + angel_move, 2);
    }
    public void OnClickUI()
    {
        int n = UnityEngine.Random.Range(0, count2d);
        ChangeImage(n);
    }
    
    private void CreateImage()
    {
        for (int i = 0; i < count2d; i++)
        {
            if (gameObjects2d.Count <= i)
            {
                gameObjects2d.Add(Instantiate(Image.gameObject, transform));
                transforms2d.Add(gameObjects2d[i].transform);
            }
            float x = Mathf.Sin(hu2d * i + movehu2d) * r2d;
            float y = Mathf.Cos(hu2d * i + movehu2d) * r2d;

            float bl = (y + r2d) / (2 * r2d);
            float xzbl = max2d * (1 - bl) + min2d * bl;//限制比例
            gameObjects2d[i].transform.localPosition = new Vector3(x, 0, 0);
            gameObjects2d[i].transform.localScale = Vector3.one * xzbl;
        }
        transforms2d = transforms2d.OrderBy(x => x.localScale.x).ToList();
        for (int i = 0; i < transforms2d.Count; i++)
        {
            transforms2d[i].SetSiblingIndex(i);//设置对应下标
        }
    }
    void Start()
    {
        //循环赋图片
        for (int i = 0; i < gameObjects2d.Count; i++)
        {
            gameObjects2d[i].GetComponent<Image>().sprite = Resources.Load<Sprite>("Image/index_" + i);
        }
        //先回正
        transforms2d = transforms2d.OrderBy(x => x.localScale.x).ToList();
        float needhu = Mathf.Asin(transforms2d[count2d - 1].localPosition.x / r2d);
        DOTween.To((b) =>
        {
            movehu2d = b;
            CreateImage();
            int index = gameObjects2d.IndexOf(transforms2d[count2d - 1].gameObject);
        }, movehu2d, movehu2d + needhu, 1);
    }

    public void OnDrag(PointerEventData eventData)
    {
        var dis = eventData.delta.x / r2d;
        movehu2d -= dis;
        CreateImage();
    }
    public void OnEndDrag(PointerEventData eventData)
    {
        var dis = eventData.delta.x / r2d;
        DOTween.To((a) =>
        {
            movehu2d -= a;
            CreateImage();
        }, dis, 0, Mathf.Abs(dis)).OnComplete(() =>
        {
            transforms2d = transforms2d.OrderBy(x => x.localScale.x).ToList();
            float needhu = Mathf.Asin(transforms2d[count2d - 1].localPosition.x / r2d);
            DOTween.To((b) =>
            {
                movehu2d = b;
                CreateImage();
                int index = gameObjects2d.IndexOf(transforms2d[count2d - 1].gameObject);
            }, movehu2d, movehu2d + needhu, 1);
        });
    }

}

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值