An audio mixer helps you control and combine different sounds in your game. It lets you change volume, mute, or add effects to sounds easily.
0
0
Audio mixer in Unity
Introduction
You want to adjust the volume of background music and sound effects separately.
You need to mute all sounds quickly, like during a pause screen.
You want to add effects like echo or reverb to certain sounds.
You want to smoothly change sound levels during gameplay.
You want to organize many audio sources in your game for better control.
Syntax
Unity
using UnityEngine; using UnityEngine.Audio; public class AudioMixerExample : MonoBehaviour { public AudioMixer mixer; public void SetVolume(float volume) { mixer.SetFloat("MasterVolume", volume); } }
You need to create an AudioMixer asset in Unity and add it to your project.
Use SetFloat to change parameters like volume by name.
Examples
Sets the music volume to -10 decibels.
Unity
mixer.SetFloat("MusicVolume", -10f);
Sets the sound effects volume to normal (0 decibels).
Unity
mixer.SetFloat("SFXVolume", 0f);
Mutes all sounds by setting master volume very low.
Unity
mixer.SetFloat("MasterVolume", -80f);
Sample Program
This program controls the master volume of an AudioMixer. It starts with normal volume, and you can mute or unmute sounds by calling the methods.
Unity
using UnityEngine; using UnityEngine.Audio; public class SimpleAudioMixerControl : MonoBehaviour { public AudioMixer mixer; void Start() { // Set initial volume to normal mixer.SetFloat("MasterVolume", 0f); Debug.Log("Master volume set to 0 dB"); } public void MuteAudio() { mixer.SetFloat("MasterVolume", -80f); // Very low volume to mute Debug.Log("Audio muted"); } public void UnmuteAudio() { mixer.SetFloat("MasterVolume", 0f); // Normal volume Debug.Log("Audio unmuted"); } }
OutputSuccess
Important Notes
AudioMixer parameters use decibel values, where 0 is normal volume and negative values lower the volume.
You must link the AudioMixer asset in the Unity Editor to the script's mixer field.
Use Debug.Log to see messages in Unity's Console window for testing.
Summary
An AudioMixer helps manage and control game sounds easily.
You change sound levels by setting parameters with SetFloat.
Use it to mute, adjust volume, or add effects to sounds in your game.