From dc2b11fc8641b452075d4b3a5066df354aa83170 Mon Sep 17 00:00:00 2001 From: Tungdv Date: Fri, 15 May 2026 15:15:13 +0700 Subject: [PATCH] feat: add config sound setting. --- .../Config/Resources/GameRunConfig.asset | 1 + .../Scripts/GameData/GameRunConfig.cs | 4 +- .../PerfectWorld/Scripts/MainFiles/EC_Game.cs | 109 ++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) diff --git a/Assets/PerfectWorld/Config/Resources/GameRunConfig.asset b/Assets/PerfectWorld/Config/Resources/GameRunConfig.asset index 00b2e3516b..4593fa7bc0 100644 --- a/Assets/PerfectWorld/Config/Resources/GameRunConfig.asset +++ b/Assets/PerfectWorld/Config/Resources/GameRunConfig.asset @@ -69,3 +69,4 @@ MonoBehaviour: - title: Config 6 fShow: 100 fHide: 115 + audioMixer: {fileID: 24100000, guid: 9c6a7598ca0dfcd4fa51470ebbdd7549, type: 2} diff --git a/Assets/PerfectWorld/Scripts/GameData/GameRunConfig.cs b/Assets/PerfectWorld/Scripts/GameData/GameRunConfig.cs index 39a1c234ee..76e85ad6ae 100644 --- a/Assets/PerfectWorld/Scripts/GameData/GameRunConfig.cs +++ b/Assets/PerfectWorld/Scripts/GameData/GameRunConfig.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; +using UnityEngine.Audio; using UnityEngine.Rendering; using UnityEngine.Serialization; @@ -13,10 +14,11 @@ namespace BrewMonster [SerializeField] private List viewDistanceCfg; [SerializeField] private List viewDistanceNPCCfg; [SerializeField] private List viewDistanceEPCfg; - + [SerializeField] private AudioMixer audioMixer; public List GetViewDistanceCfg { get => viewDistanceCfg;} public List GetViewDistanceNPCCfg { get => viewDistanceNPCCfg;} public List GetViewDistanceEPCfg { get => viewDistanceEPCfg;} + public AudioMixer GetAudioMixer { get => audioMixer;} } [Serializable] diff --git a/Assets/PerfectWorld/Scripts/MainFiles/EC_Game.cs b/Assets/PerfectWorld/Scripts/MainFiles/EC_Game.cs index 824914f420..60e5eaccb2 100644 --- a/Assets/PerfectWorld/Scripts/MainFiles/EC_Game.cs +++ b/Assets/PerfectWorld/Scripts/MainFiles/EC_Game.cs @@ -49,6 +49,18 @@ namespace BrewMonster.Network private readonly static string keySettingActiveShadow = "_keySettingActiveShadow"; private readonly static string keySettingActiveFullResolution = "_keySettingActiveFullResolution"; private readonly static string keySettingActiveFog = "_keySettingActiveFog"; + + private readonly static string keySettingVolumeSoundMaster = "_keySettingVolumeSoundMaster"; + private readonly static string keySettingVolumeSoundSFX = "_keySettingVolumeSoundSFX"; + private readonly static string keySettingVolumeBgrMusic = "_keySettingVolumeBgrMusic"; + private readonly static string keySettingSoundMaster = "_keySettingSoundMaster"; + private readonly static string keySettingSoundSFX = "_keySettingSoundSFX"; + private readonly static string keySettingBgrMusic = "_keySettingBgrMusic"; + + private readonly static string keyMaster = "Master"; + private readonly static string keyMusic = "Music"; + private readonly static string keySFX = "SFX"; + private static ViewDistance m_viewDistance; private static ViewDistance m_viewDistanceNPC; private static ViewDistance m_viewDistanceEP; @@ -693,6 +705,103 @@ namespace BrewMonster.Network int index = PlayerPrefs.GetInt(keySettingActiveFog, defaultValue: 1); return index == 1; } + + public static void SetActiveSoundMaster(bool value) + { + PlayerPrefs.SetInt(keySettingSoundMaster, value ? 1 : 0); + if (value) + { + float index = PlayerPrefs.GetInt(keySettingVolumeSoundMaster, defaultValue: 6); + float dB = Mathf.Log10(index / 6) * 20; // setting sound is 6 + GameRunConfigSO.GetAudioMixer.SetFloat(keyMaster, dB); + } + else + { + float dB = Mathf.Log10(0.0001f) * 20; // setting sound is 6 + GameRunConfigSO.GetAudioMixer.SetFloat(keyMaster, dB); + } + } + + public static bool GetActiveSoundMaster() + { + return PlayerPrefs.GetInt(keySettingSoundMaster, 1) == 1; + } + + public static void SetVolumeSoundMaster(int value) + { + bool isActive = GetActiveSoundMaster(); + PlayerPrefs.SetInt(keySettingVolumeSoundMaster, value); + if (isActive) + { + float dB = Mathf.Log10(value / 6) * 20; // setting sound is 6 + GameRunConfigSO.GetAudioMixer.SetFloat(keyMaster, dB); + } + } + + public static void SetActiveSoundSFX(bool value) + { + PlayerPrefs.SetInt(keySettingSoundSFX, value ? 1 : 0); + if (value) + { + float index = PlayerPrefs.GetInt(keySettingVolumeSoundSFX, defaultValue: 6); + float dB = Mathf.Log10(index / 6) * 20; // setting sound is 6 + GameRunConfigSO.GetAudioMixer.SetFloat(keySFX, dB); + } + else + { + float dB = Mathf.Log10(0.0001f) * 20; // setting sound is 6 + GameRunConfigSO.GetAudioMixer.SetFloat(keySFX, dB); + } + } + + public static bool GetActiveSoundSFX() + { + return PlayerPrefs.GetInt(keySettingSoundSFX, 1) == 1; + } + + public static void SetVolumeSoundSFX(int value) + { + bool isActive = GetActiveSoundMaster() && GetActiveSoundSFX(); + PlayerPrefs.SetInt(keySettingVolumeSoundMaster, value); + if (isActive) + { + float dB = Mathf.Log10(value / 6) * 20; // setting sound is 6 + GameRunConfigSO.GetAudioMixer.SetFloat(keySFX, dB); + } + } + + public static void SetActiveSoundMusic(bool value) + { + PlayerPrefs.SetInt(keySettingBgrMusic, value ? 1 : 0); + if (value) + { + float index = PlayerPrefs.GetInt(keySettingVolumeBgrMusic, defaultValue: 6); + float dB = Mathf.Log10(index / 6) * 20; // setting sound is 6 + GameRunConfigSO.GetAudioMixer.SetFloat(keyMusic, dB); + } + else + { + float dB = Mathf.Log10(0.0001f) * 20; // setting sound is 6 + GameRunConfigSO.GetAudioMixer.SetFloat(keyMusic, dB); + } + } + + public static bool GetActiveBgrMusic() + { + return PlayerPrefs.GetInt(keySettingBgrMusic, 1) == 1; + } + + public static void SetVolumeBgrMusic(int value) + { + bool isActive = GetActiveSoundMaster() && GetActiveBgrMusic(); + PlayerPrefs.SetInt(keySettingVolumeSoundMaster, value); + if (isActive) + { + float dB = Mathf.Log10(value / 6) * 20; // setting sound is 6 + GameRunConfigSO.GetAudioMixer.SetFloat(keyMusic, dB); + } + } + #endregion } }