193 lines
6.2 KiB
C#
193 lines
6.2 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using UnityEngine.Audio;
|
||
|
||
public class audioMerger : MonoBehaviour
|
||
{
|
||
[Header("Audio Mixer")]
|
||
public AudioMixer mainMixer;
|
||
|
||
[Header("Exposed Parameters")]
|
||
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";
|
||
|
||
[Header("sv object")]
|
||
public GameObject sv_audioSettings;
|
||
[Header("Inspector")]
|
||
[Tooltip("Documentation text normalized.")]
|
||
public Toggle enableGlobalMute_toggle;
|
||
[Header("Inspector")]
|
||
public Slider mainVolumn_slider;
|
||
public Slider noteHit_slider;
|
||
public Slider musicInGame_slider;
|
||
public Slider musicOutGame_slider;
|
||
public Slider skillEffect_slider;
|
||
public Slider uiInteration_slider;
|
||
public Slider cvVolumn_slider;
|
||
[Header("Inspector")]
|
||
public Text mainVolumn_text;
|
||
public Text noteHit_text;
|
||
public Text musicInGame_text;
|
||
public Text musicOutGame_text;
|
||
public Text skillEffect_text;
|
||
public Text uiInteration_text;
|
||
public Text cvVolumn_text;
|
||
|
||
// PlayerPrefs keys
|
||
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";
|
||
|
||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||
void Start()
|
||
{
|
||
Initialize();
|
||
}
|
||
|
||
void OnEnable()
|
||
{
|
||
// Ensure state is correct whenever the UI is shown
|
||
Initialize();
|
||
}
|
||
|
||
void Initialize()
|
||
{
|
||
// Load saved state (default: false)
|
||
bool isOn = PlayerPrefs.GetInt(PlayerPrefKey_EnableGlobalMute, 0) == 1;
|
||
|
||
// Apply to toggle (if available) and sv_audioSettings
|
||
if (enableGlobalMute_toggle != null)
|
||
{
|
||
enableGlobalMute_toggle.onValueChanged.RemoveAllListeners();
|
||
enableGlobalMute_toggle.isOn = isOn;
|
||
enableGlobalMute_toggle.onValueChanged.AddListener(OnEnableGlobalMuteChanged);
|
||
}
|
||
|
||
if (sv_audioSettings != null)
|
||
{
|
||
sv_audioSettings.SetActive(!isOn);
|
||
}
|
||
|
||
InitializeSliders();
|
||
}
|
||
|
||
void InitializeSliders()
|
||
{
|
||
// Initialize 5 main sliders
|
||
SetupSlider(mainVolumn_slider, mainVolumn_text, Key_MainVol, masterParam);
|
||
SetupSlider(noteHit_slider, noteHit_text, Key_NoteHitVol, noteHitParam);
|
||
SetupSlider(musicInGame_slider, musicInGame_text, Key_MusicInGameVol, musicInGameParam);
|
||
SetupSlider(musicOutGame_slider, musicOutGame_text, Key_MusicOutGameVol, musicOutGameParam);
|
||
SetupSlider(uiInteration_slider, uiInteration_text, Key_UiVol, uiInterationParam);
|
||
}
|
||
|
||
void SetupSlider(Slider slider, Text text, string prefKey, string mixerParam)
|
||
{
|
||
if (slider == null) return;
|
||
|
||
// Ensure slider range is 0 to 1
|
||
slider.minValue = 0f;
|
||
slider.maxValue = 1f;
|
||
|
||
// Load saved value (default 1.0f for 100% / 0dB)
|
||
float savedVal = PlayerPrefs.GetFloat(prefKey, 1.0f);
|
||
slider.value = savedVal;
|
||
|
||
// Update display and mixer initially
|
||
UpdateSliderEffect(slider.value, text, mixerParam);
|
||
|
||
// Add listener
|
||
slider.onValueChanged.RemoveAllListeners();
|
||
slider.onValueChanged.AddListener((val) => {
|
||
UpdateSliderEffect(val, text, mixerParam);
|
||
PlayerPrefs.SetFloat(prefKey, val);
|
||
PlayerPrefs.Save();
|
||
});
|
||
}
|
||
|
||
void UpdateSliderEffect(float value, Text text, string mixerParam)
|
||
{
|
||
// 1. Update Text (Linear percentage)
|
||
if (text != null)
|
||
{
|
||
text.text = Mathf.RoundToInt(value * 100f).ToString() + "%";
|
||
}
|
||
|
||
// 2. Update Mixer (Logarithmic)
|
||
if (mainMixer != null && !string.IsNullOrEmpty(mixerParam))
|
||
{
|
||
// Check Global Mute state from PlayerPrefs directly for robustness
|
||
bool isMuted = PlayerPrefs.GetInt(PlayerPrefKey_EnableGlobalMute, 0) == 1;
|
||
|
||
// If this is Master and Global Mute is ON, don't update mixer to anything other than -80dB
|
||
if (mixerParam == masterParam && isMuted)
|
||
{
|
||
mainMixer.SetFloat(mixerParam, -80f);
|
||
return;
|
||
}
|
||
|
||
// Normal volume application
|
||
float clampedValue = Mathf.Clamp(value, 0.0001f, 1.0f);
|
||
float dB = Mathf.Log10(clampedValue) * 20f;
|
||
|
||
mainMixer.SetFloat(mixerParam, dB);
|
||
}
|
||
}
|
||
|
||
void OnDestroy()
|
||
{
|
||
if (enableGlobalMute_toggle != null)
|
||
{
|
||
enableGlobalMute_toggle.onValueChanged.RemoveListener(OnEnableGlobalMuteChanged);
|
||
}
|
||
|
||
// Remove listeners from sliders to be safe
|
||
RemoveSliderListeners(mainVolumn_slider);
|
||
RemoveSliderListeners(noteHit_slider);
|
||
RemoveSliderListeners(musicInGame_slider);
|
||
RemoveSliderListeners(musicOutGame_slider);
|
||
RemoveSliderListeners(uiInteration_slider);
|
||
}
|
||
|
||
void RemoveSliderListeners(Slider slider)
|
||
{
|
||
if (slider != null) slider.onValueChanged.RemoveAllListeners();
|
||
}
|
||
|
||
// Listener called when the toggle value changes
|
||
void OnEnableGlobalMuteChanged(bool isOn)
|
||
{
|
||
// 1. Save state
|
||
PlayerPrefs.SetInt(PlayerPrefKey_EnableGlobalMute, isOn ? 1 : 0);
|
||
PlayerPrefs.Save();
|
||
|
||
// 2. UI visual feedback
|
||
if (sv_audioSettings != null)
|
||
{
|
||
sv_audioSettings.SetActive(!isOn);
|
||
}
|
||
|
||
// 3. Apply mute/unmute to Master
|
||
if (mainMixer != null)
|
||
{
|
||
if (isOn)
|
||
{
|
||
// Mute: set to -80dB
|
||
mainMixer.SetFloat(masterParam, -80f);
|
||
}
|
||
else
|
||
{
|
||
// Unmute: restore from PlayerPrefs (Key_MainVol)
|
||
float savedVal = PlayerPrefs.GetFloat(Key_MainVol, 1.0f);
|
||
UpdateSliderEffect(savedVal, mainVolumn_text, masterParam);
|
||
}
|
||
}
|
||
}
|
||
}
|