‘Press ESC to Quit’ Feature
This is just a quick article to cover how we can implement a ‘press ESC to quit’ feature within our games. Unity itself doesn’t assume you’ll want to have this as a feature, so we’ll need to implement it within one of our pre-existing scripts or add it to a new one.
Within an Update method, we can use Input.GetKeyDown to return a boolean value (true) during the frame the user starts pressing down the key we specify. As for which key we specify, we can do this in a number of ways:
Of these, I’ll admit the one I tend to use is the first of these, but I appreciate the last of these is probably going to be better for cross-platform support (as we can specify an alternative positive button, for instance on a controller).
The Application.Quit call should terminate the application on Windows, Mac, and Linux, but I believe it can also work on iOS and other devices (only it may appear to the user as though the application has crashed). This call should get ignored in the editor, so it might be best to add in a Debug.Log for now just to ensure the Input method is where it’s meant to be and working as intended.