省流:完整代码在底部
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
}
}