A
A
Ark Tarusov2016-07-23 03:06:43
Android
Ark Tarusov, 2016-07-23 03:06:43

Android Back button implementation in Unity3d?

Good day!)
Due to my inexperience in game development, I encountered the problem that in an application made on a unit by default, the android hardware buttons do not work -_-
I made the button itself how to track it.

using UnityEngine;

public class BackButton : MonoBehaviour {

    void OnGUI()
    {
        if (Input.GetKeyUp(KeyCode.Escape))
        {
            Application.Quit();
        }
    }
}

Google advised to use Application.Quit(); to exit the application. but I have zero effect -_- Just in case, I checked whether pressing the button is caught by inserting the loading of the scene into execution. The button catches, everything is in order.
Question
How to implement the exit to the stage back with the subsequent exit from the application by double clicking on the "Back" with the display of the system message "to exit the application, double-click" back ""?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2016-07-23
@kreo_OL

try like this:

using UnityEngine;

public class BackButton : MonoBehaviour {
function Update () 
{
    //if running on Android, check for Menu/Home and exit
        if (Application.platform == RuntimePlatform.Android)
        {
            if (Input.GetKey(KeyCode.Home) || Input.GetKey(KeyCode.Escape) || Input.GetKey(KeyCode.Menu))
            {
                Application.Quit();
                return;
            }
        }
} 
}

R
Rou1997, 2016-07-23
@Rou1997

AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
activity.Call<bool>("moveTaskToBack", true);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question