Get Out!

Welcome back! Today we’re going to talk about a simple but important feature. Being able to close out of our game using the Escape key. By default, our game doesn’t know that pressing the escape key should exit our game and if we are running in Full Screen mode this can lead to an issue. To accomplish our goal we want to use a method that Unity provides for us that allows us to exit the running game.

Application.Quit()
Application.Quit()

Application.Quit is ignored by the Unity Editor, so we will need to make a build and run it to test this feature.

If we were using the old input manager, we could add an if statement to our Update method in our GameManager class that checks for the press of the escape key and then call application quit if the key press is detected.

Using the old Unity Input Manager
Using the old Unity Input Manager

We have opted to go with the new Input System in our game so, even though the new system is much more flexible, setting this task up is a little more complicated than in the old system. First we need to open our Input Actions asset and add a new binding for our escape key press. I have added this binding to our UI Action Map, called it Exit Game, and assigned the Escape Key to it for the Keyboard input device.

Input Actions Screen
Input Actions Screen

Now that we have added our new Action to our action map we need to make sure that it is implemented where ever we are using the IUIActions interface. In our case that is on the GameManager Class.

Add new methods from IUIActions Interface
Add new methods from IUIActions Interface

Now that we have our OnExitGame callback method we can implement the Application.Quit functionality. I added a logging statement so that we can still see that the method fires while in the Unity Editor.

OnExitGame Callback Method
OnExitGame Callback Method

The last thing we need to do is connect our callback method with our PlayerActions component in Unity.

Add Callback Method to Player Actions Component
Add Callback Method to Player Actions Component

Next Time!

That’s it! We’ve added the ability to hit the escape key and exit our application! Next time we will start to look at enhancements I’ll be making as part of the certification portion of the course work that I’m building this game for. If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.

--

--

--

Unity Game Developer, Software Engineer, Gamer, Musician, and Father. Christopher is a creative that enjoys a challenge and loves coding.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Create our own Linux command using Bash

Rethinking your queuing strategy

After Processing Community Day

AWS Elastic Beanstalk disable journald RateLimitBurst

The Future of Moose Trax

Course Review: Udacity’s Intro to Algorithms

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Christopher West

Christopher West

Unity Game Developer, Software Engineer, Gamer, Musician, and Father. Christopher is a creative that enjoys a challenge and loves coding.

More from Medium

WGTU #7: Atmosphere & narration — with Don’t Starve

Candy Corn Women

Adding a homing missile powerup

What are Unity Skyboxes?