Unity点击按钮结束运行和退出游戏

省流:完整代码在底部

1、定义方法Exit

public void Exit()
{
#if UNITY_EDITOR
    UnityEditor.EditorApplication.isPlaying=false;//在Unity编译器中结束运行
#else
    Application.Quit();//在可执行程序中结束运行
#endif
}

2、创建按钮,将脚本挂载到按钮上,添加按钮单击事件

完整代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Menu_ExitGame : MonoBehaviour
{
    public void Exit()
    {
#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying=false;
#else
        Application.Quit();
#endif
    }
}

猜你喜欢

转载自blog.csdn.net/clown_sam/article/details/132701217
今日推荐