DataManager.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System;
  4. /*
  5. * 数据管理器,只读,可热更新,可使用默认值
  6. * 通过ResourceManager加载
  7. * */
  8. public class DataManager
  9. {
  10. public const string c_directoryName = "Data";
  11. public const string c_expandName = "txt";
  12. /// <summary>
  13. /// 数据缓存
  14. /// </summary>
  15. static Dictionary<string, DataTable> s_dataCache = new Dictionary<string, DataTable>();
  16. public static bool GetIsExistData(string DataName)
  17. {
  18. return ResourcesConfigManager.GetIsExitRes(DataName);
  19. }
  20. public static DataTable GetData(string DataName)
  21. {
  22. try
  23. {
  24. //编辑器下不处理缓存
  25. if (s_dataCache.ContainsKey(DataName))
  26. {
  27. return s_dataCache[DataName];
  28. }
  29. DataTable data = null;
  30. string dataJson = "";
  31. if (Application.isPlaying)
  32. {
  33. dataJson = AssetsPoolManager.ReadTextFile(DataName);
  34. }
  35. else
  36. {
  37. dataJson = ResourceIOTool.ReadStringByResource(
  38. PathTool.GetRelativelyPath(c_directoryName,
  39. DataName,
  40. c_expandName));
  41. }
  42. if (dataJson == "")
  43. {
  44. throw new Exception("Dont Find ->" + DataName + "<-");
  45. }
  46. data = DataTable.Analysis(dataJson);
  47. data.m_tableName = DataName;
  48. s_dataCache.Add(DataName, data);
  49. return data;
  50. }
  51. catch (Exception e)
  52. {
  53. throw new Exception("GetData Exception ->" + DataName + "<- : " + e.ToString());
  54. }
  55. }
  56. /// <summary>
  57. /// 清除缓存
  58. /// </summary>
  59. public static void CleanCache()
  60. {
  61. s_dataCache.Clear();
  62. }
  63. }