Tuesday, October 13, 2015

Tips for implementing / coding an in-game options or pause menu functionality in Unity

 

I recently implemented an in-game options menu in Unity. Mine looks something like the thing above. A surprising amount of the required functionality is already implemented in Unity, you just have to write some code to hook into it. In the cases when Unity didn't already have a static variable for a particular setting, like mouse sensitivity or menu language, then I'd implement my own static variable that worked with a specific PlayerPrefs key.

Anyway, here's a bunch of workflow / specific API calls that I found very useful when I did it...
  • Create a separate UI Canvas for your options menu, then save that whole object as your menu prefab. You can now instantiate this menu prefab into every game scene with minimal mess.
  • For in-game master volume, use AudioListener.volume
  • To pause in-game sounds, use AudioListener.pause = true; don't forget to unpause the sound if you exit the menu or change the current scene.
  • To pause in-game action, use Time.timeScale = 0f; again, don't forget to reset it if you exit the menu or change the current scene.
  • For an in-game brightness / gamma correction slider, I used a simple fullscreen shader. The built-in color correction Image Effects were kind of overkill for this, so I went with the simpler implementation by willywill here. (I've also archived the files as a public Gist here.)
  • When detecting the user's screen resolution, be careful: Screen.currentResolution is the full screen resolution, but Screen.width and height is the actual window size.
  • I would recommend letting the user change graphics quality only from the main menu, and not in the pause menu, since it's expensive to call QualitySettings.SetQualityLevel( ) sometimes.
  • You can detect the user's OS language with Application.systemLanguage, but then you're kind of stuck with using Unity's SystemLanguage enum which doesn't have many options.
  • If you are using PlayerPrefs, you can call PlayerPrefs.DeleteAll( ) to give the user the option to easily clear all their game data.
  • If you're making something for Steam, the most used Unity wrapper is probably Riley Labrecque's Steamworks.NET here.
Got any other Unity in-game menu implementation tips? Feel free to drop some wisdom in the comments. (I'm going to delete any Asset Store ads though, sorry.)