Project 1: Space Shooter
Quitting with ESCape is a feature
How to quit your Unity game
So you had enough of playing for the day and want to quit a test run of your game’s desktop OS version. But you forgot to build your game in windowed mode, and now you are wondering how to quit the game.
To not repeat this scenario in the near future, I will show you how to make the escape key to quit your game. Since it is a feature in Unity and not something built in.
In all honesty, it’s really simple. All I had to do was to add these two lines of code in my game manager, four if you count the braces, and I could quit my full screen game without any hassle.
In the first line GetKeyDown is nothing new, but inside the if-statement I use the Application class. With this class I can access all sorts of information about the game and use different methods that are only applicable during run-time.
Of these methods, the one that can make the game quit properly is called Quit. This will not work when testing the game inside Unity editor. So if you do implement it, to properly test the functionality you have to build your game first.