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);
    }
}
相关推荐
王维志2 小时前
Unity Embedded Browser文档翻译
unity·c#
qiu_zhongya13 小时前
unity以战斗截图并加上微信二维码分享
unity
死也不注释1 天前
【Unity UGUI 交互组件——Dropdown(TMP版本)(10)】
java·unity·交互
Magnum Lehar1 天前
wpf 3d游戏引擎的PrimitiveMesh.h和ToolsCommon.h
3d·游戏引擎
SmalBox1 天前
【光照】[光照模型]是什么?以UnityURP为例
unity·渲染
死也不注释2 天前
【Unity UGUI 交互组件——Scrollbar(8)】
unity·游戏引擎·交互
九章云极AladdinEdu2 天前
绿色算力技术栈:AI集群功耗建模与动态调频系统
人工智能·pytorch·深度学习·unity·游戏引擎·transformer·gpu算力
伽蓝_游戏2 天前
UGUI源码剖析(15):Slider的运行时逻辑与编辑器实现
游戏·ui·unity·性能优化·c#·游戏引擎·.net
lrh30252 天前
Custom SRP - Complex Maps
unity·srp·render pipeline
m0_497214153 天前
unity中通过拖拽,自定义scroll view中子物体顺序
unity·游戏引擎