UGUI实现卡片椭圆方向滚动

1、搭建场景

使用DoTween实现卡片的动画移动。

脚本:

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

public class UIRotate : MonoBehaviour {

    GameObject[] sprites;       //图片精灵集合
    int halfSize;
    Vector2 screenCenterPos;    //屏幕中心点的位置

    public float startAngle;    //中心卡牌的角度
    float deltaAngle;    //相邻卡牌的角度插值
    public float moveSpeed;     //移动动画的速度

    public Vector3 center;      //椭圆的中心
    public float A = 1;         //长轴
    public float B = 1;         //短轴

    int cardcount;              //卡牌数量

    private void Start()
    {
        Init();
    }

    void Init()
    {
        
        screenCenterPos = new Vector2(Screen.width * 0.5f, Screen.height * 0.5f);
        cardcount = transform.childCount;
        deltaAngle = 360 / cardcount;       //根据卡牌的数量得到卡牌间的角度
        halfSize = (cardcount - 1) / 2;
        sprites = new GameObject[cardcount];
        for (int i = 0; i < cardcount; i++)
        {
            sprites[i] = transform.GetChild(i).gameObject;
            SetPosition(i);
            SetDeeps(i);
        }
    }

    /// <summary>
    /// 椭圆的半径长轴为A,短半轴为B,计算椭圆上一点的位置
    /// </summary>
    /// <param name="index"></param>
    /// <param name="userTweener"></param>
    void SetPosition(int index, bool userTweener = true)
    {
        //计算每一张卡片在椭圆上相对中间卡牌的角度
        float angle = 0;
        if (index < halfSize)
        {
            angle = startAngle - (halfSize - index) * deltaAngle;
        }
        else if (index > halfSize)
        {
            angle = startAngle + (index - halfSize) * deltaAngle;
        }
        else
        {
            angle = startAngle;
        }

        //通过卡牌的角度,计算对应的位置
        float xpos = A * Mathf.Cos((angle / 180) * Mathf.PI);
        float ypos = B * Mathf.Sin((angle / 180) * Mathf.PI);

        Vector2 pos = new Vector2(xpos, ypos);
        //通过doTween控制卡牌移动动画
        if (!userTweener)
        {
            sprites[index].GetComponent<Image>().rectTransform.DOMove(new Vector2(screenCenterPos.x + pos.x, screenCenterPos.y + pos.y), 0f);
        }
        else
        {
            sprites[index].GetComponent<Image>().rectTransform.DOMove(new Vector2(screenCenterPos.x + pos.x, screenCenterPos.y + pos.y), 1f);
        }
    }

    //计算每一张卡片的层级
    void SetDeeps(int index)
    {
        int deep = 0;
        if (index < halfSize)
        {
            deep = index;
        }
        else if (index > halfSize)
        {
            deep = sprites.Length - (index + 1);
        }
        else
        {
            deep = halfSize;
        }
        sprites[index].GetComponent<RectTransform>().SetSiblingIndex(deep);
    }

    //左侧按钮点击,向左移动
    public void OnLeftBtnClick()
    {
        int length = sprites.Length;
        GameObject temp = sprites[0];

        for (int i = 0; i < length; i++)
        {
            if (i == length - 1)
            {
                sprites[i] = temp;
            }
            else
            {
                sprites[i] = sprites[i + 1];
            }
        }

        for (int i = 0; i < length; i++)
        {
            SetPosition(i);
            SetDeeps(i);
        }
    }

    //右侧按钮点击,向右移动
    public void OnRightBtnClick()
    {
        int length = sprites.Length;
        GameObject temp = sprites[length - 1];
        for (int i = length - 1; i >= 0; i--)
        {
            if (i == 0)
            {
                sprites[i] = temp;
            }
            else
            {
                sprites[i] = sprites[i - 1];
            }
        }

        for (int i = 0; i < length; i++)
        {
            SetDeeps(i);
            SetPosition(i);
        }
    }
}

运行效果:

转载自:https://blog.csdn.net/u011484013/article/details/73571988

猜你喜欢

转载自blog.csdn.net/qq_38721111/article/details/89216725
今日推荐