Implementing "Back" button functionality in Unity


I’m just wondering if there are any scripts for coding the use of the BACK button on android phones. I just want the user to hit the back button and have it end the game when this button is pressed, and not simply hide the game and return to the game in the same position as the user left it. Also, if there is such a script available, where do I attach it to? It is not an element of the game like say, a sphere, it is like the entire system.

Thank you for your help

You can’t directly handle the home button, but any MonoBehaviour will get a call to OnApplicationPause() when it is pressed.

I believe the back button is caught by KeyCode.Escape in Android builds, like so:

if (Input.GetKeyDown(KeyCode.Escape)) Debug.Log("User pressed back");