点击左右按钮循环展示图片
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//图片展示,点击左右按钮循环展示图片
public class ScrollImage2 : MonoBehaviour {
private Image image;
public Sprite[] spriteArray;
private Button leftImageButton;
private Image leftImageButton_Image;
private Button rightImageButton;
private Image rightImageButton_Image;
private void Awake()
{
image = transform.Find("ScrollImage2").GetComponent<Image>();
leftImageButton = transform.Find("LeftImageButton").GetComponent<Button>();
rightImageButton = transform.Find("RightImageButton").GetComponent<Button>();
leftImageButton_Image = transform.Find("LeftImageButton").GetComponent<Image>();
rightImageButton_Image = transform.Find("RightImageButton").GetComponent<Image>();
leftImageButton.onClick.AddListener(LeftImageButtonClick);
rightImageButton.onClick.AddListener(RightImageButtonClick);
}
void Start () {
image.sprite = spriteArray[0];
leftImageButton_Image.sprite = spriteArray[spriteArray.Length-1];
rightImageButton_Image.sprite = spriteArray[1];
}
public void LeftImageButtonClick()
{
for (int i = 0; i < spriteArray.Length; i++)
{
if (spriteArray[i] == image.sprite)
{
if (i > 0)
{
if (i==1)
{
image.sprite = spriteArray[i - 1];
leftImageButton_Image.sprite = spriteArray[spriteArray.Length-1];
rightImageButton_Image.sprite = spriteArray[i];
}
else
{
image.sprite = spriteArray[i - 1];
leftImageButton_Image.sprite = spriteArray[i - 2];
rightImageButton_Image.sprite = spriteArray[i];
}
}
else
{
image.sprite = spriteArray[spriteArray.Length - 1];
leftImageButton_Image.sprite = spriteArray[spriteArray.Length - 2];
rightImageButton_Image.sprite = spriteArray[0];
}
break;
}
}
}
public void RightImageButtonClick()
{
for (int i = 0; i < spriteArray.Length; i++)
{
if (spriteArray[i] == image.sprite)
{
if (i < spriteArray.Length - 1)
{
if (i== spriteArray.Length-2)
{
image.sprite = spriteArray[i + 1];
leftImageButton_Image.sprite = spriteArray[i];
rightImageButton_Image.sprite = spriteArray[0];
}
else
{
image.sprite = spriteArray[i + 1];
leftImageButton_Image.sprite = spriteArray[i];
rightImageButton_Image.sprite = spriteArray[i + 2];
}
}
else
{
image.sprite = spriteArray[0];
leftImageButton_Image.sprite = spriteArray[spriteArray.Length-1];
rightImageButton_Image.sprite = spriteArray[1];
}
break;
}
}
}
}