SystemSettingMgr.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using Newtonsoft.Json;
  2. using PublicTools.XMLDataBase;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace XRTool.Util
  7. {
  8. /// <summary>
  9. /// 读取系统配置
  10. /// 系统配置是json文件,默认会放在steammingpath下面
  11. /// 应用发布时会生成到sd卡目录或者可读写文件下面
  12. /// </summary>
  13. public class SystemSettingMgr : Singleton<SystemSettingMgr>
  14. {
  15. private TableInterface table;
  16. public SystemSettings settings;
  17. public SystemSettingMgr()
  18. {
  19. InitSettings();
  20. }
  21. /// <summary>
  22. /// 实例化Dlg窗口
  23. /// </summary>
  24. public void InstanceDlg()
  25. {
  26. GameObject dlg = ResourcesManager.Instance.DataLoader.Load("SystemSetDlg") as GameObject;
  27. if (dlg)
  28. {
  29. GameObject obj = GameObject.Instantiate(dlg) as GameObject;
  30. GameNode.Instance.SetParent(ObjNode.WorldCanvas, obj.transform, new Vector3(0, 0, 2), Vector3.zero, new Vector3(0.001f, 0.001f, 0.001f));
  31. }
  32. }
  33. public void OpenSql()
  34. {
  35. if (XSql.Instance.Open(Application.persistentDataPath))
  36. {
  37. table = XSql.Instance.OpenTable(typeof(SystemSettings).Name, ".xml", true);
  38. if (!table.Open())
  39. {
  40. table.Create(typeof(SystemSettings).Name);
  41. }
  42. ReadSetting();
  43. }
  44. else
  45. {
  46. UnityLog.Instance.LogError("数据库打开失败,请检查权限");
  47. }
  48. }
  49. public SystemSettings ReadSetting()
  50. {
  51. settings = table.FindData<SystemSettings>("SystemSetting");
  52. if (settings == null)
  53. {
  54. settings = new SystemSettings();
  55. table.InsertData(settings);
  56. table.Save();
  57. }
  58. return settings;
  59. }
  60. /// <summary>
  61. /// 获取配置
  62. /// </summary>
  63. public void InitSettings()
  64. {
  65. if (table == null)
  66. {
  67. OpenSql();
  68. }
  69. else
  70. {
  71. ReadSetting();
  72. }
  73. SetLogLevel();
  74. }
  75. private void SetLogLevel()
  76. {
  77. #if !UNITY_EDITOR
  78. if (settings != null)
  79. {
  80. UnityLog.Instance.logLevel = settings.LogLevel;
  81. }
  82. #endif
  83. }
  84. public void SaveSettings()
  85. {
  86. if (table == null)
  87. {
  88. OpenSql();
  89. if (table != null)
  90. {
  91. table.UpdateData(this.settings);
  92. table.Save();
  93. }
  94. }
  95. else
  96. {
  97. UnityLog.Instance.Log(JsonConvert.SerializeObject(this.settings));
  98. bool isChange = table.UpdateData(this.settings);
  99. UnityLog.Instance.Log(isChange);
  100. table.Save();
  101. UnityLog.Instance.Log(JsonConvert.SerializeObject(this.settings));
  102. UnityLog.Instance.Log(settings.IfOpenFPS);
  103. }
  104. SetLogLevel();
  105. }
  106. public void SetLogLevel(int level)
  107. {
  108. if (this.settings != null)
  109. {
  110. this.settings.LogLevel = level;
  111. SaveSettings();
  112. }
  113. }
  114. public void SetBoolFPS(bool ifOpenFPS)
  115. {
  116. if (this.settings != null)
  117. {
  118. this.settings.IfOpenFPS = ifOpenFPS;
  119. SaveSettings();
  120. }
  121. }
  122. public void SetBoolLogText(bool ifOpenLog)
  123. {
  124. if (this.settings != null)
  125. {
  126. this.settings.IfOpenLog = ifOpenLog;
  127. SaveSettings();
  128. }
  129. }
  130. public void SetQuilty(int level)
  131. {
  132. if (this.settings != null)
  133. {
  134. QualitySettings.SetQualityLevel(level);
  135. settings.QualityLevel = level;
  136. SaveSettings();
  137. }
  138. }
  139. public void SetShadowType(int shadowType)
  140. {
  141. if (this.settings != null)
  142. {
  143. settings.ShadowType = shadowType;
  144. SaveSettings();
  145. }
  146. }
  147. public void ChangeIPAddress(string ip)
  148. {
  149. if (this.settings != null)
  150. {
  151. settings.Ip = ip;
  152. SaveSettings();
  153. }
  154. }
  155. public void ChangePort(string port)
  156. {
  157. if (this.settings != null)
  158. {
  159. settings.Port = port;
  160. SaveSettings();
  161. }
  162. }
  163. }
  164. }