简易轮播图
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();
}
}