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