| using System.Collections.Generic; | 
| using System.Text; | 
| using UnityEngine; | 
| using UnityEngine.Events; | 
| using System; | 
| using System.Collections; | 
| using Cysharp.Threading.Tasks; | 
|   | 
|   | 
| public class SystemSetting : Singleton<SystemSetting> | 
|   | 
| { | 
|     const string QUALITY_LEVEL_KEY = "GameQualityLevel"; | 
|     const string SOUND_VOLUME_KEY = "SoundRatioKey";  //音乐 | 
|     const string SOUND_EFFECT_KEY = "SoundEffect"; //音效 | 
|     const string GAMEFRAME_KEY = "GameFrameSetting";//垂直同步率 | 
|   | 
|     public Dictionary<SystemSwitch, bool> systemSettings = new Dictionary<SystemSwitch, bool>(); | 
|   | 
|     int m_PlayerSyncCount = -1; | 
|   | 
|     public event Action<SystemSwitch, bool> OnSettingChanged; | 
|     public event Action qualityLevelChangeEvent; | 
|     public event Action playerSyncCountChangeEvent; | 
|     public event Action gameFrameChangeEvent; | 
|   | 
|     public SystemSetting() | 
|     { | 
|         DTC0102_tagCDBPlayer.switchAccountEvent += OnSwitchAccount; | 
|     } | 
|   | 
|     ~SystemSetting() | 
|     { | 
|         DTC0102_tagCDBPlayer.switchAccountEvent -= OnSwitchAccount; | 
|     } | 
|   | 
|     public void SetSoundVolume(float _volume) | 
|     { | 
|         LocalSave.SetFloat(SOUND_VOLUME_KEY, Mathf.Clamp01(_volume)); | 
|         SoundPlayer.Instance.musicAudioSource.volume = Mathf.Clamp01(_volume); | 
|     } | 
|   | 
|     public float GetSoundVolume() | 
|     { | 
|         return LocalSave.GetFloat(SOUND_VOLUME_KEY, .55f); | 
|     } | 
|   | 
|     public void SetSoundEffect(float value) | 
|     { | 
|         LocalSave.SetFloat(SOUND_EFFECT_KEY, Mathf.Clamp01(value)); | 
|     } | 
|   | 
|     public float GetSoundEffect() | 
|     { | 
|         return LocalSave.GetFloat(SOUND_EFFECT_KEY, .8f); | 
|     } | 
|   | 
|     public void SetGameFps(GameFps _frame) | 
|     { | 
|         LocalSave.SetInt(GAMEFRAME_KEY, (int)_frame); | 
|         Application.targetFrameRate = Mathf.Clamp((int)_frame, 30, 60); | 
|         if (gameFrameChangeEvent != null) | 
|         { | 
|             gameFrameChangeEvent(); | 
|         } | 
|     } | 
|   | 
|     public GameFps GetGameFps() | 
|     { | 
|         return (GameFps)LocalSave.GetInt(GAMEFRAME_KEY, (int)GameFps.Full); | 
|     } | 
|   | 
|     public void SetSystemSettingSwitch(SystemSwitch type, bool _value) | 
|     { | 
|         SettingMgr.Instance.SetAccountSetStr(type.ToString(), _value.ToString()); | 
|         systemSettings[type] = _value; | 
|   | 
|         if (OnSettingChanged != null) | 
|         { | 
|             OnSettingChanged(type, _value); | 
|         } | 
|     } | 
|   | 
|     public bool GetSystemSettingSwitch(SystemSwitch type) | 
|     { | 
|         if (systemSettings.ContainsKey(type)) | 
|         { | 
|             return systemSettings[type]; | 
|         } | 
|         else | 
|         { | 
|             return systemSettings[type] = SettingMgr.Instance.GetAccountSetBoolInfo(type.ToString()); ; | 
|         } | 
|     } | 
|   | 
|     public void SetPlayerSyncCount(int _syncCount) | 
|     { | 
|         LocalSave.SetInt("PlayerSyncCount", _syncCount); | 
|         if (m_PlayerSyncCount != _syncCount) | 
|         { | 
|             m_PlayerSyncCount = _syncCount; | 
|             if (playerSyncCountChangeEvent != null) | 
|             { | 
|                 playerSyncCountChangeEvent(); | 
|             } | 
|         } | 
|     } | 
|   | 
|     public int GetPlayerSyncCount() | 
|     { | 
|         if (m_PlayerSyncCount == -1) | 
|         { | 
|             var result = LocalSave.GetInt("PlayerSyncCount", -1); | 
|             if (result == -1) | 
|             { | 
|                 var quality = GetCurrentQualityLevel(); | 
|                 switch (quality) | 
|                 { | 
|                     case GameQuality.High: | 
|                         m_PlayerSyncCount = 10; | 
|                         break; | 
|                     case GameQuality.Low: | 
|                         m_PlayerSyncCount = 3; | 
|                         break; | 
|                     case GameQuality.Medium: | 
|                         m_PlayerSyncCount = 5; | 
|                         break; | 
|                 } | 
|             } | 
|             else | 
|             { | 
|                 m_PlayerSyncCount = Mathf.Clamp(result, 0, 20); | 
|             } | 
|         } | 
|   | 
|         return m_PlayerSyncCount; | 
|     } | 
|   | 
|     public void SetQualityLevel(GameQuality _quality, bool _disabeCamera = true) | 
|     { | 
|         LocalSave.SetInt(QUALITY_LEVEL_KEY, Mathf.Clamp((int)_quality, 0, 2)); | 
|         QualitySettings.SetQualityLevel(Mathf.Clamp((int)_quality, 0, 2)); | 
|         switch (_quality) | 
|         { | 
|             case GameQuality.Low: | 
|                 Shader.globalMaximumLOD = 300; | 
|                 Shader.DisableKeyword("QUALITY_HGH"); | 
|                 Shader.DisableKeyword("QUALITY_MED"); | 
|                 Shader.EnableKeyword("QUALITY_LOW"); | 
|                 break; | 
|             case GameQuality.Medium: | 
|                 Shader.globalMaximumLOD = 300; | 
|                 Shader.DisableKeyword("QUALITY_HGH"); | 
|                 Shader.EnableKeyword("QUALITY_MED"); | 
|                 Shader.DisableKeyword("QUALITY_LOW"); | 
|                 break; | 
|             case GameQuality.High: | 
|                 Shader.globalMaximumLOD = 500; | 
|                 Shader.EnableKeyword("QUALITY_HGH"); | 
|                 Shader.DisableKeyword("QUALITY_MED"); | 
|                 Shader.DisableKeyword("QUALITY_LOW"); | 
|                 break; | 
|         } | 
|   | 
| #if UNITY_ANDROID && !UNITY_EDITOR | 
|         if (_disabeCamera) | 
|         { | 
|             CameraManager.uiCamera.enabled = false; | 
|         } | 
|   | 
|         ResolutionUtility.AdjustResolution(_quality); | 
|         if (_disabeCamera) | 
|         { | 
|             Co_WaitFewMinute(); | 
|         } | 
| #endif | 
|   | 
|         if (qualityLevelChangeEvent != null) | 
|         { | 
|             qualityLevelChangeEvent(); | 
|         } | 
|   | 
|     } | 
|   | 
|     public GameQuality GetCurrentQualityLevel() | 
|     { | 
|         //目前看起来所有的安卓机跑高效果都可以接受,先给玩家默认设置高效果。 | 
|         if (Application.platform == RuntimePlatform.Android) | 
|         { | 
|             if (!LocalSave.HasKey(QUALITY_LEVEL_KEY)) | 
|             { | 
|                 var defaultQuality = GameQuality.High; | 
|                 if (DeviceUtility.cpu >= 4 && DeviceUtility.memory > 3.2f * 1024) | 
|                 { | 
|                     defaultQuality = GameQuality.High; | 
|                 } | 
|                 else if (DeviceUtility.cpu >= 4 && DeviceUtility.memory > 2.5f * 1024) | 
|                 { | 
|                     defaultQuality = GameQuality.Medium; | 
|                 } | 
|                 else if (DeviceUtility.cpu > 1 && DeviceUtility.memory > 1.5f * 1024) | 
|                 { | 
|                     defaultQuality = GameQuality.Low; | 
|                 } | 
|   | 
|                 return (GameQuality)LocalSave.GetInt(QUALITY_LEVEL_KEY, (int)defaultQuality); | 
|             } | 
|             else | 
|             { | 
|                 return (GameQuality)LocalSave.GetInt(QUALITY_LEVEL_KEY); | 
|             } | 
|         } | 
|         else | 
|         { | 
|             return (GameQuality)LocalSave.GetInt(QUALITY_LEVEL_KEY, (int)GameQuality.High); | 
|         } | 
|     } | 
|   | 
|     public void LetFPSUnLimit() | 
|     { | 
|         Application.targetFrameRate = 1000; | 
|     } | 
|   | 
|     private void OnSwitchAccount() | 
|     { | 
|         systemSettings.Clear(); | 
|         m_PlayerSyncCount = -1; | 
|     } | 
|   | 
|     private GameFps GetRecommendFps() | 
|     { | 
|         if (Screen.width * Screen.height > (1920 * 1080 + 10000)) | 
|         { | 
|             return GameFps.Half; | 
|         } | 
|         else | 
|         { | 
|             return GameFps.Full; | 
|         } | 
|     } | 
|   | 
|     async UniTask Co_WaitFewMinute() | 
|     { | 
|         await UniTask.Delay(500); | 
|         CameraManager.uiCamera.enabled = true; | 
|     } | 
| } | 
|   | 
| public enum SystemSwitch | 
| { | 
|     OtherPlayer,          //是否显示其他玩家 | 
|     HideMonster,   //是否显示普通小怪 | 
| } | 
|   | 
| public enum GameQuality | 
| { | 
|     Low = 0, | 
|     Medium = 1, | 
|     High = 2, | 
| } | 
|   | 
| public enum GameFps | 
| { | 
|     Free = -1, | 
|     Half = 30, | 
|     Full = 60, | 
| } |