Unity3D 简易轮播图

简易轮播图

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

public class LuoBo : MonoBehaviour
{
    public List<Image> LunBoTu;
    public int indexLast;
    public int index;
    public int indexNext;

    public bool isCanMove = true;
    public Tweener tweener1;
    public Tweener tweener2;

    private void OnEnable()
    {
        isCanMove = true;

        index = 0;
        indexLast = LunBoTu.Count - 1;
        indexNext = index + 1;

        foreach (Image image in LunBoTu)
        {
            image.gameObject.transform.localScale = Vector3.zero;
        }
        LunBoTu[index].gameObject.transform.localScale = Vector3.one;
        LunBoTu[index].transform.localPosition = new Vector2(0, 0);
    }
    //向左移动事件
    public void LeftButtonEvent()
    {
        if (!isCanMove) return;
        isCanMove = false;
        tweener1.Kill();
        tweener2.Kill();
        LunBoTu[index].gameObject.transform.localScale = Vector3.one;
        LunBoTu[index].transform.localPosition = new Vector2(0, 0);
        tweener1 = LunBoTu[index].gameObject.transform.DOLocalMoveX(-1000, 1);
        LunBoTu[indexNext].gameObject.transform.localScale = Vector3.one;
        LunBoTu[indexNext].transform.localPosition = new Vector2(1000, 0);
        tweener2 = LunBoTu[indexNext].gameObject.transform.DOLocalMoveX(0, 1).OnComplete(LeftButtonEventEnd); ;
    }
    //向右移动事件
    public void RightButtonEvent()
    {
        if (!isCanMove) return;
        isCanMove = false;
        tweener1.Kill();
        tweener2.Kill();
        LunBoTu[index].gameObject.transform.localScale = Vector3.one;
        LunBoTu[index].transform.localPosition = new Vector2(0, 0);
        tweener1 = LunBoTu[index].gameObject.transform.DOLocalMoveX(1000, 1);
        LunBoTu[indexLast].gameObject.transform.localScale = Vector3.one;
        LunBoTu[indexLast].transform.localPosition = new Vector2(-1000, 0);
        tweener2 = LunBoTu[indexLast].gameObject.transform.DOLocalMoveX(0, 1).OnComplete(RightButtonEventEnd);
    }
    //向左移动结束后事件
    void LeftButtonEventEnd()
    {
        indexLast = index;

        if (index == LunBoTu.Count - 1)
        {
            index = 0;
        }
        else
        {
            index++;
        }

        if (indexNext == LunBoTu.Count - 1)
        {
            indexNext = 0;
        }
        else
        {
            indexNext++;
        }

        LunBoTu[indexLast].gameObject.transform.localScale = Vector3.zero;
        LunBoTu[indexNext].gameObject.transform.localScale = Vector3.zero;

        isCanMove = true;
    }
    //向右移动结束后事件
    void RightButtonEventEnd()
    {
        indexNext = index;

        if (index == 0)
        {
            index = LunBoTu.Count - 1;
        }
        else
        {
            index--;
        }

        if (indexLast == 0)
        {
            indexLast = LunBoTu.Count - 1;
        }
        else
        {
            indexLast--;
        }

        LunBoTu[indexLast].gameObject.transform.localScale = Vector3.zero;
        LunBoTu[indexNext].gameObject.transform.localScale = Vector3.zero;

        isCanMove = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            LeftButtonEvent();
        }
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            RightButtonEvent();
        }
    }
    private void OnDisable()
    {
        tweener1.Kill();
        tweener2.Kill();
    }
}



在这里插入图片描述

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值