当前位置:   article > 正文

Unity 轮播图_unity图片轮播

unity图片轮播

核心脚本在这里插入图片描述

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);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/606858
推荐阅读
相关标签
  

闽ICP备14008679号