File size: 3,899 Bytes
40c0886 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | # Unity Audio
Unity plays audio through `AudioSource` components that reference an
`AudioClip` asset. A single `AudioListener` in the scene is required
(usually on the Main Camera).
## AudioClip
An `AudioClip` is the audio data. Supported formats: WAV (uncompressed,
fastest), MP3/Ogg Vorbis (compressed, smaller). Unity imports them and
lets you choose compression per-platform in the import settings.
## AudioSource
```csharp
var src = gameObject.AddComponent<AudioSource>();
src.clip = jumpClip;
src.volume = 0.7f;
src.pitch = 1.0f;
src.loop = false;
src.playOnAwake = false;
src.spatialBlend = 0.0f; // 0=2D, 1=3D
src.Play();
```
Important properties:
| Property | Meaning |
|-----------------|-----------------------------------------------------------|
| `clip` | The AudioClip to play |
| `volume` | 0..1 |
| `pitch` | Default 1; >1 = higher pitch, <1 = lower |
| `loop` | Repeat when the clip ends |
| `playOnAwake` | Play automatically when the AudioSource is enabled |
| `spatialBlend` | 0=fully 2D, 1=fully 3D (position-based) |
| `minDistance` | Within this distance the sound is at full volume (3D) |
| `maxDistance` | Beyond this distance the sound is inaudible (3D) |
| `rolloffMode` | Logarithmic, Linear, or custom curve (3D falloff) |
## 3D audio
For positional sounds (footsteps, gunfire, engines), set
`spatialBlend = 1.0f`. The volume then depends on the distance between the
AudioSource and the AudioListener.
```csharp
src.spatialBlend = 1.0f;
src.rolloffMode = AudioRolloffMode.Logarithmic;
src.minDistance = 1f;
src.maxDistance = 50f;
```
> Default `maxDistance` is 500 -- way too large for most gameplay. Lower
> it to 30-50 for SFX.
## One-shot SFX
For short sound effects (pickups, gunshots, impacts) use `PlayOneShot`:
```csharp
audioSource.PlayOneShot(clip, volumeScale);
```
`PlayOneShot` can layer multiple clips on the same AudioSource without
cutting each other off.
## AudioMixer
For volume control and grouping, create an **AudioMixer** asset
(`Assets > Create > Audio Mixer`). Expose parameters and control them at
runtime:
```csharp
[SerializeField] private AudioMixer mixer;
public void SetMasterVolume(float dB) => mixer.SetFloat("MasterVolume", dB);
public void SetMusicVolume(float dB) => mixer.SetFloat("MusicVolume", dB);
public void SetSfxVolume(float dB) => mixer.SetFloat("SfxVolume", dB);
```
Convert linear 0..1 to dB:
```csharp
public static float LinearToDB(float linear) =>
linear <= 0.0001f ? -80f : Mathf.Log10(linear) * 20f;
```
## The AudioManager pattern
The generated `AudioManager.cs` is a singleton with two channels
(`sfxSource`, `musicSource`). It exposes:
```csharp
AudioManager.Instance.PlaySfx(pickupClip, 1.0f);
AudioManager.Instance.PlayMusic(menuMusic);
AudioManager.Instance.StopMusic();
AudioManager.Instance.ApplyVolumes();
```
`DontDestroyOnLoad` keeps the manager alive across scene loads so music
does not restart.
## AudioListener
* Exactly one `AudioListener` per scene. The Main Camera has one by
default; remove it if you add another.
* The listener's transform defines "where the player's ears are" for 3D
audio panning.
## Performance
* Pool AudioSources for sounds that play many times per second (machine
guns, rain). Reusing one AudioSource and calling `PlayOneShot` is fine
up to ~20 simultaneous clips.
* Use compressed formats (Vorbis) for music; uncompressed (WAV) for
frequent short SFX.
* Set `AudioClip.preloadAudioData = true` for short clips and `false` for
music that should stream from disk.
* Lower `maxDistance` aggressively; large falloff radii cost CPU on 3D
sounds.
|