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);
    }
}
相关推荐
feiduoge3 小时前
教程 44 - 相机系统
windows·游戏引擎·图形渲染
lrh30257 小时前
Custom SRP - 16 Render Scale
3d·unity·srp·render pipeline·render scale
feiduoge10 小时前
教程 43 - 渲染目标和可配置渲染通道
windows·游戏引擎·图形渲染
ellis197015 小时前
Unity出安卓包知识点汇总
android·unity
DoomGT15 小时前
Audio - UE5中的音效播放重启问题
游戏·ue5·游戏引擎·虚幻·虚幻引擎
一线灵17 小时前
Axmol 开发环境快速搭建指南 (新)
游戏引擎
一线灵18 小时前
跨平台游戏引擎 Axmol-2.11.0 发布
游戏引擎
Robot侠19 小时前
ROS1从入门到精通 2:ROS1核心概念详解(节点、话题、服务一网打尽)
unity·游戏引擎·ros·机器人操作系统
世洋Blog1 天前
装饰器模式实践:告别臃肿的继承链,优雅解耦初始化状态管理
unity·设计模式·c#·装饰器模式
feiduoge1 天前
教程 41 - 增强纹理映射(采样器)
windows·游戏引擎·图形渲染