From cb5bbe82baef25bd05b584a3d5b02e7e29311103 Mon Sep 17 00:00:00 2001 From: zhangaotian <8474238@qq.com> Date: Thu, 8 Aug 2024 14:17:03 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E8=AE=BE=E7=BD=AE=E3=80=91=E5=88=86?= =?UTF-8?q?=E7=A6=BB=E8=AE=BE=E7=BD=AE=E9=9C=80=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Scripts/Framework/Manager/AudioManager.cs | 24 +++++--- .../Framework/Manager/SettingManager.cs | 59 +++++++++++++++++++ .../Framework/Manager/SettingManager.cs.meta | 11 ++++ .../Framework/Manager/StorageManager.cs | 12 ---- .../Scripts/Framework/UI/StringManager.cs | 11 +--- .../Gameplay/Constants/Constants.Level.cs | 4 +- .../GameStartLoadingExecutor.cs | 2 +- .../UI/StartPanel/UISettingPanelController.cs | 21 ++----- 8 files changed, 93 insertions(+), 51 deletions(-) create mode 100644 Forest/Assets/Scripts/Framework/Manager/SettingManager.cs create mode 100644 Forest/Assets/Scripts/Framework/Manager/SettingManager.cs.meta diff --git a/Forest/Assets/Scripts/Framework/Manager/AudioManager.cs b/Forest/Assets/Scripts/Framework/Manager/AudioManager.cs index fa48aed..fd892de 100644 --- a/Forest/Assets/Scripts/Framework/Manager/AudioManager.cs +++ b/Forest/Assets/Scripts/Framework/Manager/AudioManager.cs @@ -14,8 +14,8 @@ namespace Framework.Manager public AudioClip BGM; public AudioClip BGMInLevel; - - + + private Dictionary _soundDic; private SingleAudio _soundAudio; @@ -30,11 +30,6 @@ namespace Framework.Manager { _soundDic = new Dictionary(); AudioUnityMgr.Instance.Init(); - MuteVolume(true); - MusicVolume = PlayerPrefs.GetFloat(LevelConstants.Music, 1); - SoundVolume = PlayerPrefs.GetFloat(LevelConstants.Sound, 1); - SetMusic(MusicVolume); - SetSound(SoundVolume); } public async UniTask PreLoadAudio() @@ -89,6 +84,11 @@ namespace Framework.Manager public void SetMusic(float volume) { + if (_bgmAudio != null) + { + _bgmAudio.audioSource.volume = volume; + } + AudioUnityMgr.Instance.GlobalMusicVolume = volume; } @@ -104,6 +104,12 @@ namespace Framework.Manager public void MuteMusic(bool isEnable) { + var volume = isEnable ? 1f : 0f; + if (_bgmAudio != null) + { + _bgmAudio.audioSource.volume = volume; + } + AudioUnityMgr.Instance.SetMusicEnable(isEnable); } @@ -122,8 +128,8 @@ namespace Framework.Manager { AudioUnityMgr.Instance.SetVolumeEnable(isEnable); } - - + + public void PlayMusic(AudioType type, AudioClip clip, UnityAudio audioData, Transform trans = null, Vector3 pos = new()) { diff --git a/Forest/Assets/Scripts/Framework/Manager/SettingManager.cs b/Forest/Assets/Scripts/Framework/Manager/SettingManager.cs new file mode 100644 index 0000000..22af8f0 --- /dev/null +++ b/Forest/Assets/Scripts/Framework/Manager/SettingManager.cs @@ -0,0 +1,59 @@ +using Framework.Manager; +using Framework.UI; +using UnityEngine; +using PhxhSDK; + +public class SettingData +{ + public float MusicVolume = 1f; + public float SoundVolume = 1f; + + public string Language; +} + +public class SettingManager : Singlenton +{ + private const string SettingKey = "SettingKey"; + private SettingData _settingData; + + public void Init() + { + _settingData = StorageManager.Instance.GetStorage(SettingKey); + _settingData ??= new SettingData(); + + //初始化音量 + AudioManager.Instance.MuteVolume(true); + AudioManager.Instance.SoundVolume = _settingData.SoundVolume; + AudioManager.Instance.MusicVolume = _settingData.MusicVolume; + var muteMusic = Mathf.Approximately(_settingData.MusicVolume, 1f); + var muteSound = Mathf.Approximately(_settingData.SoundVolume, 1f); + AudioManager.Instance.MuteMusic(muteMusic); + AudioManager.Instance.MuteSound(muteSound); + + //初始化语言 + StringManager.Instance.InitLanguage(_settingData.Language); + } + + public void SetMusic(float music) + { + _settingData.MusicVolume = music; + SaveSetting(); + } + + public void SetSound(float sound) + { + _settingData.SoundVolume = sound; + SaveSetting(); + } + + public void SetLanguage(string language) + { + _settingData.Language = language; + SaveSetting(); + } + + private void SaveSetting() + { + StorageManager.Instance.SaveWithoutUpdate(); + } +} \ No newline at end of file diff --git a/Forest/Assets/Scripts/Framework/Manager/SettingManager.cs.meta b/Forest/Assets/Scripts/Framework/Manager/SettingManager.cs.meta new file mode 100644 index 0000000..7ad26e2 --- /dev/null +++ b/Forest/Assets/Scripts/Framework/Manager/SettingManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2547a1a1f16b34289b1cd986570d1c7b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Forest/Assets/Scripts/Framework/Manager/StorageManager.cs b/Forest/Assets/Scripts/Framework/Manager/StorageManager.cs index 174a2a7..10a0079 100644 --- a/Forest/Assets/Scripts/Framework/Manager/StorageManager.cs +++ b/Forest/Assets/Scripts/Framework/Manager/StorageManager.cs @@ -141,17 +141,5 @@ namespace Framework.Manager _serverDataIO.LoadToClass(jsonData, ref AppInfoManager.Instance.RemoteAppUserInfo); } } - - public void DeleteSaveData() - { - foreach (var key in localStorageMap.Keys) - { - UnityEngine.PlayerPrefs.DeleteKey(key); - } - - localStorageMap.Clear(); - UnityEngine.PlayerPrefs.Save(); - DebugUtil.LogWarning("存档数据已成功删除"); - } } } \ No newline at end of file diff --git a/Forest/Assets/Scripts/Framework/UI/StringManager.cs b/Forest/Assets/Scripts/Framework/UI/StringManager.cs index 977019c..842866f 100644 --- a/Forest/Assets/Scripts/Framework/UI/StringManager.cs +++ b/Forest/Assets/Scripts/Framework/UI/StringManager.cs @@ -32,16 +32,9 @@ namespace Framework.UI EN, } - public void InitLanguage() + public void InitLanguage(string language) { - if (UnityEngine.PlayerPrefs.HasKey(LevelConstants.Language)) - { - CurLanguage = String2LanguageType(UnityEngine.PlayerPrefs.GetString(LevelConstants.Language)); - } - else - { - CurLanguage = System2LanguageType(Application.systemLanguage); - } + CurLanguage = !string.IsNullOrEmpty(language) ? String2LanguageType(language) : System2LanguageType(Application.systemLanguage); EventManager.Instance.Send(EventManager.EventName.RefreshLanguage); } diff --git a/Forest/Assets/Scripts/Gameplay/Constants/Constants.Level.cs b/Forest/Assets/Scripts/Gameplay/Constants/Constants.Level.cs index e698882..820a38b 100644 --- a/Forest/Assets/Scripts/Gameplay/Constants/Constants.Level.cs +++ b/Forest/Assets/Scripts/Gameplay/Constants/Constants.Level.cs @@ -14,9 +14,7 @@ namespace Framework.Constants public const string English = "English"; //设置本地存档Key - public const string Music = "Music"; - public const string Sound = "Sound"; - public const string Language = "Language"; + public const string FirstLaunch = "Launch"; //BI事件 diff --git a/Forest/Assets/Scripts/Gameplay/LoadingExecutor/GameStartLoadingExecutor.cs b/Forest/Assets/Scripts/Gameplay/LoadingExecutor/GameStartLoadingExecutor.cs index 1e27226..239c03d 100644 --- a/Forest/Assets/Scripts/Gameplay/LoadingExecutor/GameStartLoadingExecutor.cs +++ b/Forest/Assets/Scripts/Gameplay/LoadingExecutor/GameStartLoadingExecutor.cs @@ -32,7 +32,7 @@ namespace Gameplay.LoadingExecutor _destProgress = 40f; await PreLoadSelectLevel(); _destProgress = 50f; - StringManager.Instance.InitLanguage(); + SettingManager.Instance.Init(); await InitBuildScene(); _destProgress = 60f; await InitGfx(); diff --git a/Forest/Assets/Scripts/Gameplay/UI/StartPanel/UISettingPanelController.cs b/Forest/Assets/Scripts/Gameplay/UI/StartPanel/UISettingPanelController.cs index c2e3477..2a8eaec 100644 --- a/Forest/Assets/Scripts/Gameplay/UI/StartPanel/UISettingPanelController.cs +++ b/Forest/Assets/Scripts/Gameplay/UI/StartPanel/UISettingPanelController.cs @@ -23,9 +23,7 @@ public class UISettingPanelController : UIWindow private GameObject _tipConfirm; private TMP_Dropdown _lanDropdown; - - private TMP_Text _account; - + private bool _isMuteMusic; private bool _isMuteSound; @@ -40,7 +38,6 @@ public class UISettingPanelController : UIWindow _uiBtnCloseM = FindObj("BG_SettingPanel/Music/Btn_Switch_M/UI_Btn_Close_M").gameObject; _uiBtnOpenS = FindObj("BG_SettingPanel/Sound/Btn_Switch_S/UI_Btn_Open_S").gameObject; _uiBtnCloseS = FindObj("BG_SettingPanel/Sound/Btn_Switch_S/UI_Btn_Close_S").gameObject; - _account = FindObj("Txt_Account").GetComponent(); BindButton("BG_SettingPanel/Btn_Close", () => { EventManager.Instance.Send(EventManager.EventName.ShowGfx); @@ -99,13 +96,11 @@ public class UISettingPanelController : UIWindow EventManager.Instance.Send(EventManager.EventName.RefreshLanguage); BIManager.Instance.TrackEvent(biEvent); - PlayerPrefs.SetString(LevelConstants.Language, language.ToString()); - PlayerPrefs.Save(); + SettingManager.Instance.SetLanguage(language.ToString()); } protected override void OnOpenWindow(object data) { - UpdateID(); EventManager.Instance.Send(EventManager.EventName.HideGfx); _rawImageGaussianBlurMask.texture = CommonUIUtils.RenderTexture; } @@ -117,18 +112,12 @@ public class UISettingPanelController : UIWindow UpdateUI(); } - void UpdateID() - { - _account.text = LoginManager.Instance.Account; - } - private void UpdateUI() { _uiBtnOpenM.SetActive(!_isMuteMusic); _uiBtnCloseM.SetActive(_isMuteMusic); _uiBtnOpenS.SetActive(!_isMuteSound); _uiBtnCloseS.SetActive(_isMuteSound); - _account.text = LoginManager.Instance.Account; } private void ChangeMusic() @@ -139,8 +128,7 @@ public class UISettingPanelController : UIWindow UpdateUI(); var eventName = _isMuteMusic ? cfg.BI.Event.music_off : cfg.BI.Event.music_on; BIManager.Instance.TrackEvent(eventName); - PlayerPrefs.SetFloat(LevelConstants.Music, AudioManager.Instance.MusicVolume); - PlayerPrefs.Save(); + SettingManager.Instance.SetMusic(AudioManager.Instance.MusicVolume); } private void ChangeSound() @@ -151,7 +139,6 @@ public class UISettingPanelController : UIWindow UpdateUI(); var eventName = _isMuteSound ? cfg.BI.Event.sfx_off : cfg.BI.Event.sfx_on; BIManager.Instance.TrackEvent(eventName); - PlayerPrefs.SetFloat(LevelConstants.Sound, AudioManager.Instance.SoundVolume); - PlayerPrefs.Save(); + SettingManager.Instance.SetSound(AudioManager.Instance.SoundVolume); } } \ No newline at end of file