Audio & the AudioMixer
Unity audio has three parts: AudioClips (the sound files), AudioSources (components that play clips, in 2D or positioned in 3D) and one AudioListener (usually on the camera: the "ears"). An AudioMixer asset sits in between and routes sources into groups (Music, SFX, Voice) with volume, effects and snapshots.
Playing sounds
using UnityEngine;
public class Footsteps : MonoBehaviour
{
[SerializeField] AudioSource source; // Spatial Blend = 1 for 3D sound, Output = the SFX mixer group
[SerializeField] AudioClip[] steps;
[SerializeField] Vector2 pitchRange = new(0.9f, 1.1f);
int last = -1;
public void PlayStep() // called from an animation event
{
int i;
do { i = Random.Range(0, steps.Length); } while (steps.Length > 1 && i == last); // no immediate repeats
last = i;
source.pitch = Random.Range(pitchRange.x, pitchRange.y);
source.PlayOneShot(steps[i], 0.8f); // overlapping one-shots on one source
}
}source.Play() plays the source's assigned clip and restarts it if already playing; PlayOneShot layers clips on one source (but they share its pitch and position). AudioSource.PlayClipAtPoint creates a temporary GameObject per call; fine for rare sounds, wasteful for frequent ones. Unity 6 also offers the Audio Random Container asset, which picks and varies clips without code.
Import settings matter for memory: long music clips should use Streaming load type; short frequent SFX Decompress On Load; medium ones Compressed In Memory. Check them in each clip's Inspector.
The AudioMixer
- Create > Audio Mixer (in the Project window), open it (Window > Audio > Audio Mixer).
- Add groups under Master:
Music,SFX,UI. Route each AudioSource's Output to a group. - Right-click a group's Volume in the Inspector and choose Expose … to script. In the mixer's Exposed Parameters list, rename it
MusicVolume. - Add effects per group (low-pass for "underwater", duck SFX while dialogue plays) and snapshots (
Gameplay,Paused) to blend between.
Mixer volumes are in decibels: 0 dB is full volume, -80 dB is silence, and loudness is logarithmic. A settings slider runs from 0 to 1, so convert:
using UnityEngine;
using UnityEngine.Audio;
public class VolumeSettings : MonoBehaviour
{
[SerializeField] AudioMixer mixer;
public void SetMusic(float slider01) // hooked to a UI slider (0..1)
{
float dB = slider01 > 0.0001f ? Mathf.Log10(slider01) * 20f : -80f;
mixer.SetFloat("MusicVolume", dB);
PlayerPrefs.SetFloat("music", slider01);
}
public void Pause(bool paused) =>
mixer.FindSnapshot(paused ? "Paused" : "Gameplay").TransitionTo(0.3f);
}The model shows why the conversion matters. Setting decibels linearly from the slider makes the bottom half of the slider nearly silent and the top half barely change:
using System;
Console.WriteLine("slider | linear->dB (wrong) | log10 (right) | perceived amplitude");
foreach (double s in new[] { 1.0, 0.75, 0.5, 0.25, 0.1, 0.01, 0.0 })
{
double wrong = -80 + 80 * s; // naive: map 0..1 to -80..0 dB
double right = s > 0.0001 ? Math.Log10(s) * 20 : -80;
double amplitude = Math.Pow(10, right / 20); // back to a 0..1 gain
Console.WriteLine(quot;{s,6:F2} | {wrong,15:F1} dB | {right,9:F1} dB | {amplitude,6:F2}");
}
Console.WriteLine(quot;naive slider at 0.5 gives gain {Math.Pow(10, -40 / 20.0):F2} (1% of full)");slider | linear->dB (wrong) | log10 (right) | perceived amplitude
1.00 | 0.0 dB | 0.0 dB | 1.00
0.75 | -20.0 dB | -2.5 dB | 0.75
0.50 | -40.0 dB | -6.0 dB | 0.50
0.25 | -60.0 dB | -12.0 dB | 0.25
0.10 | -72.0 dB | -20.0 dB | 0.10
0.01 | -79.2 dB | -40.0 dB | 0.01
0.00 | -80.0 dB | -80.0 dB | 0.00
naive slider at 0.5 gives gain 0.01 (1% of full)
With the log conversion, the slider value is the gain, which feels natural. (For an even more perceptual feel some games square the slider first.)
Tip: Restore saved volumes in
Start, notAwake:AudioMixer.SetFloatcalls made inAwakecan be overridden as the mixer initializes.
3D sound
Set Spatial Blend to 1 on the AudioSource, choose a Volume Rolloff (Logarithmic is realistic; a custom curve gives designers control) and Min/Max Distance. Only one active AudioListener may exist; with split-screen, keep one and position sound relative to it.
For large projects with adaptive music and many sound designers, middleware such as FMOD or Wwise replaces most of Unity's audio pipeline; both have Unity integrations (check their licences and free tiers).
Test yourself
An AudioMixer volume parameter is set to -6 dB. Roughly what gain is that?
- 0.06
- 0.5
- 0.94
- -6
Which load type suits a 4-minute background music track?
- Decompress On Load
- Streaming
- Compressed In Memory with preload
- None, music can't be imported
Why use PlayOneShot for rapid footsteps instead of Play?
- It is 3D only
- It lets several clips overlap on one source; Play restarts the source's clip
- It loops automatically
- It ignores the mixer