Full Unity 2D Game Tutorial 2019 – Audio Manager

Sound File

Sharing is caring!

Full Unity 2D Game Tutorial 2019 – Audio Manager

Sounds and music are an important part of a game that can help emphasise the theme of your game and take it to a higher level. In this part of the tutorial we will be adding an Audio Manager which will control what sounds will play and loop through the playlist of music. This Audio manager was adapted from the tutorial on Brackeys youtube channel. He has a great set of tutorial covering many aspects of Unity game development. Check out his web page brackeys.com too.

Full Unity 2D Game Tutorial 2019 – Where to get Sound effects and Music from?

If like me you haven’t been blessed with the skills to make cool music and audio effects you can still find a lot of effects and music free that you can use within your game. The first of those is freesound.org.  The freesound website has a huge range of sound which you can use in your game. Just make sure to check the licence for the sound effect your using as many of them just require you to give appropriate credit and a link to the license.

The Second site is freemusicarchive.org, again this site has a huge range of Music and many of them are available to use in your game. Remember to check the license for each song you use and to give credit even if it isn’t required. People have worked hard to produce something and they deserve to be recognised for it.

For this section of the tutorial we will be adding 3 songs to a playlist and 1 sound effect for the bullets. Use these sites to find yourself some music and effects. In our game the bullets fire fast and there is a lot of them so try to find something that won’t make the player want to mute the game as soon as they fire.

Full Unity 2D Game Tutorial 2019 – Audio Manager Sound Code

The first thing we will do is create a class that will store all the details about our music/effect. It will hold things like the volume, the actual music clip, the source etc.

Code Notes:
Since we are not using the monobehaviour and we want to see the variables in the Unity editor we add [System.Serializable]

Full Unity 2D Game Tutorial 2019 – Audio Manager Code

Full Unity 2D Game Tutorial 2019 – Audio Manager Code Details

Lets start with the tough bit. The audio manager instance. This is a variable that will store only the first AudioManager we create, and any other AudioManagers that get created will be destroyed. This coupled with the DontDestroyOnLoad(gameObject); means the AudioMager will persist through all of our scenes making it available to all our scenes. This is what is known as a Singleton(not a true singleton but close enough for us). For more information on this pattern check it out on wikipedia.

Now we have that out of the way. In our Audio Manager we have the sounds and playlist Arrays to store all our music and sounds. We have an index which is used to see which of our playlist songs is playing and it doubles as a flag to check if the playlist is playing. We also have a method for creating audio sources. Music and effects need an audio source. It’s the place in the game world where the sound is played from.

We have also created PlaySound and PlayMusic so we can trigger the playing of sounds and music. There is a StopMusic but no StopEffect because we will let the effects control when they should stop.

The getSongName method will be used by our UI to display what music is currently playing and the MusicVolumeChnaged and effectsVolumeChanged will be used to update our audio sources volume when we implement the UI and preferences.

Another thing you may have noticed is the PlayerPrefs.Getxxx methods. Even though we haven’t implemented any preferences they have a default value attached so they will always get a value even if we haven’t added a way for the user to change them yet.

Full Unity 2D Game Tutorial 2019 – Adding Audio Files

Audio Manager Folders

We have our Sound and AudioManager scripts so move them into the scripts folder for cleanliness. Create an Audio folder and inside that create an Effects Folder and a Musics Folder. Add your effects and music by dropping them in their respective folders. You shouldn’t have any settings to change for the audio however not every audio format is supported so be aware.

Our AudioManager exists but not in our Unity world. To add it to our world we will create an empty game object and attach the script to it.

Audio Manager settingsWe can then set the size of our Sound and Music arrays. 1 for the effects since we only have 1 effect at the moment and 3 for our music. The settings will be blank when you first create the arrays. We just need to populate them with the audio by dragging the audio from our project folder into the clip field and then setting the other values by hand.

Clip settings

If you play the game now you will hear music and it will loop through the playlist of songs you added. Yay! now we work on playing the sounds for the bullet.

Full Unity 2D Game Tutorial 2019 – Sound Effects

I will show you 2 ways of playing a sound effect for your bullet. The first which is the easiest and uses our audio manager is to update the Start method in our Bullet script to this:

Now, we can see how the use of the Singleton pattern allows us to call the AudioManager from a script without having to link it inside Unity by using AudioManager.instance.

The second way to make the bullet play a sound is to bypass the AudioManager all together and add an audio source to out bullet prefab that plays on awake. So open your bullet prefab and add an AudioSource. Then set the audio clip to the sound effect you want to play. Make sure Play On Awake is checked and all your bullets will automatically play a sound when they are created.

Manual Audio Source

I advise against using this method because you will have to create some way of setting the volume for the clip based on our preferences later. You also created an AudioManager to handle this sort of thing already. It was added here so you could see how to implement sound without the Audio Manager.

That’s it for this part of the tutorial. The source, as always, is available here on Github.

Previous Part  –  Next Part

Sharing is caring!

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

  1. Hi, there’s a lot of great stuff in your tutorials. How would you go about adding multiple sounds to the same game object? I have an object which makes various chimes, but with the above I can’t figure how to play more than one sound. Currently I’m using an array of sounds on each object and playing a random clip but I would like to be able to use your system for better use.

Leave a Reply

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