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

Asked

Viewed 10,363 times

3

I’m learning Unity and would like to know what the command to exit the game, ie close the application and also to know this command as I apply to press the back button of mobile Android execute such a command.

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();

    }
}
  • confirm if this is the condition mentioned in your code is the back button of the device ?

  • I have no android to test here, but from what I remember and what I searched on the internet, yes, that’s right.

  • ok I’ll test now

  • is that it worked out well worth beast

3

In the Unity, there are parameters like the Escape which are customized for the software to be really universal. You can see all the keys supported on Unity documentation.

What would Escape be?

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

https://es.wikipedia.org/wiki/Escape_(key)

Therefore, in mobile devices this key would correspond to the key voltar, whether it’s Android, Windows or iOS.

Already to make the application output in Unity we use Application.Quit();, that according to its own documentation has the objective: "Close 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.