92 lines
2.6 KiB
C#
92 lines
2.6 KiB
C#
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using AibisDream.Framework;
|
|
using AibisDream.Kit;
|
|
using UnityEngine;
|
|
using UnityEngine.Localization.Settings;
|
|
|
|
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 LoadAllSetting()
|
|
{
|
|
var settingConfig = _container.ReadAll();
|
|
foreach (var pair in settingConfig)
|
|
{
|
|
OnSettingChanged(pair.Key, pair.Value);
|
|
}
|
|
}
|
|
|
|
private async void OnSettingChanged(string propName, string value)
|
|
{
|
|
// 配置项同步
|
|
switch (propName)
|
|
{
|
|
case "Volume":
|
|
AudioManager.Instance.SetVolume(float.Parse(value, CultureInfo.InvariantCulture));
|
|
break;
|
|
case "TextSpeed":
|
|
// TODO 调整文字速度
|
|
break;
|
|
case "Resolution":
|
|
// TODO 调整分辨率
|
|
break;
|
|
case "WindowMode":
|
|
// 屏幕模式
|
|
GameManager.Instance.SetScreenMode(value);
|
|
break;
|
|
case "Language":
|
|
// 确保本地化系统已初始化
|
|
await LocalizationSettings.InitializationOperation.Task;
|
|
LocalizationKit.SwitchLanguage(value);
|
|
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()
|
|
{
|
|
Instance = new SettingLoader();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |