How to Implement a Unity Game to Continue Having a Process Working Even With the Game Being Closed

Quitting the game in Unity can be a very simple task.

Which is great, because just about any game or application you make is probably going to need a way to exit at some point.

However, while the basic method of quitting a game in Unity is very simple, there 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 different platforms handle the closing of an application.

But don't worry, because in this article you'll learn everything you need to know about quitting a game in Unity, step by step.

So, how do you quit a game in Unity?

You can quit a game in Unity by calling the Application.Quit function, which will close a running application. However, while this works to end a built application, Application Quit is ignored when running the game in Play Mode in the editor.

In this article, I'll show you how to properly use the Quit function, how to exit play mode from a script when working in the editor, and how you can run specific code when the application tries to close.

Let's start with the basic 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 function, which will close the running application.

Like this:

          Application.Quit();        

This works in built versions of the game, and can be used to give 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 example, the Escape key, simply check for the Key Down condition in Update and trigger the quit function when it's pressed.

Like this:

          public class QuitManager : MonoBehaviour {     void Update()     {         if (Input.GetKeyDown(KeyCode.Escape))         {             Application.Quit();         }     } }        

Which will quit the game as soon as the Escape key is pressed.

However, while exiting when a key is pressed can be useful, chances are that you'll be triggering the Quit function from a menu button instead.

So how can you connect the quit function to a button?

How to quit the game using a button

To trigger the quit function from a menu button, you'll need to place it inside a public function.

Like this:

          public void QuitGame() {     Application.Quit(); }        

Making the function public will allow the button to access the quit game method when it's clicked.

Next, on the Button component that you want to use to trigger the quit function, add a new On Click Event:

Add New On Click Event Unity

And then drag the script containing the quit function to the empty On Click object field:

Connect Script to a button in Unity

Then, to trigger the quit function 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 sure it's a public method.

If you can't find the function in this list, go back to the script to make sure that it's definitely public.

Then, when you click the button, the game will close.

However… keep in mind, that this will only work from a built application.

In the Unity editor, the Application Quit function is ignored so pressing this button in Play Mode won't do anything.

So, how can you add a quit function to your game that also works in the editor?

How to exit Play Mode from a script in Unity

While the Application Quit function works in a built game to close the application, it doesn't do anything in the Unity editor.

The function is ignored, so trying to use the quit function to exit Play Mode, doesn't work.

However, it is still possible to exit out of Play Mode from a script in Unity, you just need to use a different function to do it.

This works by setting the Is Playing property of the Editor Application class to false.

Like this:

          UnityEditor.EditorApplication.isPlaying = false;        

This has the same 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 first, as well as a shortcut for leaving Play Mode quickly.

For example, when a key is pressed.

Like this:

          public class QuitManager : MonoBehaviour {     void Update()     {         if (Input.GetKeyDown(KeyCode.Escape))         {             QuitGame();         }     }     public void QuitGame()     {         UnityEditor.EditorApplication.isPlaying = false;     } }        

Exiting Play Mode can be set up to work in the same way as the standard quit function, by placing it inside a method that's called when a key or a button is pressed

There's just one problem.

While setting Is Playing to false works to exit Play Mode in the editor, it won't work in the built game.

In fact, because the Unity Editor class can't be included in the built application, you won't even be able to build the game when using this code.

And if you try, you'll get an error.

Screenshot of the EditorApplication namespace error

Trying to include the Unity Editor class in a build will cause an error.

So how can you exit Play Mode from a script, without running into errors when you try to build your game?

How to run different code in the Unity Editor (using Preprocessor Directives)

Preprocessor Directives are used to conditionally compile code in your game.

In Unity, you can use preprocessor directives to run different code depending on certain conditions.

For example 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 could ignore blocks of code that relate to the old Input Manager.

In this case, preprocessor directives are useful for checking if the game is being run in the editor or not.

This means that you can use a different quit method for when you're working in the editor, and it will be automatically excluded when the time comes to build your game.

Like this:

          public void QuitGame()     {         #if UNITY_EDITOR         UnityEditor.EditorApplication.isPlaying = false;         

0 Response to "How to Implement a Unity Game to Continue Having a Process Working Even With the Game Being Closed"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel