Files
bansonic_beta_main/Assets/audioSettingsPreloader.cs

71 lines
2.2 KiB
C#

using UnityEngine;
using UnityEngine.Audio;
public class audioSettingsPreloader : MonoBehaviour
{
[Header("Audio Mixer")]
public AudioMixer mainMixer;
[Header("Exposed Parameters (Must match Editor)")]
public string masterParam = "Master_Vol";
public string noteHitParam = "noteHit_sfx_Vol";
public string musicInGameParam = "inGameMusic_Vol";
public string musicOutGameParam = "outGameMusic_Vol";
public string uiInterationParam = "button_sfx_Vol";
// PlayerPrefs keys (Must match audioMerger.cs)
const string PlayerPrefKey_EnableGlobalMute = "EnableGlobalMute";
const string Key_MainVol = "Volume_Main";
const string Key_NoteHitVol = "Volume_NoteHit";
const string Key_MusicInGameVol = "Volume_MusicInGame";
const string Key_MusicOutGameVol = "Volume_MusicOutGame";
const string Key_UiVol = "Volume_UI";
void Start()
{
ApplyAudioSettings();
}
public void ApplyAudioSettings()
{
if (mainMixer == null)
{
Debug.LogWarning("[audioSettingsPreloader] Main Mixer is not assigned!");
return;
}
// 1. Check Global Mute first
bool isMuted = PlayerPrefs.GetInt(PlayerPrefKey_EnableGlobalMute, 0) == 1;
if (isMuted)
{
// Set Master to -80dB immediately
mainMixer.SetFloat(masterParam, -80f);
}
else
{
// Apply normal Master volume
ApplyVolume(Key_MainVol, masterParam);
}
// 2. Apply other volumes
ApplyVolume(Key_NoteHitVol, noteHitParam);
ApplyVolume(Key_MusicInGameVol, musicInGameParam);
ApplyVolume(Key_MusicOutGameVol, musicOutGameParam);
ApplyVolume(Key_UiVol, uiInterationParam);
Debug.Log($"[audioSettingsPreloader] Audio settings applied. Muted: {isMuted}");
}
void ApplyVolume(string prefKey, string mixerParam)
{
if (string.IsNullOrEmpty(mixerParam)) return;
float linearValue = PlayerPrefs.GetFloat(prefKey, 1.0f);
float clampedValue = Mathf.Clamp(linearValue, 0.0001f, 1.0f);
float dB = Mathf.Log10(clampedValue) * 20f;
mainMixer.SetFloat(mixerParam, dB);
}
}