Unity Scroll View内容轮播实现

 

效果图如下

QQ20260310-193111

 直接贴代码

代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;[RequireComponent(typeof(ScrollRect))]
public class LunboUI : MonoBehaviour
{public float scrollSpeed = 0.1f; // 滚动速度,可调整private ScrollRect scrollRect;private bool isScrollingDown = true;void Start(){scrollRect = GetComponent<ScrollRect>();}void FixedUpdate(){if (scrollRect.content.rect.height <= scrollRect.viewport.rect.height){return; // 内容不足,不滚动
        }// 计算新位置float step = scrollSpeed * Time.deltaTime;float newPos = scrollRect.verticalNormalizedPosition + (isScrollingDown ? -step : step);// 边界检查与方向反转if (newPos <= 0){newPos = 0;isScrollingDown = false;}else if (newPos >= 1){newPos = 1;isScrollingDown = true;}scrollRect.verticalNormalizedPosition = newPos;}
}