122 lines
3.3 KiB
C#
122 lines
3.3 KiB
C#
using DG.Tweening;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
[AddComponentMenu("Carousel/轮播")]
|
|
public class Carousel : MonoBehaviour
|
|
{
|
|
public static Carousel inst;
|
|
public bool isClick = true;
|
|
public List<RectTransform> tfs; //需要扩展只需往该数组添加Item即可
|
|
|
|
public Image imageComponent; // UI Image 组件
|
|
public List<Sprite> imagesToDisplay; // 要轮播的图片数组
|
|
public int currentIndex = 0; // 当前显示的图片索引
|
|
public float switchInterval = 2f; // 图片切换间隔时间
|
|
private float timer = 0f; // 计时器
|
|
public Button close_button;
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
inst = this;
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
if (imageComponent != null && imagesToDisplay.Count > 0)
|
|
{
|
|
imageComponent.sprite = imagesToDisplay[currentIndex]; // 设置初始图片
|
|
}
|
|
|
|
close_button.onClick.AddListener(() =>
|
|
{
|
|
gameObject.SetActive(false);
|
|
});
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
//timer += Time.deltaTime;
|
|
|
|
//if (timer >= switchInterval)
|
|
//{
|
|
// ShowNextImageWithAnimation();
|
|
// timer = 0f;
|
|
//}
|
|
}
|
|
|
|
void ShowNextImageWithAnimation()
|
|
{
|
|
currentIndex = (currentIndex + 1) % imagesToDisplay.Count; // 循环获取下一张图片的索引
|
|
|
|
// 图片切换动画
|
|
StartCoroutine(AnimateImageChange(imagesToDisplay[currentIndex]));
|
|
}
|
|
|
|
IEnumerator AnimateImageChange(Sprite newImage)
|
|
{
|
|
// 实现图片切换动画,这里使用简单的淡入淡出效果
|
|
float duration = 0.15f;
|
|
float elapsedTime = 0f;
|
|
Color originalColor = imageComponent.color;
|
|
|
|
while (elapsedTime < duration)
|
|
{
|
|
imageComponent.color = Color.Lerp(originalColor, Color.clear, elapsedTime / duration);
|
|
elapsedTime += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
imageComponent.sprite = newImage;
|
|
|
|
elapsedTime = 0f;
|
|
while (elapsedTime < duration)
|
|
{
|
|
imageComponent.color = Color.Lerp(Color.clear, originalColor, elapsedTime / duration);
|
|
elapsedTime += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
imageComponent.color = originalColor;
|
|
isClick = true;
|
|
}
|
|
|
|
// 外部调用该方法来动态配置要显示的图片
|
|
public void SetImages(List<Sprite> images)
|
|
{
|
|
imagesToDisplay = images;
|
|
currentIndex = 0;
|
|
if (imageComponent != null && imagesToDisplay.Count > 0)
|
|
{
|
|
imageComponent.sprite = imagesToDisplay[currentIndex];
|
|
}
|
|
}
|
|
|
|
|
|
public void LeftBtnClickEvent()//左边按键
|
|
{
|
|
if (!isClick) return;
|
|
var num = currentIndex - 1;
|
|
if (num < 0)
|
|
num = imagesToDisplay.Count - 1;
|
|
currentIndex = (num) % imagesToDisplay.Count; // 循环获取下一张图片的索引
|
|
isClick = false;
|
|
// 图片切换动画
|
|
StartCoroutine(AnimateImageChange(imagesToDisplay[currentIndex]));
|
|
}
|
|
public void RightBtnClickEvent()//右边按键
|
|
{
|
|
if (!isClick) return;
|
|
var num = currentIndex + 1;
|
|
currentIndex = (num) % imagesToDisplay.Count; // 循环获取下一张图片的索引
|
|
isClick = false;
|
|
// 图片切换动画
|
|
StartCoroutine(AnimateImageChange(imagesToDisplay[currentIndex]));
|
|
}
|
|
|
|
}
|