EditorUser.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using Newtonsoft.Json;
  4. using UnityEditor;
  5. using UnityEngine;
  6. public static class EditorUser
  7. {
  8. [MenuItem("用户/Blue/自动定位", false)]
  9. static void BlueAutoImmersalSet()
  10. {
  11. CheckType();
  12. BlueUserListConfig[0].Auto = !BlueUserListConfig[0].Auto;
  13. string jsonData = JsonConvert.SerializeObject(BlueUserListConfig);
  14. WriteUserListConfig(jsonData);
  15. Menu.SetChecked("用户/Blue/自动定位", BlueUserListConfig[0].Auto);
  16. }
  17. [MenuItem("用户/Blue/文心一言", false)]
  18. static void BlueERNIEBotSet()
  19. {
  20. CheckType();
  21. BlueUserListConfig[0].ERNIEBot = !BlueUserListConfig[0].ERNIEBot;
  22. string jsonData = JsonConvert.SerializeObject(BlueUserListConfig);
  23. WriteUserListConfig(jsonData);
  24. Menu.SetChecked("用户/Blue/文心一言", BlueUserListConfig[0].ERNIEBot);
  25. }
  26. [MenuItem("用户/文创/自动定位", false)]
  27. static void WenChuangAutoImmersalSet()
  28. {
  29. CheckType();
  30. BlueUserListConfig[1].Auto = !BlueUserListConfig[1].Auto;
  31. string jsonData = JsonConvert.SerializeObject(BlueUserListConfig);
  32. WriteUserListConfig(jsonData);
  33. Menu.SetChecked("用户/文创/自动定位", BlueUserListConfig[1].Auto);
  34. }
  35. [MenuItem("用户/文创/文心一言", false)]
  36. static void WenChuangERNIEBotSet()
  37. {
  38. CheckType();
  39. BlueUserListConfig[1].ERNIEBot = !BlueUserListConfig[1].ERNIEBot;
  40. string jsonData = JsonConvert.SerializeObject(BlueUserListConfig);
  41. WriteUserListConfig(jsonData);
  42. Menu.SetChecked("用户/文创/文心一言", BlueUserListConfig[1].ERNIEBot);
  43. }
  44. [MenuItem("用户/凝元/自动定位", false)]
  45. static void NingYuanAutoImmersalSet()
  46. {
  47. CheckType();
  48. BlueUserListConfig[2].Auto = !BlueUserListConfig[2].Auto;
  49. string jsonData = JsonConvert.SerializeObject(BlueUserListConfig);
  50. WriteUserListConfig(jsonData);
  51. Menu.SetChecked("用户/凝元/自动定位", BlueUserListConfig[2].Auto);
  52. }
  53. [MenuItem("用户/凝元/文心一言", false)]
  54. static void NingYuanERNIEBotSet()
  55. {
  56. CheckType();
  57. BlueUserListConfig[2].ERNIEBot = !BlueUserListConfig[2].ERNIEBot;
  58. string jsonData = JsonConvert.SerializeObject(BlueUserListConfig);
  59. WriteUserListConfig(jsonData);
  60. Menu.SetChecked("用户/凝元/文心一言", BlueUserListConfig[2].ERNIEBot);
  61. }
  62. [MenuItem("用户/杨浦党建/自动定位", false)]
  63. static void YangPuDangJianAutoImmersalSet()
  64. {
  65. CheckType();
  66. BlueUserListConfig[3].Auto = !BlueUserListConfig[3].Auto;
  67. string jsonData = JsonConvert.SerializeObject(BlueUserListConfig);
  68. WriteUserListConfig(jsonData);
  69. Menu.SetChecked("用户/杨浦党建/自动定位", BlueUserListConfig[3].Auto);
  70. }
  71. [MenuItem("用户/杨浦党建/文心一言", false)]
  72. static void YangPuDangJianERNIEBotSet()
  73. {
  74. CheckType();
  75. BlueUserListConfig[3].ERNIEBot = !BlueUserListConfig[3].ERNIEBot;
  76. string jsonData = JsonConvert.SerializeObject(BlueUserListConfig);
  77. WriteUserListConfig(jsonData);
  78. Menu.SetChecked("用户/杨浦党建/文心一言", BlueUserListConfig[3].ERNIEBot);
  79. }
  80. private static void WriteUserListConfig(string UserName)
  81. {
  82. string folderPath = Application.dataPath + "/Resources";
  83. string filePath = folderPath + "/BlueUserListConfig.txt";
  84. if (!Directory.Exists(folderPath))
  85. Directory.CreateDirectory(folderPath);
  86. if (File.Exists(filePath))
  87. File.WriteAllText(filePath, UserName);
  88. else
  89. {
  90. FileStream file = new FileStream(filePath, FileMode.Create);
  91. byte[] bts = System.Text.Encoding.UTF8.GetBytes(UserName);
  92. file.Write(bts, 0, bts.Length);
  93. if (file != null)
  94. {
  95. file.Flush();
  96. file.Close();
  97. file.Dispose();
  98. }
  99. }
  100. AssetDatabase.Refresh();
  101. }
  102. private static BlueUserConfig mBlueUserConfig;
  103. private static void WriteUserConfig(BlueUserType blueUserType)
  104. {
  105. string filePathRes = Application.dataPath + "/Resources" + "/BlueUserConfig.txt";
  106. string filePathStr = Application.dataPath + "/StreamingAssets" + "/BlueUserConfig.txt";
  107. string filePathList = Application.dataPath + "/Resources" + "/BlueUserListConfig.txt";
  108. string jsonString = Resources.Load<TextAsset>("BlueUserListConfig").text;
  109. List<BlueUserConfig> BlueUserListConfig = JsonConvert.DeserializeObject<List<BlueUserConfig>>(jsonString);
  110. foreach(var BlueUserConfig in BlueUserListConfig)
  111. {
  112. if(BlueUserConfig.BlueUserType==blueUserType.ToString())
  113. {
  114. mBlueUserConfig = new BlueUserConfig()
  115. {
  116. BlueUserType = blueUserType.ToString(),
  117. Auto = BlueUserConfig.Auto,
  118. ERNIEBot = BlueUserConfig.ERNIEBot
  119. };
  120. }
  121. }
  122. string jsonData = JsonConvert.SerializeObject(mBlueUserConfig);
  123. if (File.Exists(filePathRes))
  124. File.WriteAllText(filePathRes, jsonData);
  125. else
  126. {
  127. FileStream file = new FileStream(filePathRes, FileMode.Create);
  128. byte[] bts = System.Text.Encoding.UTF8.GetBytes(jsonData);
  129. file.Write(bts, 0, bts.Length);
  130. if (file != null)
  131. {
  132. file.Flush();
  133. file.Close();
  134. file.Dispose();
  135. }
  136. }
  137. if (File.Exists(filePathStr))
  138. File.WriteAllText(filePathStr, jsonData);
  139. else
  140. {
  141. FileStream file = new FileStream(filePathStr, FileMode.Create);
  142. byte[] bts = System.Text.Encoding.UTF8.GetBytes(jsonData);
  143. file.Write(bts, 0, bts.Length);
  144. if (file != null)
  145. {
  146. file.Flush();
  147. file.Close();
  148. file.Dispose();
  149. }
  150. }
  151. AssetDatabase.Refresh();
  152. }
  153. private static List<BlueUserConfig> BlueUserListConfig;
  154. private static void CheckType()
  155. {
  156. TextAsset jsonTextAsset = Resources.Load("BlueUserListConfig") as TextAsset;
  157. if (jsonTextAsset != null)
  158. BlueUserListConfig = JsonConvert.DeserializeObject<List<BlueUserConfig>>(jsonTextAsset.text);
  159. else
  160. {
  161. var mBlueUserConfig = new BlueUserConfig()
  162. {
  163. BlueUserType = BlueUserType.Blue.ToString(),
  164. Auto = false,
  165. ERNIEBot = false
  166. };
  167. var mWCUserConfig = new BlueUserConfig()
  168. {
  169. BlueUserType = BlueUserType.WenChuang.ToString(),
  170. Auto = false,
  171. ERNIEBot = false
  172. };
  173. var mNYUserConfig = new BlueUserConfig()
  174. {
  175. BlueUserType = BlueUserType.NingYuan.ToString(),
  176. Auto = false,
  177. ERNIEBot = false
  178. };
  179. var mYPDJUserConfig = new BlueUserConfig()
  180. {
  181. BlueUserType = BlueUserType.YangPuDangJian.ToString(),
  182. Auto = false,
  183. ERNIEBot = false
  184. };
  185. BlueUserListConfig = new List<BlueUserConfig>()
  186. {
  187. mBlueUserConfig, // Blue
  188. mWCUserConfig, //文创
  189. mNYUserConfig, //凝元
  190. mYPDJUserConfig}; //杨浦党建
  191. }
  192. }
  193. #region
  194. private static BlueUserType BlueUserType = BlueUserType.DefaultUser;
  195. [MenuItem("用户/选择用户/Blue", false)]
  196. public static void SelectUserBlue()
  197. {
  198. BlueUserType = BlueUserType.Blue;
  199. Active();
  200. WriteUserConfig(BlueUserType.Blue);
  201. }
  202. [MenuItem("用户/选择用户/文创", false)]
  203. public static void SelectUserWC()
  204. {
  205. BlueUserType = BlueUserType.WenChuang;
  206. Active();
  207. WriteUserConfig(BlueUserType.WenChuang);
  208. }
  209. [MenuItem("用户/选择用户/凝元", false)]
  210. public static void SelectUserNY()
  211. {
  212. BlueUserType = BlueUserType.NingYuan;
  213. Active();
  214. WriteUserConfig(BlueUserType.NingYuan);
  215. }
  216. [MenuItem("用户/选择用户/杨浦党建", false)]
  217. public static void SelectUserYPDJ()
  218. {
  219. BlueUserType = BlueUserType.YangPuDangJian;
  220. Active();
  221. WriteUserConfig(BlueUserType.YangPuDangJian);
  222. }
  223. private static void Active()
  224. {
  225. Menu.SetChecked("用户/选择用户/Blue", BlueUserType == BlueUserType.Blue);
  226. Menu.SetChecked("用户/选择用户/文创", BlueUserType == BlueUserType.WenChuang);
  227. Menu.SetChecked("用户/选择用户/凝元", BlueUserType == BlueUserType.NingYuan);
  228. Menu.SetChecked("用户/选择用户/杨浦党建", BlueUserType == BlueUserType.YangPuDangJian);
  229. }
  230. #endregion
  231. }