Exit the game
Page update date :
Page creation date :
Verification environment
- Windows
-
- Windows 11
- Unity Editor
-
- 2021.3.3f1
- Input System Packages
-
- 1.3.0
Prerequisites for this tip
The following settings are pre-configured as a prerequisite for the explanation of these tips.
How to exit the game programmatically
To programmatically exit the game, run the following code:
UnityEngine.Application.Quit();
If you want to close the game correctly, exit the game from the menu, etc., and save the setting data before exiting.
However, the above code is only valid if you build the game and run it on its own, and nothing happens when you run it in the Unity Editor. If you want to stop your game from running in the Unity Editor, you need to run the following code:
UnityEditor.EditorApplication.isPlaying = false;
If you want it to work correctly in any environment, you can write it as follows using compilation symbols.
public void OnClickExit()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#elif UNITY_STANDALONE
UnityEngine.Application.Quit();
#endif
}