Click the Play button again, or press Ctrl+P (Cmd+P on Mac). From code, use EditorApplication.isPlaying = false in the editor and Application.Quit() in a build.
using UnityEngine;
public class QuitOnKey : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
}
}Application.Quit()does nothing in the editor, which is why the#if UNITY_EDITORbranch is needed.- Changes made to objects while in Play mode are discarded when you exit. To keep them, use the component's context menu: Copy Component, then Paste Component Values after stopping.
- Enable Edit > Preferences > Colors to tint the editor in Play mode so you notice when you are still playing.
