第1步: 首先定义通用配置管理器接口与辅助类。
代码如下:
1 /*** 2 * 3 * Title: "SUIFW" UI框架项目 4 * 主题: 通用配置管理器接口 5 * Description: 6 * 功能: 7 * 基于“键值对”配置文件的通用解析 8 * 9 * Date: 2017 10 * Version: 0.1版本 11 * Modify Recoder: 12 * 13 * 14 */ 15 16 using System; 17 using System.Collections; 18 using System.Collections.Generic; 19 using UnityEngine; 20 21 namespace SUIFW 22 { 23 public interface IConfigManager { 24 25 /// <summary> 26 /// 只读属性: 应用设置 27 /// 功能: 得到键值对集合数据 28 /// </summary> 29 Dictionary<string, string> AppSetting { get; } 30 31 /// <summary> 32 /// 得到配置文件(AppSeting)最大的数量 33 /// </summary> 34 /// <returns></returns> 35 int GetAppSettingMaxNumber(); 36 37 } 38 39 [Serializable] 40 internal class KeyValuesInfo 41 { 42 //配置信息 43 public List<KeyValuesNode> ConfigInfo = null; 44 } 45 46 [Serializable] 47 internal class KeyValuesNode 48 { 49 //键 50 public string Key = null; 51 //值 52 public string Value = null; 53 } 54 } |
第2步: 定义Json 解析异常类。
Json 的解析过程如果出错,推荐使用我们自己定义的异常处理,为了更好的发现程序错误,所以自定义Json 解析异常类定义如下:
1 /*** 2 * 3 * Title: "SUIFW" UI框架项目 4 * 主题: Json 解析异常 5 * Description: 6 * 功能:专门负责对于JSon 由于路径错误,或者Json 格式错误造成的异常,进行捕获。 7 * 8 * Date: 2017 9 * Version: 0.1版本 10 * Modify Recoder: 11 * 12 * 13 */ 14 15 using System; 16 using System.Collections; 17 using System.Collections.Generic; 18 using UnityEngine; 19 20 namespace SUIFW 21 { 22 public class JsonAnlysisException : Exception { 23 public JsonAnlysisException() : base(){} 24 public JsonAnlysisException(string exceptionMessage) : base(exceptionMessage){} 25 } 26 } |
第3步:定义“配置管理器”类
开发实现IConfigManager 接口的通用配置管理器
1 /*** 2 * 3 * Title: "SUIFW" UI框架项目 4 * 主题:基于Json 配置文件的“配置管理器” 5 * Description: 6 * 功能: 7 * 8 * Date: 2017 9 * Version: 0.1版本 10 * Modify Recoder: 11 * 12 * 13 */ 14 15 using System; 16 using System.Collections; 17 using System.Collections.Generic; 18 using UnityEngine; 19 20 namespace SUIFW 21 { 22 public class ConfigManagerByJson : IConfigManager 23 { 24 //保存(键值对)应用设置集合 25 private static Dictionary<string, string> _AppSetting; 26 27 /// <summary> 28 /// 只读属性: 得到应用设置(键值对集合) 29 /// </summary> 30 public Dictionary<string, string> AppSetting 31 { 32 get { return _AppSetting; } 33 } 34 35 /// <summary> 36 /// 构造函数 37 /// </summary> 38 /// <param name="jsonPath">Json配置文件路径</param> 39 public ConfigManagerByJson(string jsonPath) 40 { 41 _AppSetting=new Dictionary<string, string>(); 42 //初始化解析Json 数据,加载到(_AppSetting)集合。 43 InitAndAnalysisJson(jsonPath); 44 } 45 46 /// <summary> 47 /// 得到AppSetting 的最大数值 48 /// </summary> 49 /// <returns></returns> 50 public int GetAppSettingMaxNumber() 51 { 52 if (_AppSetting!=null && _AppSetting.Count>=1) 53 { 54 return _AppSetting.Count; 55 } 56 else 57 { 58 return 0; 59 } 60 } 61 62 /// <summary> 63 /// 初始化解析Json 数据,加载到集合众。 64 /// </summary> 65 /// <param name="jsonPath"></param> 66 private void InitAndAnalysisJson(string jsonPath) 67 { 68 TextAsset configInfo = null; 69 KeyValuesInfo keyvalueInfoObj = null; 70 71 //参数检查 72 if (string.IsNullOrEmpty(jsonPath)) return; 73 //解析Json 配置文件 74 try{ 75 configInfo = Resources.Load<TextAsset>(jsonPath); 76 keyvalueInfoObj=JsonUtility.FromJson<KeyValuesInfo>(configInfo.text); 77 } 78 catch{ 79 throw new JsonAnlysisException(GetType() + "/InitAndAnalysisJson()/Json Analysis Exception ! Parameter jsonPath=" + jsonPath); 80 } 81 //数据加载到AppSetting 集合中 82 foreach (KeyValuesNode nodeInfo in keyvalueInfoObj.ConfigInfo) 83 { 84 _AppSetting.Add(nodeInfo.Key,nodeInfo.Value); 85 } 86 } 87 88 89 90 } 91 } |
代码说明:
以上定义的“配置管理器”,可以对所有具备“键值对”特性的Json 配置文件,做统一数据提取工作,从而对于“UI预设”、“游戏对象”、“日志配置文件”、“语言国际化”等信息,可以做统一处理,极大提供开发效率。以下笔者提供本UI框架需要用到的除企业日志系统外,其他两种配置文件的截图,供参考。
(企业级Log日志中使用到的配置信息)
(“UI预设” 路径信息Json配置文件)
上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。