MOST Audio Manager
A persistent audio settings controller that maps 0–1 sliders and mute toggles to a Unity AudioMixer
via exposed parameter names, with JSON save/load.
Safe-by-default: If no Mixer or parameter name is set,
Apply()
is a no‑op (no errors).Four Ready-To-Go logical channels: Master, Music, SFX, UI.
Persistence: Optional auto‑load on Play; auto‑save on change and/or on inspector edits.
Mixer routing hints: Optional
AudioMixerGroup
references for Master/Music/SFX/UI to routeAudioSource.outputAudioMixerGroup
in your playback code.
Quick Start
Create an Audio Mixer Project → Create → Audio Mixer and one or more child groups (Master/Music/SFX/UI) as you prefer.
Expose the group Volume parameters:
In the Mixer window, right‑click a group’s Volume → Expose.
In the Exposed Parameters list, give each a clear name (e.g.,
MasterVolume
,MusicVolume
,SFXVolume
,UIVolume
).
Create the asset: Project → Create → MOST → Audio Manager → name it
M_AudioManager
.In the asset inspector:
Assign Mixer to your Audio Mixer.
(Optional) Assign MasterGroup / MusicGroup / SFXGroup / UIGroup.
Ensure the Exposed Parameter Names match your Mixer’s names.
Call once on boot (e.g., from a bootstrap component):
audioManager.ApplyAll();
(or rely on auto‑load).Bind UI to
SetVolume(channel, value)
andSetEnabled(channel, on)
. Leave ranges at [0..1].
Persistence Settings
autoLoadOnEnable: On Play, load from file if present, else keep inspector defaults and save.
autoSaveOnChange: Save after any SetVolume / SetEnabled (Toggle) / Apply
autoSaveInEditor: Mirror AudioManager inspector edits in Edit Mode (apply + save). (All audio controls will be visible on enable).
databaseKey: By default, "audio_state.json"... refers to the file name under Application.persistentDataPath
.
API
// Audio Channels (Enums)
public enum Channel { Master, Music, SFX, UI }
// MOST_AudioManager.Master > from anywhere
// MOST_AudioManager.Music
// MOST_AudioManager.SFX
// MOST_AudioManager.UI
// Sets the 0..1 volume for a channel (clamped)
// applies to mixer immediately, and saves if autoSaveOnChange.
void SetVolume(MOST_AudioManager.Channel ch, float slider01)
// Mutes/unmutes a channel
// applies to mixer immediately, and saves if autoSaveOnChange.
void SetEnabled(MOST_AudioManager.Channel ch, bool on)
// Pushes all 4 channels to the mixer.
// Safe no‑op if Mixer is null or param names are empty.
void ApplyAll()
// Pushes a single channel to the mixer.
// Safe no‑op if Mixer/param is unset.
void Apply(MOST_AudioManager.Channel ch)
// Returns the configured mixer group (may be null).
// Useful for wiring AudioSource.outputAudioMixerGroup.
AudioMixerGroup GetGroup(MOST_AudioManager.Channel ch)
// Serializes current volumes/mutes to JSON.
//Path: If null, uses persistentDataPath/databaseKey.
// Returns true on success.
bool SaveToFile(string path = null)
// Loads JSON if present and (optionally) applies values to the mixer.
// Returns true if a file existed and was parsed.
bool LoadFromFile(string path = null, bool applyNow = true)
// Returns a compact JSON string of volumes/mutes (no UnityEngine.Object references).
string Serialize()
// Loads from JSON text, clamping to 0..1; optionally applies to the mixer.
// Returns true on success.
bool Deserialize(string json, bool applyNow = true)
// Conversions between 0..1 linear scale and dB (0 → −80 dB, 1 → 0 dB).
MOST_AudioManager float LinearToDb(float v)
MOST_AudioManager float DbToLinear(float db)
Bind to UI
//
public MOST_AudioManager AudioMgr;
// Initialize at Startup
void Start()
{
if (!audioMgr.LoadFromFile(applyNow:true))
{
AudioMgr.ApplyAll();
AudioMgr.SaveToFile();
}
}
// Slider callbacks (0..1)
// bind each function to a UI slider
public void OnMasterSlider(float v) => AudioMgr.SetVolume(MOST_AudioManager.Channel.Master, v);
public void OnMusicSlider(float v) => AudioMgr.SetVolume(MOST_AudioManager.Channel.Music, v);
public void OnSFXSlider(float v) => AudioMgr.SetVolume(MOST_AudioManager.Channel.SFX, v);
public void OnUISlider(float v) => AudioMgr.SetVolume(MOST_AudioManager.Channel.UI, v);
// Toggle callbacks (mute)
// bind each function to a UI toggle
public void OnMasterToggle(bool on) => AudioMgr.SetEnabled(MOST_AudioManager.Channel.Master, on);
public void OnMusicToggle(bool on) => AudioMgr.SetEnabled(MOST_AudioManager.Channel.Music, on);
public void OnSFXToggle(bool on) => AudioMgr.SetEnabled(MOST_AudioManager.Channel.SFX, on);
public void OnUIToggle(bool on) => AudioMgr.SetEnabled(MOST_AudioManager.Channel.UI, on);
// Route Sources to Groups
// After assigning groups on the asset
source.outputAudioMixerGroup = AudioMgr.GetGroup(MOST_AudioManager.Channel.SFX);
Last updated