DataConfMgr.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using PublicTools.XMLDataBase;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using XRTool.Util;
  6. namespace XRTool.UI
  7. {
  8. /// <summary>
  9. /// 数据访问单例
  10. /// </summary>
  11. /// <typeparam name="T"></typeparam>
  12. public class DataConfMgr<T> : Singleton<DataConfMgr<T>>
  13. {
  14. private TableHelper<T> tableHelper;
  15. public TableHelper<T> TableHelper { get => tableHelper; set => tableHelper = value; }
  16. public bool IsInit()
  17. {
  18. if (TableHelper == null)
  19. {
  20. return false;
  21. }
  22. return TableHelper.isInit;
  23. }
  24. /// <summary>
  25. /// 按照主键打开表格
  26. /// </summary>
  27. /// <param name="priKey"></param>
  28. public void OpenTable(string priKey = null)
  29. {
  30. TableHelper.Open(priKey);
  31. }
  32. /// <summary>
  33. /// 打开某路径下的资源
  34. /// </summary>
  35. /// <param name="path"></param>
  36. public void OpenData(string path, bool isForce = false)
  37. {
  38. if (TableHelper == null || isForce)
  39. {
  40. if (TableHelper != null)
  41. {
  42. TableHelper.Reset(!isForce);
  43. }
  44. TableHelper = new TableHelper<T>(path);
  45. }
  46. }
  47. /// <summary>
  48. /// 清除所有
  49. /// </summary>
  50. public void ClearAll()
  51. {
  52. TableHelper?.DelAll();
  53. }
  54. /// <summary>
  55. /// 删除某一数据
  56. /// </summary>
  57. /// <param name="key"></param>
  58. /// <param name="isSave"></param>
  59. public void DelConf(string key, bool isSave = true)
  60. {
  61. TableHelper?.DelConf(key, isSave);
  62. }
  63. /// <summary>
  64. /// 按照指定的key值查找
  65. /// </summary>
  66. /// <param name="key"></param>
  67. /// <returns></returns>
  68. public T Get(string key)
  69. {
  70. if (TableHelper == null)
  71. {
  72. return default(T);
  73. }
  74. return TableHelper.FindData(key);
  75. }
  76. /// <summary>
  77. /// 获取所有
  78. /// </summary>
  79. /// <returns></returns>
  80. public List<T> GetAll()
  81. {
  82. if (TableHelper == null)
  83. {
  84. return null;
  85. }
  86. return TableHelper.DataList;
  87. }
  88. /// <summary>
  89. /// 加入一条数据
  90. /// </summary>
  91. /// <param name="t"></param>
  92. /// <param name="isSave"></param>
  93. public void Insert(T t, bool isSave = true)
  94. {
  95. TableHelper.AddData(t, isSave);
  96. }
  97. public void Close()
  98. {
  99. TableHelper.Close();
  100. }
  101. public void Save()
  102. {
  103. TableHelper.SaveData();
  104. }
  105. }
  106. }