107 lines
3.5 KiB
C#
107 lines
3.5 KiB
C#
using System.IO;
|
||
using UnityEngine;
|
||
|
||
namespace BaseGames.Core
|
||
{
|
||
/// <summary>
|
||
/// 全局设置管理器。从 GlobalSettingsSO 读取默认值,从文件加载用户覆盖。
|
||
/// </summary>
|
||
[DefaultExecutionOrder(-800)]
|
||
public class SettingsManager : MonoBehaviour, ISettingsService
|
||
{
|
||
private const string SettingsFileName = "settings.json";
|
||
|
||
[SerializeField] private GlobalSettingsSO _defaultSettings;
|
||
|
||
private GlobalSettingsData _current;
|
||
private string SettingsFilePath => Path.Combine(Application.persistentDataPath, SettingsFileName);
|
||
|
||
public GlobalSettingsData Current => _current;
|
||
|
||
private void Awake()
|
||
{
|
||
ServiceLocator.Register<ISettingsService>(this);
|
||
}
|
||
|
||
/// <summary>由 GameManager.Awake 调用。读取设置文件,应用音量/分辨率。</summary>
|
||
public void Initialize()
|
||
{
|
||
_current = Load() ?? _defaultSettings?.CreateDefault() ?? new GlobalSettingsData();
|
||
Apply(_current);
|
||
}
|
||
|
||
private GlobalSettingsData Load()
|
||
{
|
||
if (!File.Exists(SettingsFilePath)) return null;
|
||
try
|
||
{
|
||
var json = File.ReadAllText(SettingsFilePath);
|
||
return JsonUtility.FromJson<GlobalSettingsData>(json);
|
||
}
|
||
catch
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
|
||
public void Save()
|
||
{
|
||
try
|
||
{
|
||
var json = JsonUtility.ToJson(_current, true);
|
||
File.WriteAllText(SettingsFilePath, json);
|
||
}
|
||
catch (System.Exception e)
|
||
{
|
||
Debug.LogWarning($"[SettingsManager] 设置保存失败: {e.Message}");
|
||
}
|
||
}
|
||
|
||
private void Apply(GlobalSettingsData data)
|
||
{
|
||
QualitySettings.vSyncCount = data.VSync ? 1 : 0;
|
||
if (!data.VSync) Application.targetFrameRate = data.TargetFPS;
|
||
|
||
if (data.FullScreen)
|
||
Screen.fullScreenMode = FullScreenMode.FullScreenWindow;
|
||
}
|
||
|
||
// ── 音量设置(调用 AudioManager)────────────────────
|
||
public void SetMasterVolume(float v) { _current.MasterVolume = v; Save(); }
|
||
public void SetBGMVolume(float v) { _current.BGMVolume = v; Save(); }
|
||
public void SetSFXVolume(float v) { _current.SFXVolume = v; Save(); }
|
||
public void SetAmbientVolume(float v) { _current.AmbientVolume = v; Save(); }
|
||
|
||
// ── 画面设置 ──────────────────────────────────────────────────────
|
||
public void SetResolution(int w, int h, FullScreenMode mode)
|
||
{
|
||
Screen.SetResolution(w, h, mode);
|
||
_current.FullScreen = (mode != FullScreenMode.Windowed);
|
||
Save();
|
||
}
|
||
public void SetVSync(bool enabled)
|
||
{
|
||
_current.VSync = enabled;
|
||
QualitySettings.vSyncCount = enabled ? 1 : 0;
|
||
Save();
|
||
}
|
||
public void SetTargetFrameRate(int fps)
|
||
{
|
||
_current.TargetFPS = fps;
|
||
if (!_current.VSync) Application.targetFrameRate = fps;
|
||
Save();
|
||
}
|
||
|
||
public void SetLanguage(string localeCode)
|
||
{
|
||
_current.Language = localeCode;
|
||
Save();
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
ServiceLocator.Unregister<ISettingsService>(this);
|
||
}
|
||
}
|
||
}
|