主界面添加,许多bug修复。祝贺黑盒1如期开始
This commit is contained in:
@@ -1,8 +1,19 @@
|
||||
using UnityEngine;
|
||||
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")]
|
||||
@@ -25,11 +36,27 @@ public class audioMerger : MonoBehaviour
|
||||
public Text uiInteration_text;
|
||||
public Text cvVolumn_text;
|
||||
|
||||
// PlayerPrefs key for storing the toggle state
|
||||
// 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;
|
||||
@@ -37,7 +64,6 @@ public class audioMerger : MonoBehaviour
|
||||
// Apply to toggle (if available) and sv_audioSettings
|
||||
if (enableGlobalMute_toggle != null)
|
||||
{
|
||||
// Prevent accidentally invoking listeners while initializing
|
||||
enableGlobalMute_toggle.onValueChanged.RemoveAllListeners();
|
||||
enableGlobalMute_toggle.isOn = isOn;
|
||||
enableGlobalMute_toggle.onValueChanged.AddListener(OnEnableGlobalMuteChanged);
|
||||
@@ -45,9 +71,73 @@ public class audioMerger : MonoBehaviour
|
||||
|
||||
if (sv_audioSettings != null)
|
||||
{
|
||||
// Documentation text normalized.
|
||||
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()
|
||||
@@ -56,25 +146,47 @@ public class audioMerger : MonoBehaviour
|
||||
{
|
||||
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)
|
||||
{
|
||||
// Save to PlayerPrefs
|
||||
// 1. Save state
|
||||
PlayerPrefs.SetInt(PlayerPrefKey_EnableGlobalMute, isOn ? 1 : 0);
|
||||
PlayerPrefs.Save();
|
||||
|
||||
// Documentation text normalized.
|
||||
// 2. UI visual feedback
|
||||
if (sv_audioSettings != null)
|
||||
{
|
||||
sv_audioSettings.SetActive(!isOn);
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,8 +34,8 @@ public class controllerSettings : MonoBehaviour
|
||||
private bool previousKeyValid = false;
|
||||
|
||||
// slider range
|
||||
private const float NoteSpeedMin = 0.8f;
|
||||
private const float NoteSpeedMax = 1.25f;
|
||||
private const float NoteSpeedMin = 0.5f;
|
||||
private const float NoteSpeedMax = 2f;
|
||||
private const string NoteSpeedPrefKey = "noteSpeedMultiplier";
|
||||
|
||||
// continuous change
|
||||
@@ -441,7 +441,9 @@ public class controllerSettings : MonoBehaviour
|
||||
{
|
||||
if (noteSpeedMultipler_valueText != null)
|
||||
{
|
||||
noteSpeedMultipler_valueText.text = value.ToString("F3");
|
||||
// 将 0.5-2.0 映射为 50%-200% 的百分比显示
|
||||
int percentage = Mathf.RoundToInt(value * 100f);
|
||||
noteSpeedMultipler_valueText.text = $"{percentage}%";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user