diff --git a/Assets/Scripts/AudioManager.cs b/Assets/Scripts/AudioManager.cs index 1e7f5ce..c00020e 100644 --- a/Assets/Scripts/AudioManager.cs +++ b/Assets/Scripts/AudioManager.cs @@ -30,6 +30,13 @@ public class AudioManager : MonoBehaviour return instance; } } + + [SerializeField, Range(0, 1)] + private float masterVolume = 1f; + + private float prevMasterVolume; + + private FMOD.ChannelGroup masterChannelGroup; void Awake() { @@ -43,6 +50,20 @@ public class AudioManager : MonoBehaviour InitFmod(); } + void Update() + { + if (!fmodSystem.hasHandle() || !masterChannelGroup.hasHandle()) + return; + + if (!Mathf.Approximately(masterVolume, prevMasterVolume)) + { + masterChannelGroup.setVolume(masterVolume); + prevMasterVolume = masterVolume; + } + + masterChannelGroup.getVolume(out masterVolume); + } + void OnDisable() { // Ensure FMOD gets closed before hot reload @@ -85,6 +106,11 @@ public class AudioManager : MonoBehaviour result = fmodSystem.init(MaxVirtualChannels, FMOD.INITFLAGS.VOL0_BECOMES_VIRTUAL, IntPtr.Zero); CheckFmodResult(result, "FMOD.System.init"); + + result = fmodSystem.getMasterChannelGroup(out masterChannelGroup); + CheckFmodResult(result, "FMOD.System.getMasterChannelGroup"); + masterChannelGroup.setVolume(masterVolume); + prevMasterVolume = masterVolume; } private void CheckFmodResult(FMOD.RESULT result, string cause)