using System.Collections; using System.Collections.Generic; using UnityEngine; public class CardCarousel : MonoBehaviour { [Header("垂直布局设置")] public float startOffset = -1; // 起始Y位置(顶部) public float endOffset = 1; // 结束Y位置(底部) public float cardSpacing = 100; // 卡片垂直间距 [Header("运动控制")] public float slideSpeed = 100f; // 垂直移动速度 public bool slideDownward = false; // 向下滑动为true,向上滑动为false public bool isPlaying = true; private List imageList; private void Start() { InitializeCarousel(); } private void InitializeCarousel() { imageList = new List(); float currentYPosition = startOffset; // 从顶部开始 foreach (RectTransform child in transform) { imageList.Add(child); // 设置垂直位置(X保持为0) child.anchoredPosition = new Vector2(0f, currentYPosition); currentYPosition -= cardSpacing; // 向下排列,所以减去间距 } Debug.Log($"初始化了 {imageList.Count} 个卡片,起始Y: {startOffset}, 间距: {cardSpacing}"); } private void Update() { if (!isPlaying || imageList.Count == 0) return; // 计算垂直移动量 float verticalDirection = slideDownward ? -1f : 1f; float deltaY = verticalDirection * slideSpeed * Time.deltaTime; foreach (var image in imageList) { // 垂直移动 image.anchoredPosition += new Vector2(0f, deltaY); // 检查是否需要回收 if (slideDownward) { // 向下滑动:卡片移动到endOffset(底部)以下时,回收到顶部 if (image.anchoredPosition.y < endOffset) { // 找到当前最高的卡片 float highestY = GetHighestCardPosition(); image.anchoredPosition = new Vector2(0f, highestY + cardSpacing); } } else { // 向上滑动:卡片移动到startOffset(顶部)以上时,回收到底部 if (image.anchoredPosition.y > startOffset) { // 找到当前最低的卡片 float lowestY = GetLowestCardPosition(); image.anchoredPosition = new Vector2(0f, lowestY - cardSpacing); } } } } /// /// 获取当前最高卡片的位置(最大的Y值) /// private float GetHighestCardPosition() { if (imageList.Count == 0) return startOffset; float highestY = imageList[0].anchoredPosition.y; foreach (var card in imageList) { if (card.anchoredPosition.y > highestY) { highestY = card.anchoredPosition.y; } } return highestY; } /// /// 获取当前最低卡片的位置(最小的Y值) /// private float GetLowestCardPosition() { if (imageList.Count == 0) return endOffset; float lowestY = imageList[0].anchoredPosition.y; foreach (var card in imageList) { if (card.anchoredPosition.y < lowestY) { lowestY = card.anchoredPosition.y; } } return lowestY; } /// /// 添加新卡片到轮播 /// public void AddCard(RectTransform newCard) { if (newCard == null) return; newCard.SetParent(transform); newCard.localScale = Vector3.one; // 获取当前最高卡片位置,新卡片放在它下面 float highestY = GetHighestCardPosition(); newCard.anchoredPosition = new Vector2(0f, highestY - cardSpacing); imageList.Add(newCard); } /// /// 设置播放状态 /// public void SetPlaying(bool playing) { isPlaying = playing; } /// /// 设置滑动方向(true=向下,false=向上) /// public void SetDirection(bool downward) { slideDownward = downward; } /// /// 切换滑动方向 /// public void ToggleDirection() { slideDownward = !slideDownward; Debug.Log($"滑动方向切换为: {(slideDownward ? "向下" : "向上")}"); } /// /// 获取所有卡片位置信息(用于调试) /// public void DebugCardPositions() { for (int i = 0; i < imageList.Count; i++) { Debug.Log($"卡片 {i}: Y位置 = {imageList[i].anchoredPosition.y}"); } } }