When the game is built for Windows Fullscreen, there is currently no way to exit the game without using the Windows Taskmanager to quit the application. So I’m going to set the escape key to quit the game. But I want to make it flexible because later on, I may choose to modify that functionality.
Searching the documentation shows a simple command that can be used to quit an application in Unity. The command that is used for his is Application.Quit().
Since there is already a GameManager script, that will be the most reasonable place to add the functionality to quit the game.
I’ll create a public method called ExitGame(), and it’ll simply have the job of running the Application.Quit() command. I’m making it a public method because I may eventually want to create a way to access the method through a UI instead of directly with the Escape key. The ExitGame() method will look like this.
To call this method, I’ll add an if statement in the Update() method to check if the Escape key has been pressed. If so, the method will be called. The code for this is below.
And that is all that is needed for basic functionality to use the Escape key to quit the game.
I hope you found this informative, and until next time I wish you luck on your coding journey.