Full Unity 2D Game Tutorial 2019 – Preferences


Sharing is caring!

Full Unity 2D Game Tutorial 2019 – Preferences

In this section of the tutorial we will be adding the preferences code to allow us to update the music and effects volume. These preferences can be applied to any item you want to store like volume, username, fullscreen/windowed screensize, key/controller configurations etc. For more information check out the unity documentation for PlayerPrefs.

Full Unity 2D Game Tutorial 2019 – Preferences UI

In the last part of this tutorial we got up to adding a volume slider for the music. Now that we already have a slider we can just duplicate the music one and rename it to EffectsVolumeSlider and change the text to Effects Volume. You should have something like this:

Preferences Volume and Effects

Our sliders have an On Value Changed event just like the buttons On Click event. We will use this to trigger a method in one of our scripts. But first we have to make that script. So here’s the code:

Code Notes:
PlayerPrefs can be used to to get or set a float, int or string from the preferences (stored in your registry on windows)
Using UnityEngine.UI because we access slider’s values.

This code is pretty self explanatory so we will move onto setting the sliders to run these methods when changed.

Full Unity 2D Game Tutorial 2019 – Preferences Slider On Value Changed

Firstly lets add the OptionsMenu script to our OptionsMenu object. This will allows us to link the sliders to the references we have in our code. Link the sliders and rename them now if you haven’t already so we know which slider is which.

Link Slider to Options Menu

Assign the Options menu to the slider Events and select the updateMusicVolume method for the Music volume event and the updateEffectsVolume method for the Effects volume.

Music Slider Events

Effects Slider Events

If you play the game now in unity you should be able to adjust the effects and volume sliders(this wont change the volumes yet) and then quit and play the game again. The values should match the values you set them at from the previous play. This means they have been saved in your preferences. You can further double check the values in windows by opening Regedit (winkey+r > regedit [enter]) and going to Computer\HKEY_CURRENT_USER\Software\Unity\UnityEditor\DefaultCompany\projectname. Here we can see in my registry we have both the music and effects volume saved.

regedit saved

We now need to update the AudioManager to tell it that the volumes have changes  and all the music and effect’s volumes should be changed.

Full Unity 2D Game Tutorial 2019 – Update Audio Manager

There is two ways of doing this. We could add a new event to the OnValueChanged and add the Audio manager and select the method for updating the volume. This would mean we have to set the Audio Manager to the slider in Unity. The problem with this is we already have the AudioManager set as a singleton so why don’t we use this.

Code Notes:
We used the singleton pattern to access the audio manager and update the volumes.

Now, when you play the game you should be able to adjust the volume and hear the volume change for the music and effects.

Full Unity 2D Game Tutorial 2019 – Bonus Content

In some of my games I require a username for things like a leader board or an online game where names should be unique. People may skip the option to set their user name in the options panel so I usually have a list of first and last names that can be used to generate a random name. The first name list is usually something like an emotion or an adverb and the second is an animal. This allows me to come up with some fun name for people like angry duck or dangerous fly.  We are going to implement this in our game.

First we need an input box that will allow the user to change their name if they don’t like the randomly generated one.

TextMeshPro Input

Remove the Placeholder text object as we will not need it because we will automatically populate it with code. We set the text in the TmpInputUname object and not the text object because TextMeshPro takes care of updating the text field for us. Play around with the colours and size until you get something you like. You should be familiar with all this now if you’ve been folllowing this tutorial from the beginning.

Now lets update our code:

Code Notes:
We check if the username is null or blank and create a new one if it is
The username is saved once it is created.
The createUname loads text assets(which we will add next) and then generate a random name from the 2 lists.

As you may have seen we use a couple of TextAssets. A TextAsset is a plain old text file stored inside our unity assets folder which we can load and get data from. Our code relies on these files being here so we will need to create a Rsources folder and put these two files inside. The first file here is the fname.txt file:

The second is the lname.txt file:

Place these 2 text files in your Resources folder and press play. You will be granted a random name when you first start. This name will be saved in your preferences. Hehe! I got Fearless-cow. By using these 2 lists we are able to generate over 9000 name combinations (A wild Vegeta appears, It’s over 9000!!!!!!)


So now we have a random name but we still can’t change it because we haven’t used the text field to update out preferences when its changed. To do that we simple update the TmpInputUname event On End Edit event to call our OptionsMenu script and call the updateUname method. This will save our uname to the preferences.

On End Edit TextMeshPro Input

Now as a finishing touch let’s let the player know that this box is for their username. We will copy the Text from one of the volume sliders and paste it into our TmpInputUname. Use our Anchor preset to attach it to the top left corner and move it up a bit manually. You should end up with something like this:

Username Label

That’s it for this section of the tutorial. As usual the finished source code for this section of the tutorial is available on Github here.

Previous Part  –  Next Part

Sharing is caring!

2 Replies to “Full Unity 2D Game Tutorial 2019 – Preferences”

  1. the section to add the input field is missing

    1. Also the updateUname function does nothing (it sets the user name to the already existing user name..). it has to be the same like in void Start:
      PlayerPrefs.SetString(“username”, createUname());
      uname = PlayerPrefs.GetString(“username”);
      username.text = uname;

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.