Unity 动态切换图片

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>切换对象显示</summary>
public class SwitchObject : MonoBehaviour
{
    public GameObject[] goList;

    public Button nextButton;
    public Button previousButton;

    private void Awake()
    {
        previousButton.onClick.AddListener(Previous);
        nextButton.onClick.AddListener(Next);
    }

    private int index;

    private void OnEnable()
    {
        index = 0;
        changeObj();
    }

    public void Next()
    {
        index++;
        if (index > goList.Length - 1) index = goList.Length - 1;
        changeObj();
    }

    public void Previous()
    {
        index--;
        if (index < 0) index = 0;
        changeObj();
    }

    private void changeObj()
    {
        for(int i=0;i< goList.Length;i++)
        {
            goList[i].SetActive(i == index);
        }

        previousButton.interactable= (index != 0);
        nextButton.interactable = (index != goList.Length - 1);

        //previousButton.gameObject.SetActive(index != 0);
        //nextButton.gameObject.SetActive(index != goList.Length - 1);
    }
}
相关推荐
一个小狼娃17 小时前
Android集成Unity避坑指南
android·游戏·unity
极客柒17 小时前
Unity 协程GC优化记录
java·unity·游戏引擎
黄思搏17 小时前
Unity SpriteRenderer 进度条 Shader 实现
unity·游戏引擎
猫屋小鱼丸19 小时前
手把手教你在unity中实现一个视觉小说系统(一)
unity
国服第二切图仔1 天前
Rust开发实战之简单游戏开发(piston游戏引擎)
开发语言·rust·游戏引擎
HahaGiver6661 天前
Unity与Android原生交互开发入门篇 - 打开Unity游戏的设置
android·unity·交互
@LYZY1 天前
Unity TextMeshPro 文本对齐方式详解
unity·游戏引擎·textmeshpro·tmp
在路上看风景1 天前
2.1 ShaderLab - 渲染状态
unity
AA陈超1 天前
虚幻引擎5 GAS开发俯视角RPG游戏 P07-06 能力输入的回调
c++·游戏·ue5·游戏引擎·虚幻
一线灵2 天前
跨平台游戏引擎 Axmol-2.9.1 发布
游戏引擎