H3XED

How to Handle Android Back Button Event in C# Unity Game Engine (Quit, Pause, Etc)

Nov 23, 2017   Programming   Nick Vogt   Comments
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).

Here's sample code that quits the app when the back button is pressed:

void Update()
{
    // Make sure user is on Android platform
    if (Application.platform == RuntimePlatform.Android) {
        
        // Check if Back was pressed this frame
        if (Input.GetKeyDown(KeyCode.Escape) {
            
            // Quit the application
            Application.Quit();
        }
    }
}
Share This Post
Facebook Twitter

Comments (0)

Share This Post
Facebook Twitter
H3XED © Nick Vogt   RSS   Policies   Facebook   Twitter   Google+