How to quit a game in Unity

Every game needs to pause and quit. Unity has a simple one liner to make the game quit in any device. But you have to save your game before doing so. Efficient quitting will help player resume their game and keep the engagement rate higher. In this tutorial, you will learn how to quit your game in different devices and the different methods available in Unity.

Best practices to Quit the game

  1. Game should never quit directly from a game play session. If this option is available then there might be accidental quit during gameplay, which will cause a bad experience for the player.
  2. Add a confirmation popup before you quit. You can also pause your game and add a quit button to the pause menu.
  3. Save player data and level data required to resume game before you quit.
  4. The quit button should be in a place where the player cannot press it accidentally.
Quit a game in Unity

Quit your game in Unity

It’s very simple to quit a game in Unity. You just need to call the method

Application.Quit();

let’s see how you can do this in different scenarios

Using a Button to quit the game

Create a public function which calls Application.Quit as shown in the code sample below.

using UnityEngine;

public class Exit_script : MonoBehaviour
{
    public void button_exit()
    {
    
        Application.Quit();

    }
}

Create a Button in Unity hierarchy window and name it as exit button. Add an on click event to the button.

Add the script above to an empty game object and drag and drop it to the button event. Select the public function you created in the list.

Add exit function to button's Onclick event

Using a Confirmation popup

First, we need to build a UI panel that will work as a confirmation popup while the player tries to quit. Here is how to do it.

  • Go to scene window and create a new panel by clicking Create>UI>Panel.
  • Resize the panel to look like a popup. You can set the color based on your choice.
  • Add two buttons to the panel for confirmation. One to quit the game and the other to resume the game.
  • Set the panel as deactivated in the scene.
Confirmation panel deactivated in the Hierarchy window

Next let’s create a button and a script to call the confirmation panel on button press.

  • Create a new button to activate the panel.
  • Create an empty object and add the script below. Let’s call it Exit_manager.
using UnityEngine;

public class Exit_script : MonoBehaviour
{
    public GameObject confirmation_panel;
    public void button_exit()
    {
    
        Application.Quit();

    }
    public void enable_confirmation()
    {
        confirmation_panel.SetActive(true);
    }
    public void disable_confirmation()
    {
        confirmation_panel.SetActive(false);
    }
}

We have created three public functions. One to enable panel, one to disable the panel and one to quit the application.

game quit scene view

Add the respective functions to the on click event of the respective buttons.

1. enable_confirmation() function to Exit button.

2. disable_confirmation() function to No button.

3. button_exit() function to Yes button.

Drag and drop the confirmation panel to the Exit_manager.

inspector view

Now you are all set. Once you press the Exit button on the screen the confirmation popup will be displayed. if you press yes, the application will quit. If you press no the game will resume.

Enable confirmation with escape key

In case of windows, you can enable the exit pop when the escape key is pressed. To do that you can call the enable_confirmation() function from an Update when the escape key is pressed.

This code will also work with the back button on Android.

using UnityEngine;

public class escape_button : MonoBehaviour
{
    Exit_script exi;
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            exi.enable_confirmation();
        }
    }
}

Exit play mode in Unity editor

Appication.Quit does not work inside the Unity editor. It is designed to work only on game builds. So, if you want to quit the play mode when the exit button is pressed you need to add the namespace using UnityEditor and set the EditorApplication.isPlaying to false. Here is a code sample.

using UnityEngine;
using UnityEditor;

public class Exit_Editor : MonoBehaviour
{
    
    public void button_exit()
    {
    

        EditorApplication.isPlaying=false;

    }
}

If you want a common code for both editor and game builds then you can use Preprocessor directives to check whether the game is running on Unity Editor interface or on an actual device. Preprocessor directive give instructions to compiler to process the information before compiling the code.

using UnityEngine;
using UnityEditor;

public class Exit_Editor : MonoBehaviour
{
    
    public void button_exit()
    {
        #if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
        #endif
        Application.Quit();

    }
}

How to quit a game with back button in Mobile devices

The earlier code using the escape button will work with back button of Android devices. Unity also has a simple one liner to quit the game when the back button is pressed.

Input.backButtonLeavesApp = true;

By default, this value is set to false. You can set it to true in any script when the game starts. This function works on Android and Windows.

It will terminate the application on Android and suspend the application on Windows.

You can check out our blog post on implementing back button in mobile devices for more details.

How to Cancel Game Quit in Unity

When Application is asked to quit, Unity event handler raises an event to notify application quit. You can subscribe to this event and use it for other purposes like auto saving a game or checking if the player actually wants to quit.

The method that we use to subscribe to the event needs to return a Boolean. If you return true the application will continue and quit. If you return false the application quit process will be cancelled.

Let’s dig into some code to understand more

using UnityEngine;

public class Cancel_QuitExample : MonoBehaviour
{
    void Awake()
    {
        Application.wantsToQuit += StopQuit;//Subscribing to the wants to quit event
    }

    static bool StopQuit()
    {
        return false;//If you return True the application will Quit
    }  
    
}

The StopQuit() will return false to stop the application from quitting. You can add your own logic to do things before quitting the game.

Hope this article covers all aspects of how to quit a game. If you have any other questions, feel free to leave it in the comment box below.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from VionixStudio

Subscribe now to keep reading and get access to the full archive.

Continue reading