SoundManager2D is a simple script that handles 2D Audio within a Unity project.
It is a static class, which means it can be placed anywhere in your project and can be called by any script without the need to attach it to a Game Object. It provides functionality for both ‘one shot’ sound effects and looping sounds.
It uses a pool of Audio Sources for playback. Sounds will persist between scene changes but can easily be stopped.
The interface is simple and is explained below.
Downloads:
SoundManager2D.cs – The script that provides all the sound playing functionality can be downloaded here:
https://www.goshdarngames.com/soundmanager2d/SoundManager2D.cs
Web Player Demo – A simple sound board that shows the functionality of the Sound Manager can be viewed here:
https://www.goshdarngames.com/soundmanager2d/
Demo Project – If you are interested in looking through the project used for the demo it can be downloaded here:
https://www.goshdarngames.com/soundmanager2d/2DSoundManager.7z
How to Use:
Place SoundManager2D.cs anywhere in your project’s Asset folder (ideally in a ‘scripts’ directory).
Now other scripts can call the functions that play sound. Your scripts will need to have access to a Unity Audio Clip object. These are created by importing a sound file (e.g. .mp3) by placing it in your project’s Asset folder. Make sure to un-tick ‘3D Sound’ in the Audio Clips properties so that it is treated as a 2D sound.
One way of accessing these Audio Clips from a game object is to create an inspector variable of the type Audio Clip e.g.
public AudioClip whizzClip;
This ‘whizzClip’ variable will now be accessible using the Unity Inspector when the script is attached to a Game Object.
For examples of how to set these variables up check out the
‘DemoGUI.cs’ file found in the Demo Project linked above.
Now that we have an Audio Clip to play we can call the following functions to play sound:
SoundManager2D.playOneShotSound(AudioClip clip, float volume = 1.0F)
This will play a ‘one shot’ sound effect. That is to say the sound will be played once and then forgotten.
Arguments:
- The first argument is the Audio Clip to play.
- The second optional argument is the volume of the clip from 0.0f to 1.0f
SoundManager2D.stopAllOneShotSounds()
This will stop all one shot sounds immediately. Due to how the one shot sounds are implemented it is not possible to stop them individually.
SoundManager2D.playLoopingSound(AudioClip clip, string soundID, int priority, float volume = 1.0F)
This method will play a looping sound. By default the system can play 32 looping sounds concurrently. An exception will be thrown if all the channels are in use and you try to play a new sound. The limit of looping sounds can be change by editing the constant NUM_LOOPING_SOURCES found in SoundManager2D.cs.
Arguments:
- The first argument is the AudioClip to play.
- The second argument is an ID to assign the loop. This ID allows us to stop the looping sounds individually even if they are playing the same Audio Clip.
- The third argument is the priority, which can be any integer in the range 1-255. The lower the value, the less likely Unity will mute the sound when all hardware audio channels are in use. If the priority is not within the range 1-255 an exception will be thrown.
- The fourth optional argument is the volume, which can be a float in the range 0.0f to 1.0f.
SoundManager2D.stopLoopingSound(string soundID)
This method will stop a looping sound as identified by soundID (The ID that was set in playLoopingSound()). An exception will be raised if the sound specified is not currently playing.
SoundManager2D.stopAllLoopingSounds()
This method will stop all looping sounds that are currently playing. Sounds will continue playing through a scene change so use this method to stop them if you want.