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);
    }
}
相关推荐
元气少女小圆丶5 小时前
SenseGlove Nova 2+Unity开发笔记1
笔记·学习·unity
mxwin6 小时前
Unity URP下新技术MSSPT 取代SSR和光线追踪
unity·游戏引擎·shader
LF男男9 小时前
IBuilder.cs 接口
unity
心之所向,自强不息10 小时前
# Unity MCP + Codex CLI 完整教程(Windows)
windows·unity·游戏引擎
KillJUMP11 小时前
GODOT SHADER关键函数
游戏引擎·godot
Oiiouui14 小时前
Godot(4.x): Python处理转换Excel为注入Json
游戏引擎·godot
追光者♂16 小时前
【测评系列3】CSDN AI数字营销实测体验官:测试 开源项目——Superpowers 游戏引擎从零开发实战指南
人工智能·深度学习·机器学习·typescript·开源·游戏引擎·superpowers
小拉达不是臭老鼠17 小时前
Unity数据持久化_Json
学习·unity
heimeiyingwang17 小时前
【架构实战】订单系统架构设计:电商核心系统的演进
unity·架构·系统架构
元气少女小圆丶17 小时前
SenseGlove Nova 2+Unity开发笔记3
笔记·unity·游戏引擎