Do I Want to Try Again or Quit?
Quitting the game in Unity can be a very simple job.
Which is great, because just nigh any game or application you lot make is probably going to demand a manner to exit at some point.
However, while the basic method of quitting a game in Unity is very unproblematic, at that place are a few extra things to consider.
Such as quitting in the editor compared to quitting from a built version of the game, how to automatically run code when the game exits and how unlike platforms handle the endmost of an awarding.
But don't worry, because in this article you'll larn everything you need to know about quitting a game in Unity, pace past pace.
So, how do you quit a game in Unity?
You can quit a game in Unity by calling the Awarding.Quit role, which will close a running application. However, while this works to end a congenital application, Application Quit is ignored when running the game in Play Mode in the editor.
In this article, I'll prove yous how to properly use the Quit office, how to go out play mode from a script when working in the editor, and how you can run specific code when the awarding tries to shut.
Let's start with the bones method of quitting a game in Unity.
How to quit the game in Unity (using Application Quit)
You can quit a game in Unity using the Application.Quit office, which will close the running application.
Like this:
Application.Quit(); This works in built versions of the game, and can be used to requite the player manual control over exiting the application.
Such as when a key is pressed for example.
How to quit when the Escape key is pressed
To trigger the quit function when a key is pressed, for case, the Escape fundamental, simply check for the Central Downward condition in Update and trigger the quit function when information technology's pressed.
Like this:
public class QuitManager : MonoBehaviour { void Update() { if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); } } } Which will quit the game as before long equally the Escape fundamental is pressed.
Nevertheless, while exiting when a key is pressed can exist useful, chances are that you'll be triggering the Quit role from a carte button instead.
And then how tin you connect the quit office to a button?
How to quit the game using a button
To trigger the quit role from a card button, you'll need to identify it inside a public part.
Similar this:
public void QuitGame() { Application.Quit(); } Making the function public will permit the button to access the quit game method when it's clicked.
Next, on the Button component that you lot want to utilise to trigger the quit function, add a new On Click Event:
And then drag the script containing the quit office to the empty On Click object field:
Then, to trigger the quit part when the button is pressed, select the quit script and then the quit function from the drop-down menu:
If you don't see the quit game function in the list, make certain information technology's a public method.
If you tin't find the function in this list, go dorsum to the script to make sure that information technology'due south definitely public.
And then, when you click the button, the game will close.
However… keep in mind, that this will merely work from a built application.
In the Unity editor, the Application Quit office is ignored then pressing this button in Play Mode won't practice annihilation.
So, how can you add a quit office to your game that also works in the editor?
How to exit Play Manner from a script in Unity
While the Awarding Quit function works in a built game to close the application, information technology doesn't practise annihilation in the Unity editor.
The function is ignored, so trying to utilise the quit function to leave Play Manner, doesn't work.
Even so, it is still possible to leave out of Play Mode from a script in Unity, yous simply demand to use a dissimilar function to do it.
This works past setting the Is Playing property of the Editor Application grade to false.
Similar this:
UnityEditor.EditorApplication.isPlaying = false; This has the aforementioned effect as quitting the game using Application Quit, except that it works in the editor.
This can be useful for testing the game quitting process without building the game commencement, as well equally a shortcut for leaving Play Style quickly.
For example, when a key is pressed.
Like this:
public course QuitManager : MonoBehaviour { void Update() { if (Input.GetKeyDown(KeyCode.Escape)) { QuitGame(); } } public void QuitGame() { UnityEditor.EditorApplication.isPlaying = false; } } Exiting Play Style can be fix to work in the same way equally the standard quit function, by placing it inside a method that's called when a cardinal or a button is pressed
There's just i problem.
While setting Is Playing to false works to exit Play Way in the editor, information technology won't work in the built game.
In fact, considering the Unity Editor class can't be included in the congenital awarding, yous won't even exist able to build the game when using this lawmaking.
And if you endeavour, you'll get an error.
Trying to include the Unity Editor class in a build will crusade an mistake.
And then how tin you go out Play Mode from a script, without running into errors when you try to build your game?
How to run unlike code in the Unity Editor (using Preprocessor Directives)
Preprocessor Directives are used to conditionally compile lawmaking in your game.
In Unity, y'all can employ preprocessor directives to run different lawmaking depending on certain weather condition.
For case you could run different code depending on the platform the game is running on.
Or, if you've switched to the new Input System in Unity, you lot could ignore blocks of lawmaking that relate to the old Input Manager.
In this instance, preprocessor directives are useful for checking if the game is being run in the editor or not.
This means that you lot can use a different quit method for when you're working in the editor, and it volition be automatically excluded when the time comes to build your game.
Similar this:
public void QuitGame() { #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = fake;
0 Response to "Do I Want to Try Again or Quit?"
Post a Comment