美文网首页
点击左右按钮循环展示图片

点击左右按钮循环展示图片

作者: 萧非子 | 来源:发表于2017-10-11 17:45 被阅读89次
    左右展示图片.png

    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;
            }
    
        }
    }  
    

    }

    相关文章

      网友评论

          本文标题:点击左右按钮循环展示图片

          本文链接:https://www.haomeiwen.com/subject/lxowyxtx.html