109 lines
3.3 KiB
C#
109 lines
3.3 KiB
C#
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using AibisDream.Framework;
|
|
using AibisDream.Kit;
|
|
using AibisDream.UI;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream
|
|
{
|
|
/// <summary>
|
|
/// 用于规定配置如何影响游戏内容
|
|
/// </summary>
|
|
public class SettingLoader
|
|
{
|
|
private static readonly string SettingPath = Application.streamingAssetsPath + "/Config/setting.json";
|
|
|
|
private readonly ConfigContainer _container;
|
|
|
|
private SettingLoader()
|
|
{
|
|
// 读取数据
|
|
_container = new ConfigContainer(SettingPath);
|
|
LoadAllSetting();
|
|
// 注册回调函数
|
|
_container.OnWrite += OnSettingChanged;
|
|
}
|
|
|
|
private void Dispose()
|
|
{
|
|
_container.OnWrite -= OnSettingChanged;
|
|
}
|
|
|
|
private void LoadAllSetting()
|
|
{
|
|
var settingConfig = _container.ReadAll();
|
|
foreach (var pair in settingConfig)
|
|
{
|
|
OnSettingChanged(pair.Key, pair.Value);
|
|
}
|
|
}
|
|
|
|
private void OnSettingChanged(string propName, string value)
|
|
{
|
|
// 配置项同步
|
|
switch (propName)
|
|
{
|
|
case "Volume":
|
|
AudioManager.Instance.SetVolume(float.Parse(value, CultureInfo.InvariantCulture));
|
|
break;
|
|
case "TextSpeed":
|
|
EnumEventSystem.Global.Send(SettingChangeEvent.TextSpeed, value);
|
|
break;
|
|
case "Resolution":
|
|
// TODO 调整分辨率
|
|
break;
|
|
case "WindowMode":
|
|
// 屏幕模式
|
|
GameManager.Instance.SetScreenMode(value);
|
|
break;
|
|
case "Language":
|
|
// 确保本地化系统已初始化
|
|
LocalizationKit.SwitchLanguage(value);
|
|
break;
|
|
case "OpenTimer":
|
|
var isTimerOpen = bool.TryParse(value, out var result) ? result : false;
|
|
UIManager.Instance.GetPanel<InfoPanel>().SetTimerActive(isTimerOpen);
|
|
break;
|
|
case "EndTime":
|
|
int endTime = int.TryParse(value, NumberStyles.None, CultureInfo.InvariantCulture, out var endTime1) ? endTime1 : 1200;
|
|
UIManager.Instance.GetPanel<InfoPanel>().SetTimerDuration(endTime);
|
|
break;
|
|
default:
|
|
Debug.Log($"{propName}未正确处理");
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void Write(string propName, string value)
|
|
{
|
|
_container.Write(propName, value);
|
|
}
|
|
|
|
public bool TryRead(string key, out string value)
|
|
{
|
|
return _container.TryRead(key, out value);
|
|
}
|
|
|
|
public Dictionary<string, string> ReadAll()
|
|
{
|
|
return _container.ReadAll();
|
|
}
|
|
|
|
#region 单例部分
|
|
|
|
public static SettingLoader Instance { get; private set; }
|
|
|
|
public static void Init()
|
|
{
|
|
if (Instance != null)
|
|
{
|
|
Instance.Dispose();
|
|
Instance = null;
|
|
}
|
|
Instance = new SettingLoader();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |