携程代码轮转图

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

public class Rotart2D : MonoBehaviour, IDragHandler, IEndDragHandler,IBeginDragHandler
{
    public Image prefab;//预制体
    public float spacing = 100;//间距
    public int n;//个数
    float zc;
    float r;
    float ang;
    List<Image> s = new List<Image>();
    List<RectTransform> sl = new List<RectTransform>();
    float moveAng = 0;//拖动弧度
    // Start is called before the first frame update
    void Start()
    {
        ////周长=(图片宽度+间距)*个数
        zc = (prefab.rectTransform.rect.width + spacing) * n;
        //半径=周长/2πr
        r = zc / (2 * Mathf.PI);
        //弧度= 2πr/个数
        ang = 2 * Mathf.PI / n;
        Move();
    }
    private void Move()
    {
        for (int i = 0; i < n; i++)
        {
            //判断集合长度
            if (s.Count <= i)
            {
                s.Add(Instantiate(prefab, transform));
                s[i].GetComponent<Image>().sprite = Resources.Load<Sprite>("Image/Tou_" + i);
                s[i].name = "Tou_" + i;
                sl.Add(s[i].rectTransform);
            }
            int id = i;
            Button btn = s[i].GetComponent<Button>();
            btn.onClick.AddListener(() => 
            {
                int index = s.IndexOf(sl[n - 1].GetComponent<Image>());
                float sp = (id - index) * ang;
                float sp0 = 2 * Mathf.PI - Mathf.Abs(sp);
                sp0 = sp > 0 ? -sp0 : sp0;
                sp = Mathf.Abs(sp) < Mathf.Abs(sp0) ? sp : sp0;
                StopAllCoroutines();//关闭携程
                StartCoroutine(DuiQi(sp));//图片对齐
            });
            float x = Mathf.Sin(i * ang + moveAng) * r;
            float y = Mathf.Cos(i * ang + moveAng) * r;
            float scale = (y + r) / (r * 2) * 0.5f + 0.5f;
            s[i].rectTransform.anchoredPosition = new Vector2(x, 0);
            s[i].transform.localScale = Vector3.one * scale;
        }
        sl.Sort((a, b) =>
        {
            if (a.localScale.x < b.localScale.x)
            {
                return -1;
            }
            else if (a.localScale.x == b.localScale.x)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        });
        for (int i = 0; i < sl.Count; i++)
        {
            sl[i].SetSiblingIndex(i);
        }
    }
    public void OnBeginDrag(PointerEventData eventData)
    {
        StopAllCoroutines();//关闭携程
    }
    public void OnDrag(PointerEventData eventData)
    {
        float dis = eventData.delta.x;
        float drang = dis / r;
        moveAng += drang;
        Move();
    }
    public void OnEndDrag(PointerEventData eventData)
    {
        float dis = eventData.delta.x;
        StartCoroutine(GuanXin(dis));//开启携程  惯性移动
    }

    IEnumerator GuanXin(float dis)
    {
        float drang = dis / r;
        float time = Math.Abs(dis) / 100;
        while (time>0)
        {
            yield return new WaitForSeconds(Time.deltaTime);
            time -= Time.deltaTime;
            if(time<=0)
            {
                time = 0;
            }
            moveAng += drang * time;
            Move();
        }
        if(time<=0)
        {
            StopAllCoroutines();
            StartCoroutine(DuiQi(0));//开启携程  图片对齐
        }
    }

    IEnumerator DuiQi(float sp)
    {
        float AlingAng = sl[n - 1].anchoredPosition.x / r + sp;
        float time = 0;
        float Alingdis = moveAng;
        while (time<0.5f)
        {
            yield return new WaitForSeconds(Time.deltaTime);
            time += Time.deltaTime;
            if(time>=0.5f)
            {
                time = 0.5f;
            }
            moveAng = Mathf.Lerp(Alingdis, Alingdis - AlingAng, time * 2);
            Move();
        }
        if(time>=0.5f)
        {
            StopAllCoroutines();//关闭携程
        }
    }

}

 

posted @ 2023-04-11 14:11  温暖的海  阅读(53)  评论(0)    收藏  举报