157 lines
4.6 KiB
C#
157 lines
4.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using AibisDream.Utility;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream.Kit
|
|
{
|
|
public class ConfigUtil : SingletonBase<ConfigUtil>
|
|
{
|
|
private const string CharacterConfigPath = "/Config/character.csv";
|
|
|
|
private Dictionary<string, Character> _characters;
|
|
|
|
/// <summary>
|
|
/// 用于单例模式
|
|
/// </summary>
|
|
private ConfigUtil()
|
|
{
|
|
#if UNITY_EDITOR
|
|
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
|
|
#endif
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
private void OnPlayModeStateChanged(PlayModeStateChange state)
|
|
{
|
|
OnSingletonInit();
|
|
}
|
|
#endif
|
|
|
|
/// <summary>
|
|
/// 调用时初始化
|
|
/// </summary>
|
|
public override void OnSingletonInit()
|
|
{
|
|
InitCharacterConfig();
|
|
}
|
|
|
|
private void InitCharacterConfig()
|
|
{
|
|
var characterList = CsvUtil.ReadAsBean<Character>(Application.streamingAssetsPath + CharacterConfigPath);
|
|
_characters = characterList
|
|
.Where(item => !string.IsNullOrWhiteSpace(item.Key))
|
|
.ToDictionary(item => item.Key, item => item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 按key获取角色预设
|
|
/// </summary>
|
|
/// <param name="key">key</param>
|
|
/// <param name="character">角色配置</param>
|
|
/// <returns>是否能获取</returns>
|
|
public bool TryGetCharacter(string key, out Character character)
|
|
{
|
|
return _characters.TryGetValue(key, out character);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 按类型读取Json配置
|
|
/// </summary>
|
|
/// <param name="path">路径</param>
|
|
/// <param name="key">key</param>
|
|
/// <param name="res">结果</param>
|
|
/// <typeparam name="T">类型</typeparam>
|
|
/// <returns>是否成功</returns>
|
|
public bool TryLoadConfig<T>(string path, string key, out T res)
|
|
{
|
|
try
|
|
{
|
|
res = JsonUtil.ReadBean<T>(path, key);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
res = default;
|
|
Debug.Log(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 按类型读取
|
|
/// </summary>
|
|
/// <param name="path">路径</param>
|
|
/// <param name="res">结果</param>
|
|
/// <typeparam name="T">类型</typeparam>
|
|
/// <returns>是否成功</returns>
|
|
public bool TryLoadConfig<T>(string path, out T res)
|
|
{
|
|
try
|
|
{
|
|
string formattedKey = JsonUtil.FormatAsJsonPath(path);
|
|
res = JsonUtil.ReadBean<T>(formattedKey);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
res = default;
|
|
Debug.Log(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存Bean
|
|
/// </summary>
|
|
/// <param name="bean"></param>
|
|
/// <param name="path"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
public void SaveConfig<T>(T bean, string path)
|
|
{
|
|
var jsonPath = JsonUtil.FormatAsJsonPath(path);
|
|
JsonUtil.SaveBean(bean, jsonPath);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存配置
|
|
/// </summary>
|
|
/// <param name="bean">bean</param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="path">路径</param>
|
|
/// <param name="saveMode">保存类型</param>
|
|
/// <typeparam name="T">类型</typeparam>
|
|
public void SaveConfig<T>(T bean, string key, string path, SaveMode saveMode = SaveMode.Add)
|
|
{
|
|
JsonUtil.SaveBean(bean, key, path, saveMode);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询某个文件夹下的所有文件
|
|
/// </summary>
|
|
/// <param name="folderPath">
|
|
/// </param>
|
|
/// <returns></returns>
|
|
public string[] GetFileNames(string folderPath)
|
|
{
|
|
if (!Directory.Exists(folderPath))
|
|
{
|
|
return Array.Empty<string>(); // 如果目录不存在,返回空数组
|
|
}
|
|
|
|
// 获取所有 .json 文件的完整路径
|
|
string[] filePaths = Directory.GetFiles(folderPath, "*.json");
|
|
|
|
// 提取文件名(不包括路径)
|
|
string[] fileNames = filePaths.Select(Path.GetFileName).ToArray();
|
|
|
|
return fileNames;
|
|
}
|
|
}
|
|
}
|