应分为两种情况:
使用Unity调试时UnityEditor.EditorApplication.isPlaying = false; 1 打包发布后
Application.Quit(); 1
完整代码如下:
#if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; #else Application.Quit(); #endif 12345
实际用例代码如下:
using UnityEngine; using UnityEngine.SceneManagement; public class GameMenu : MonoBehaviour { public void OnStartGame() { SceneManager.LoadScene(1); } public void OnExitGame() { #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; #else Application.Quit(); #endif } }
123456789101112131415161718