Skip to content

Audio Manager

Description

Inherits from: Singleton <T>

The Audio Manager Script controls all AudioSources used by the character, grouping them in different categories, SFx, Music and Voice.

Editor Properties

Property Function
Sfx Volume Defines the Sound Effects volume, e.g explosions, environment and weapons.
Voice Volume Defines the Voice volume, e.g character responses and CutScenes.
Music Volume Defines the Music volume.
Sfx Mixer The SFx Mixer is used to apply effects such echoing and deafness to SFx sources.
Music Mixer The Music Mixer is not affect by any king of effect, for this reason, it is perfect for all Music Sources in game.

Public Properties

Property Function
Instance Access singleton instance through this propriety.
SFxVolume Defines the Sound Effects volume, e.g explosions, environment and weapons.
VoiceVolume Defines the Voice volume, e.g character responses and CutScenes.
MusicVolume Defines the Music volume.
SFxMixer Returns the SFx AudioMixerGroup. (Read Only)
MusicMixer Returns the Music AudioMixerGroup. (Read Only)

Public Methods

Method Function
RegisterSource Register a new AudioEmitter and returns its reference.
GetSource Search and return an AudioEmitter by the given name.
Play Plays an AudioClip in the AudioEmitter if it's not being already used.
ForcePlay Forces the AudioEmitter to stop playing the current AudioClip and play immediately the requested sound.
CalculateVolumeByPercent Calculate the AudioEmitter volume by using the formula: (volume = 1 - value / startValue). Useful when you need to start playing a sound at certain point. e.g when the character vitality is below certain point, it will increase the volume as the vitality gets lower.
Stop Immediately stop playing the current AudioClip.
PlayClipAtPoint Plays an AudioClip at a given position in world space.

Example

We recommend that you use AudioManager only to register AudioEmitters. However, if necessary, we have added functions to decrease the verbosity of your code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Recommended way, more verbose.
private AudioClip jumpSound;

// It's recommended to cache the AudioEmitter and use its reference.
private AudioEmitter playerSource;

private void Start()
{
    // To avoid additional overhead, do not assign an 
    // AudioEmitter in a function called every frame.
    playerSource = AudioManager.Instance.RegisterSource("PlayerSource", transform);
}

private void Update()
{
    if (InputManager.GetButtonDown("Jump"))
    {
        // With the cached reference it's easy 
        // and fast to access AudioEmitter and play a sound.
        playerSource.Play(jumpSound, 0.5f);
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// Alternative mode, less verbose yet expensive.
private AudioClip jumpSound;

private void Start()
{
    // Registers the AudioEmitter "PlayerSource"
    // allowing you to use it by using its name.
    AudioManager.Instance.RegisterSource("PlayerSource", transform);
}

private void Update()
{
    if (InputManager.GetButtonDown("Jump"))
    {
        // Search if there is any AudioEmitter with that name, 
        // then play the sound.
        AudioManager.Instance.Play("PlayerSource", jumpSound, 0.5f);
    }
}

It is worth pointing out that the alternative method is extremely inefficient when compared to the recommended method (caching the reference at the Object initialization) because the system has to search for AudioEmitter at each execution.