unity app android back button code example

Example 1: unity android back button

using UnityEngine.SceneManagement;

void Update()
{
    if (Input.GetKey(KeyCode.Escape))
    {
        SceneManager.LoadScene("Main Menu");
    }
}

Example 2: unity android back button

public class RrAndroidBackButton : MonoBehaviour {
 
    public static RrAndroidBackButton instance;
 
    void Awake() {
        if (null == instance) {
            instance = this;
            DontDestroyOnLoad(this.gameObject);
        } else {
            Destroy(gameObject);
        }
    }
 
    void FiexdUpdate () {
        if (Application.platform == RuntimePlatform.Android){
            if (Input.GetKey(KeyCode.Escape)) {
                Application.Quit();
 
                System.Diagnostics.Process.GetCurrentProcess().Kill();
 
                AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer")
                        .GetStatic<AndroidJavaObject>("currentActivity");
                activity.Call<bool>("moveTaskToBack", true);
            }
        }
    }
}

Tags:

Misc Example