Unity system background image carousel

Unity system background image carousel

set up
Insert image description here

Insert image description here
code

/*****************************************************************
*CopyRight(C) 2021 by  
 *All rights reserved. 
 *FileName:ControlPictureScript.cs 
 *Author:Zhaozhen
 *Version:1.0 
 *UnityVersion:2020.3.1f1
 *Date:2022/11/15 17:25:51 
 *Description:    加载界面轮播图
 *History:
*******************************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using System.Reflection;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;

public class ControlPictureScript : MonoBehaviour
{
    
    
    public Transform LoadPanel1;
    public Transform LoadPanel2;
    public Transform LoadPanel3;
    public Sprite[] Sprites;
    public int second = 5;//轮播间隔时间
    private float i;
    private Transform Temp;
    private int index = 0;
    private bool time = true; 
    // Start is called before the first frame update
    private void Awake()
    {
    
    
        LoadPanel1.GetComponent<Image>().sprite = Sprites[index];
        index = index + 1;
        LoadPanel2.GetComponent<Image>().sprite = Sprites[index];
        LoadPanel1.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, 0);
        LoadPanel2.GetComponent<RectTransform>().anchoredPosition = new Vector2(Screen.width, 0);
        LoadPanel3.GetComponent<RectTransform>().anchoredPosition = new Vector2(Screen.width, 0);
    }

    // Update is called once per frame
    void FixedUpdate() 
    {
    
    
        if (time)
        {
    
    
            i++;
        }
        if (i == second * 50)
        {
    
    
            time = false;
            i = 0;
            LoadPanel1.GetComponent<RectTransform>().DOAnchorPosX(-Screen.width, 0.99f).SetAutoKill(true).OnComplete(() =>
            {
    
    
                LoadPanel1.position = LoadPanel3.position;
            });
            LoadPanel2.GetComponent<RectTransform>().DOAnchorPosX(0, 1.0f).SetAutoKill(true).OnComplete(() =>
            {
    
    
                Temp = LoadPanel2;
                LoadPanel2 = LoadPanel1;
                LoadPanel1 = Temp;
                index++;
                if (index >= Sprites.Length)
                {
    
    
                    index = 0;
                }
                LoadPanel2.GetComponent<Image>().sprite = Sprites[index];
                time = true;
            });
        }
    }
}

Effect
Insert image description here

Guess you like

Origin blog.csdn.net/qq_37179591/article/details/128496900