赞
踩
核心脚本
using UnityEngine; using UnityEngine.UI; using System.IO; using DG.Tweening; public class ImageSlider : MonoBehaviour { public RawImage mainImage; // 中间显示的主要图片 public RawImage leftImage; // 左侧辅助图片 public RawImage rightImage; // 右侧辅助图片 public Button prevButton; public Button nextButton; private string streamingAssetsPath; public string imagesFolderName = "ImgData"; // 存放图片的文件夹名称 public Texture2D[] images; // 存储要轮播的图片 private int currentImageIndex = 0; public float xPositionToMove = 100.0f; // 设置X轴上移动主图片的目标位置 private void Start() { streamingAssetsPath = Application.streamingAssetsPath; LoadImagesFromStreamingAssets(); // 初始化显示第一张图片 ShowImages(currentImageIndex); DOTween.Init(); DOTween.defaultEaseType = Ease.Linear; DOTween.defaultTimeScaleIndependent = true; // 绑定按钮点击事件 prevButton.onClick.AddListener(ShowPreviousImage); nextButton.onClick.AddListener(ShowNextImage); } private void LoadImagesFromStreamingAssets() { // 获取 StreamingAssets 中的图片路径 string imagesFolderPath = Path.Combine(streamingAssetsPath, imagesFolderName); // 获取文件夹中的所有图片文件 string[] imageFiles = Directory.GetFiles(imagesFolderPath, "*.png"); // 初始化 images 数组 images = new Texture2D[imageFiles.Length]; // 从文件加载图片到数组中 for (int i = 0; i < imageFiles.Length; i++) { byte[] imageData = File.ReadAllBytes(imageFiles[i]); Texture2D texture = new Texture2D(2, 2); texture.LoadImage(imageData); images[i] = texture; } } //private void ShowImages(int currentIndex) //{ // if (currentIndex >= 0 && currentIndex < images.Length) // { // // 显示主要图片 // mainImage.texture = images[currentIndex]; // // 设置当前主图的索引 // currentImageIndex = currentIndex; // // 缩放动画持续时间 // float animationDuration = 0.5f; // // 缩放主图并平滑过渡 // mainImage.rectTransform.DOScale(new Vector3(1.2f, 1.2f, 1.0f), animationDuration) // .SetEase(Ease.OutQuad); // // 恢复辅助图的大小并平滑过渡 // int leftIndex = (currentIndex - 1 + images.Length) % images.Length; // int rightIndex = (currentIndex + 1) % images.Length; // leftImage.texture = images[leftIndex]; // leftImage.rectTransform.localScale = new Vector3(1.0f, 1.0f, 1.0f); // leftImage.rectTransform.DOScale(Vector3.one, animationDuration) // .SetEase(Ease.OutQuad); // rightImage.texture = images[rightIndex]; // rightImage.rectTransform.localScale = new Vector3(1.0f, 1.0f, 1.0f); // rightImage.rectTransform.DOScale(Vector3.one, animationDuration) // .SetEase(Ease.OutQuad); // } //} private void ShowImages(int currentIndex) { if (currentIndex >= 0 && currentIndex < images.Length) { // 显示主要图片 mainImage.texture = images[currentIndex]; mainImage.rectTransform.localScale = new Vector3(1.2f, 1.2f, 1.0f); // 扩大主图 // 显示左侧辅助图片 int leftIndex = (currentIndex - 1 + images.Length) % images.Length; leftImage.texture = images[leftIndex]; leftImage.rectTransform.localScale = new Vector3(1.0f, 1.0f, 1.0f); // 恢复辅助图的大小 // 显示右侧辅助图片 int rightIndex = (currentIndex + 1) % images.Length; rightImage.texture = images[rightIndex]; rightImage.rectTransform.localScale = new Vector3(1.0f, 1.0f, 1.0f); // 恢复辅助图的大小 // 设置当前主图的索引 currentImageIndex = currentIndex; } } private void ShowNextImage() { // 显示下一张图片 currentImageIndex = (currentImageIndex + 1) % images.Length; ShowImages(currentImageIndex); } private void ShowPreviousImage() { // 显示上一张图片 currentImageIndex = (currentImageIndex - 1 + images.Length) % images.Length; ShowImages(currentImageIndex); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。