How to quit the game is going to depend on the platform you are targeting.
If you create a standalone/PC/Mac build then you could have a quit button and use the following;
public void Quit()
{
Application.Quit();
}
The above would cause the running application to close, just as if you were using an application like Microsoft Word and went to File -> Exit, or, click on the X icon in the top corner.
If you just want to have a quit button which stops the game from within the editor, returning you to Unity, but, in a build as above actually closes the application, you could use the following;
public void Quit()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
The above will stop the game from playing within the Unity editor if the Quit()
method is called, or, if the game is running outside of Unity it will close.
For mobile devices, both Apple and Google will have specifications on whether you should quit the application or not. I believe, certainly in the case of iOS, that the memory is managed and apps are closed only if more memory needs to be freed up. Thus, a user would use the Home button, which effectively sends the app to the background.
Android devices have the back button and I would expect them to behave in a similar fashion.
In order to determine which platform the game is actually running on, you can use Application.platform
, this would allow you to create a reasonably generic Quit()
method, which could handle running within the Unity editor, a standalone build, Android, iOS or WebGL builds - and effectively do something different (or nothing) for each case.
Hope this helps.
See also;
Unity - Scripting API : Application.platform
Updated Tue Sep 26 2017 12:13
Can we mark this topic as solved now @Brady_Eatherton?