Used to route audio to specific channels in an AudioMixer, allowing for grouped processing and effects.
Commonly used for managing different categories of sounds, like music, sound effects (SFX), and dialogue with shared volume controls and audio effect settings.
AudioMixerGroups can be nested for more complex audio hierarchies. Typically, a root "Master" group is used that contains multiple specific groups like "Music", "SFX" and "Dialogue".
Note: Create your AudioMixerGroups in the editor before referencing them in code.
Additional resources: AudioMixer, AudioSource, AudioListener
using UnityEngine;
using UnityEngine.Audio;
// Example of a class that manages audio in a game
public class AudioManager : MonoBehaviour
{
// References to the AudioMixer and AudioSources
public AudioMixer audioMixer;
public AudioSource musicSource;
public AudioSource sfxSource;
// Handles to the AudioMixerGroups
private AudioMixerGroup musicGroup;
private AudioMixerGroup sfxGroup;
private void Start()
{
// Find the AudioMixerGroups and set the output of the AudioSources to them
musicGroup = audioMixer.FindMatchingGroups("Music")[0];
musicSource.outputAudioMixerGroup = musicGroup;
sfxGroup = audioMixer.FindMatchingGroups("SFX")[0];
sfxSource.outputAudioMixerGroup = sfxGroup;
SetMusicVolume(0.8f);
SetSFXVolume(1.0f);
}
public void SetMusicVolume(float volume)
{
// Set the volume of the Music group in the AudioMixer
// Volume needs to be exposed in the AudioMixer
audioMixer.SetFloat("MusicVolume", volume);
}
public void SetSFXVolume(float volume)
{
// Set the volume of the SFX group in the AudioMixer
// Volume needs to be exposed in the AudioMixer
audioMixer.SetFloat("SFXVolume", volume);
}
}