Files
Gen_Hack-and-Slash-Roguelite/Client/Assets/Scripts/Base/Setting.cs

108 lines
3.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Setting.cs
using Newtonsoft.Json;
using UnityEngine;
namespace Base
{
public class Setting : Utils.Singleton<Setting>
{
// 游戏设置数据类(用于序列化)
[System.Serializable]
public class GameSettings
{
public float progressStepDuration = 0.5f;
public float exitAnimationDuration = 1f;
public bool developerMode = false; // 默认值仍为 false在编辑器中会被覆盖
public bool friendlyFire = false;
public bool showMiniMap = true;
public float globalVolume = 1.0f;
public WindowMode currentWindowMode = WindowMode.Fullscreen;
public Vector2Int windowResolution = new(1920, 1080);
public string[] loadOrder;
public bool showHealthBarByHit = true;
public bool alwaysShowHealthBar = false;
public bool showHitNumber = true;
}
// 当前游戏设置
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()
{
var json = JsonConvert.SerializeObject(CurrentSettings);
PlayerPrefs.SetString("GameSettings", json);
PlayerPrefs.Save();
}
public void LoadSettings()
{
if (PlayerPrefs.HasKey("GameSettings"))
{
var json = PlayerPrefs.GetString("GameSettings");
CurrentSettings = JsonConvert.DeserializeObject<GameSettings>(json);
}
#if UNITY_EDITOR
CurrentSettings.developerMode = true;
#endif
// 应用加载的设置
ApplyAudioSettings();
ApplyWindowSettings();
}
public void Apply()
{
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;
}
}
}
}