ConfigManagerByJson.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /***
  2. *
  3. * Title: "SUIFW" UI框架项目
  4. * 主题: 配置管理器
  5. * Description:
  6. * 功能: 对于Json(核心数据)配置文件的读取处理。
  7. * Date: 2017
  8. * Version: 0.1版本
  9. * Modify Recoder:
  10. *
  11. *
  12. */
  13. using System;
  14. using System.Collections.Generic;
  15. using UnityEngine;
  16. namespace SUIFW
  17. {
  18. public class ConfigManagerByJson:IConfigManager
  19. {
  20. //定义应用设置集合
  21. private static Dictionary<string, string> _AppSetting;
  22. /// <summary>
  23. /// 属性: 应用设置
  24. /// </summary>
  25. public Dictionary<string, string> AppSetting
  26. {
  27. get { return _AppSetting; }
  28. }
  29. /// <summary>
  30. /// 构造函数
  31. /// </summary>
  32. /// <param name="JsonPath">Json文件路径</param>
  33. public ConfigManagerByJson(string JsonPath)
  34. {
  35. _AppSetting = new Dictionary<string, string>();
  36. //初始化解析Json数据,到集合中
  37. InitAndAnalysisJson(JsonPath);
  38. }
  39. /// <summary>
  40. /// 初始化解析XML数据,到集合中(_AppSetting)
  41. /// </summary>
  42. /// <param name="JsonPath">Json的路径</param>
  43. private void InitAndAnalysisJson(string JsonPath)
  44. {
  45. TextAsset configInfo = null;
  46. KeyValuesInfo keyValuesInfoObj = null;
  47. //参数检查
  48. if (string.IsNullOrEmpty(JsonPath)) return;
  49. try
  50. {
  51. configInfo = Resources.Load<TextAsset>(JsonPath);
  52. keyValuesInfoObj = JsonUtility.FromJson<KeyValuesInfo>(configInfo.text);
  53. }
  54. catch
  55. {
  56. throw new JsonAnalysisException(GetType() + "/InitAndAnalysisJson()/Json Analysis Exception! ,please Check Json file Or Json file Path! Parameter JsonPath= " + JsonPath);
  57. }
  58. foreach (KeyValuesNode node in keyValuesInfoObj.ConfigInfo)
  59. {
  60. _AppSetting.Add(node.Key,node.Value);
  61. }
  62. }
  63. /// <summary>
  64. /// 得到AppSetting的最大数量
  65. /// </summary>
  66. public int GetAppSettingMaxNumber()
  67. {
  68. if (_AppSetting != null && _AppSetting.Count >= 1)
  69. {
  70. return _AppSetting.Count;
  71. }
  72. else
  73. {
  74. return 0;
  75. }
  76. }
  77. }
  78. }