using Newtonsoft.Json; using UnityEngine; namespace Base { public class Setting : Utils.Singleton { // 游戏设置数据类(用于序列化) [System.Serializable] public class GameSettings { public float progressStepDuration = 1f; public float exitAnimationDuration = 2f; public bool developerMode = false; public bool friendlyFire = false; public float globalVolume = 1.0f; public WindowMode currentWindowMode = WindowMode.Fullscreen; public Vector2Int windowResolution = new(1920, 1080); } // 当前游戏设置 public GameSettings CurrentSettings = new(); // 窗口模式枚举 public enum WindowMode { Fullscreen, Windowed, Borderless } // 常用分辨率选项 public static readonly Vector2Int[] CommonResolutions = new Vector2Int[] { new(800, 600), new(1024, 768), new(1280, 720), new(1366, 768), new(1600, 900), new(1920, 1080), new(2560, 1440), new(3840, 2160) }; // 初始化加载设置 public void Init() { LoadSettings(); } public void SaveSettings() { string json = JsonConvert.SerializeObject(CurrentSettings); PlayerPrefs.SetString("GameSettings", json); PlayerPrefs.Save(); } public void LoadSettings() { if (PlayerPrefs.HasKey("GameSettings")) { string json = PlayerPrefs.GetString("GameSettings"); CurrentSettings = JsonConvert.DeserializeObject(json); } // 应用加载的设置 ApplyAudioSettings(); ApplyWindowSettings(); } // 应用音频设置 private void ApplyAudioSettings() { AudioListener.volume = Mathf.Clamp01(CurrentSettings.globalVolume); } // 应用窗口设置 private void ApplyWindowSettings() { switch (CurrentSettings.currentWindowMode) { case WindowMode.Fullscreen: Screen.SetResolution(CurrentSettings.windowResolution.x, CurrentSettings.windowResolution.y, FullScreenMode.FullScreenWindow); break; case WindowMode.Windowed: Screen.SetResolution(CurrentSettings.windowResolution.x, CurrentSettings.windowResolution.y, FullScreenMode.Windowed); break; case WindowMode.Borderless: Screen.SetResolution(CurrentSettings.windowResolution.x, CurrentSettings.windowResolution.y, FullScreenMode.MaximizedWindow); break; } } } }