You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Texture图片按钮关联至其他屏幕?配置按钮跳转与退出功能

实现Texture按钮的场景跳转与退出游戏功能

一、通过Inspector面板快速设置(无需编写代码)

1. 配置“playbuttonactive”跳转至gamescreen场景

  • 先确认gamescreen场景已添加到Build Settings(路径:File > Build Settings,将场景拖拽至Scenes In Build列表中)
  • 在Hierarchy面板选中playbuttonactive按钮对象
  • 找到Inspector面板的Button组件,展开On Click()事件列表
  • 点击+添加事件条目,拖拽任意空对象(或场景管理对象)到对象输入槽
  • 在方法下拉菜单选择SceneManager > LoadScene(string sceneName),在参数框输入gamescreen(需与场景名称完全一致)

2. 配置“exitbtn”退出游戏

  • 在Hierarchy面板选中exitbtn按钮对象
  • 同样展开On Click()事件列表,添加新条目
  • 拖拽对象到输入槽,选择Application > Quit()方法(注:Unity编辑器内点击不会退出,需打包后测试)

二、通过C#代码绑定事件(更灵活)

创建名为ButtonHandler的脚本,代码如下:

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class ButtonHandler : MonoBehaviour
{
    public Button playButton;
    public Button exitButton;

    void Start()
    {
        playButton.onClick.AddListener(LoadGameScene);
        exitButton.onClick.AddListener(QuitApplication);
    }

    void LoadGameScene()
    {
        SceneManager.LoadScene("gamescreen");
    }

    void QuitApplication()
    {
#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false; // 编辑器内停止播放
#else
        Application.Quit(); // 打包后退出程序
#endif
    }
}
  • 将脚本挂载到Canvas或任意空对象上
  • 在Inspector面板中,把playbuttonactive和exitbtn分别拖拽到对应的playButton、exitButton变量槽中即可

注意事项

  • 必须确保gamescreen场景已加入Build Settings的场景列表,否则场景跳转失效
  • 编辑器内测试退出功能时,依赖代码中的编辑器判断逻辑来停止播放模式

内容的提问来源于stack exchange,提问作者Sponge bob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 15:37:34