The Escape Button Is a Feature

Matej Marek
Geek Culture
Published in
3 min readMay 3, 2021

Now that we have built our application on two different platforms(Desktop - Windows and WebGL), it is time to address the elephant in the room. There is no way to exit the game. The only way to do that was to either close the browser for the WebGL or use a task manager for the desktop build. While this works, it is far from ideal. With that said, it is time to implement the Exit feature!

Original animation by Alfrey Davillahttps://dribbble.com/shots/3020221-Escape-from-Work

I will be utilizing the GameManager and the ‘Escape’ key for this very feature!

All we need to do is to check for the ‘Esc’ key being pressed in the ‘Update()’ loop. When it is, we call the method for quitting the application that Unity provides us with!

I have also decided to create a public method, which we might be calling later via an event on a button in ‘Main Menu’ perhaps, or in some other way.

One thing to note is that the ‘Application.Quit()’ method will not be called in the Editor - and thus to test out this functionality, you would have to build your game.

If we wanted to have such a feature in Unity’s editor as well, we could use the ‘UnityEditor’ namespace and the ‘EditorApplication.ExitPlaymode()’ method.

With this, your game will close on any platform by hitting the ‘Escape’ key! - but… there is one more special bit of code that we can add to our script - and that is the usage of the ‘Platform dependent compilation.’ With this code, you can specify which lines of code should run on different platforms.

You could also specify that something should happen specifically for the ‘WebGL’ version, for example:

And that is it for today; we have now implemented the Escape feature - a safe and convenient way for the players to exit the game, which every game should include in some way or the other.

But that is it for now, thank you for reading and feel free to follow me for more articles - and as always, good luck and see you next time.

--

--