What is the command to close or exit the game created in Unity?

3

I'm learning Unity and would like to know the command to quit the game, ie close the application and also knowing this command as I apply when pressing the back button of the mobile Android execute this command.

    
asked by anonymous 30.12.2015 / 18:35

2 answers

5

In C # you can do the following:

using UnityEngine;
using System.Collections;

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

    }
}
    
30.12.2015 / 18:40
3

In Unity , there are parameters like Escape that are custom for the software to be truly universal. You can see all supported keys in the Unity Documentation .

What would the Escape be?

  

The key (Esc) was created by Bob Bemer. It is labeled Esc or    Escape e is generally used to generate the escape character    ASCII , whose number is 27 . This character is generally used for   generate an escape sequence. It is typically located in the corner   top left corner of the keyboard. Its use is continuous for small boxes   of the Microsoft Windows dialog, which is equivalent to responses such as: Não, Remover, Exit, Cancelar ou Abortar .

     

link

So, in mobile devices this key would correspond to the voltar key, be it Android, Windows or iOS.

Now to exit the application on Unity we use Application.Quit(); , which according to its own documentation has the purpose: " Terminate the game application. ".

Now in code how can we do this?

C #

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Update() {
        if (Input.GetKey("escape"))
            Application.Quit();

    }
}

JavaScript:

function Update () {
    if (Input.GetKey ("escape")) {
        Application.Quit();
    }
}

References: Unity 3D Documentation

    
30.12.2015 / 19:07