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);
    }
}
相关推荐
魔士于安12 小时前
unity 音乐会场景 unity2022
游戏·unity·游戏引擎·贴图·模型
一线灵14 小时前
Axmol 3.x 输入系统重构:从 Touch/Mouse 到统一 Pointer,再到现代 InputField
重构·游戏引擎
Zwarwolf14 小时前
Godot零散知识点项目汇总
游戏引擎·godot
Mediary14 小时前
Unity is running with Administrator privileges, which isnot supported...
unity
游乐码17 小时前
Unity基础(十四)场景异步加载
unity·游戏引擎
mxwin17 小时前
Unity Shader URP:法线在空间变换上的特殊性
unity·游戏引擎·shader
nnsix17 小时前
Unity 动态批处理、静态批处理、GPU Instaning、SRP Batcher 笔记
笔记·unity·单一职责原则
charlee4418 小时前
Unity在安卓端如何调试输出信息
android·unity·adb·游戏引擎·真机调试
TCW112120 小时前
Minetest游戏引擎源代码解析
游戏引擎