Audio
The Audio system provides a centralized way to manage music, sound effects (SFX), and UI sounds.
Components
Singleton manager for playing Music, SFX, UI, and Other
AudioClips.Supports:
Mixer group routing (
MusicGroup,SFXGroup,UIGroup,OtherGroup).Fade-in/out for Music.
Saving/restoring music playback positions.
Randomized pitch for SFX/UI.
Override previous Other audio clip.
Requires an
AudioRegistry.asset in aResourcesfolder in your project.
ScriptableObjectthat mapsstringkeys toAudioClips.Create one
AudioRegistryin your Project viaCreate > MeshMapLabs > Building Blocks > Audio > AudioRegistry. Place it in aResourcesfolder to be loaded viaAudioRegistryCache.Instance.Change load path dynamically with
AudioRegistryCache.SetResourcePath().
UI slider bindings for mixer volume control.
Saves user preferences to
PlayerPrefs.Mixer parameters:
MusicVolume,SFXVolume,UIVolume.
Simple
MonoBehaviourto play a clip on anAudioSource.Optional pitch randomization.
Can trigger on
OnEnableor manually.
Example Workflow
Add the
AudioManagerWithSettings.prefab to your scene.Create an
AudioRegistry.asset in aResourcesfolder.Add unique
stringkeys andAudioClipsto theAudioRegistry.Add three
Slidersto your game UI and assign them inAudioSettings.Add a
SliderValueTextcomponent to eachSliderand assign aTextMeshProUGUIto each.Play the
AudioClipsfrom anywhere in your project by referencingAudioManager.Instance.
Example Code
// Play UI sound
AudioManager.Instance.PlayUI("UI.ButtonClick", randomizePitch: true);
// Play looping background music
AudioManager.Instance.PlayMusic("Music.MainTheme", loop: true, fadeIn: true);
// Adjust music volume via UI slider
public void OnMusicVolumeChanged(float value)
{
AudioManager.Instance.SetMusicVolume(value);
}Prefabs
AudioManager — Ready-to-use manager
GameObjectwith mixer groups andAudioSources preconfigured. Make sure there is anAudioRegistryin your Project.AudioManagerWithSettings — Variant with
AudioSettingsUI bindings. Requires a UI with volume sliders to be setup.
Last updated