Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access mobiles back button in unity for android applications

I am building a VR application with a menu scene and many other scenes.. I would like to get back into the menu scene from any other scene when the user hits the mobiles back button (android).. What is the script for that and where should i place the script??

like image 706
Shreenath M Avatar asked Oct 09 '16 03:10

Shreenath M


People also ask

How to use back button of android in unity?

In Unity, the Android back button is treated the same as the "Escape" KeyCode. So in order to do something when the user presses the back button on their Android device, you need to check for the Escape key being pressed down and also make sure the user is on the Android platform (if you're cross-platform).

How to use back button in unity?

Input. backButtonLeavesApp=true; Add this line to the update function of your code. It will terminate the application on an android phone when the user presses the back button.


1 Answers

You use the Escape keycode to detect back button press on Android.

using UnityEngine.SceneManagement;

void Update()
{
    if (Input.GetKey(KeyCode.Escape))
    {
        SceneManager.LoadScene("Main Menu");
    }
}
like image 111
Programmer Avatar answered Sep 27 '22 23:09

Programmer