I've been using this singleton for a couple of XR projects based in Unity, figured I should keep updating it here. I referenced a few existing audio manager scripts out there, particularly this logic from CodeMonkey.
-
Setup:
- Create an empty AudioManager GameObject and attach the script to it.
- Define sound clips in the
SoundClips
enum list. - Create an Audio Mixer according to your sounds and the way you like to setup your mix.
- In the inspector, populate the
audioClipArray
withAudioClipToPlay
instances, associating each sound clip with its corresponding audio clip and audio mixer group.
-
Play Sounds:
- Call
PlaySound3D
orPlaySound2D
in a script attached to a GameObject to play 3D or 2D sounds, respectively. - Provide the appropriate parameters such as sound clip type, pitch, volume, spatial blend, spread, and loop flag.
- Call
-
Stop Sounds:
- Call
StopSound
to stop a currently playing sound based on the specified sound clip.
- Call
-
Fade In/Out:
- Use the
FadeInSound
andFadeOutSound
methods for implementing sound fading.
- Use the
// Play a 3D sound at the object's position with default settings
// Note - "ButtonClick" will be a sound from the SoundClips enum
AudioManager.PlaySound3D(AudioManager.SoundClips.ButtonClick, playerTransform, 1f, 1f, 0f, false, true, 1f);
// Play a 2D looping sound with custom pitch and volume
AudioManager.PlaySound2D(AudioManager.SoundClips.RoomAmbience, 0.8f, 0.5f, true, false, 0f);
// Stop the RoomAmbience sound
AudioManager.StopSound(AudioManager.SoundClips.RoomAmbience);
// Fade Out sound
StartCoroutine(FadeOutSound(AudioManager.SoundClips.RoomAmbience, 3f));
I know this can be optimized even further, probably by using object pooling instead of creating a new GameObject for each sound, if you have a lot of sounds playing frequently. I'll try adding this in the future either as a separate script or integrated into the Audio Manager.