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.
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.
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();
}
}
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
orEscape
e is generally used to generate the escape characterASCII
, whose number is27
. 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
.
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