How to Handle Android Back Button Event in C# Unity Game Engine (Quit, Pause, Etc)
Nov 23, 2017ProgrammingComments (7)
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:
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();
}
}
}