Escaping the Game (Game Dev Day 32)
Objective: Close the .exe file of a build using the esc key.
There is no build in way to exit a program built in Unity (short of Alt+F4). Like everything else, we have to input the logic through code to give the esc key functionality.
Fortunately, it is extremely easy and can be done with minimal coding. This QoL function should find its way into all of your builds (unless you have created a menu on the esc key instead).
First let’s identify the script that should hold this command. As the Player script already has keyboard functions attached for movement, I am going add the appropriate code into the Void Update section of the script.
With this simple “if” statement, we can now close our application at the touch of a button. This logic can be used for other commands as well by replacing the “Application.Quit();” command with another.
If you create a pause menu, you can turn on your UI elements and lock the game to a specific frame to create a stagnant game state. On the UI, add some buttons with other functionality (settings menu, main menu, quit, etc). Simple QoL functions like this will keep players happy and gives a more professional look to your project.