1.6 KiB
Audio System
Centralised voice-limited audio for the game. Lives in Assets/_Project/Scripts/Audio/.
How it works
AudioManager is a per-scene MonoBehaviour singleton. On Awake it pre-allocates a pool of AudioSource components (on its own GameObject) for each sound category. Callers request playback via AudioManager.Instance.Play(clip, category, pitch) — the manager picks the next source in that category's pool using round-robin, interrupting the oldest sound if all voices are busy.
Categories (AudioCategory enum)
| Category | Typical use |
|---|---|
TowerFire |
Tower attack sounds |
EnemyHitDeath |
Enemy damage and death sounds |
UI |
Button clicks, placement confirmation, etc. |
Music |
Background music |
Voice limits and volume per category are configured in the Inspector on the AudioManager scene GameObject.
Scene setup
An empty GameObject named AudioManager must exist in each gameplay scene with the AudioManager component attached. Configure the Categories array in the Inspector — one entry per AudioCategory value.
Suggested defaults:
- TowerFire: 4 voices, volume 1.0
- EnemyHitDeath: 8 voices, volume 1.0
- UI: 4 voices, volume 1.0
- Music: 2 voices, volume 0.8
Adding a new sound-emitting component
- Get a reference to an
AudioClipvia[SerializeField] - Call
AudioManager.Instance?.Play(clip, AudioCategory.YourCategory)at the point the sound should trigger - Pitch randomization (if desired) is the caller's responsibility:
Random.Range(minPitch, maxPitch)passed as the third argument
See TeslaTowerFireSound.cs in Scripts/Combat/ as a reference implementation.