PcConfig.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*******************************************************************************
  2. Copyright © 2015-2022 PICO Technology Co., Ltd.All rights reserved.
  3. NOTICE:All information contained herein is, and remains the property of
  4. PICO Technology Co., Ltd. The intellectual and technical concepts
  5. contained herein are proprietary to PICO Technology Co., Ltd. and may be
  6. covered by patents, patents in process, and are protected by trade secret or
  7. copyright law. Dissemination of this information or reproduction of this
  8. material is strictly forbidden unless prior written permission is obtained from
  9. PICO Technology Co., Ltd.
  10. *******************************************************************************/
  11. using System;
  12. using System.IO;
  13. using LitJson;
  14. using UnityEditor;
  15. using UnityEngine;
  16. namespace Pico.Platform.Editor
  17. {
  18. /// <summary>
  19. /// Unity Setting Getter and Setter
  20. /// </summary>
  21. public enum Region
  22. {
  23. cn = 0,
  24. i18n = 1,
  25. }
  26. public class PcConfig : ScriptableObject
  27. {
  28. public Region region = Region.cn;
  29. public string accessToken = "";
  30. internal bool hasError = false;
  31. }
  32. [CustomEditor(typeof(PcConfig))]
  33. public class PcConfigEditor : UnityEditor.Editor
  34. {
  35. static string filepath = "Assets/Resources/PicoSdkPCConfig.json";
  36. private static string i18nLink = "https://developer-global.pico-interactive.com/document/unity/pc-end-debugging-tool";
  37. private static string cnLink = "https://developer-cn.pico-interactive.com/document/unity/pc-end-debugging-tool";
  38. public override void OnInspectorGUI()
  39. {
  40. var x = Selection.activeObject as PcConfig;
  41. if (x.hasError)
  42. {
  43. EditorGUILayout.LabelField("Config file error,please check the file");
  44. return;
  45. }
  46. base.OnInspectorGUI();
  47. //Read the document
  48. {
  49. GUILayout.Space(5);
  50. var referenceStyle = new GUIStyle(EditorStyles.label);
  51. referenceStyle.normal.textColor = new Color(0, 122f / 255f, 204f / 255f);
  52. referenceStyle.focused.textColor = new Color(0, 122f / 255f, 204f / 255f);
  53. referenceStyle.hover.textColor = new Color(0, 122f / 255f, 204f / 255f);
  54. if (GUILayout.Button("Read the document", referenceStyle))
  55. {
  56. var link = i18nLink;
  57. if (Application.systemLanguage == SystemLanguage.Chinese || Application.systemLanguage == SystemLanguage.ChineseSimplified || Application.systemLanguage == SystemLanguage.ChineseTraditional)
  58. {
  59. link = cnLink;
  60. }
  61. Application.OpenURL(link);
  62. }
  63. }
  64. this.save();
  65. }
  66. public static PcConfig load()
  67. {
  68. var obj = CreateInstance<PcConfig>();
  69. obj.hasError = false;
  70. try
  71. {
  72. if (File.Exists(filepath))
  73. {
  74. var jsonContent = File.ReadAllText(filepath);
  75. var jsonConf = JsonMapper.ToObject(jsonContent);
  76. obj.accessToken = jsonConf["account"]["access_token"].ToString();
  77. if (!Region.TryParse(jsonConf["general"]["region"].ToString() ?? "", out obj.region))
  78. {
  79. obj.region = Region.cn;
  80. }
  81. }
  82. }
  83. catch (Exception e)
  84. {
  85. Debug.LogError(e);
  86. obj.hasError = true;
  87. }
  88. return obj;
  89. }
  90. public void save()
  91. {
  92. var obj = Selection.activeObject as PcConfig;
  93. if (obj.hasError)
  94. {
  95. return;
  96. }
  97. var conf = new JsonData();
  98. conf["general"] = new JsonData();
  99. conf["account"] = new JsonData();
  100. conf["package"] = new JsonData();
  101. conf["general"]["region"] = obj.region.ToString();
  102. conf["account"]["access_token"] = obj.accessToken.Trim();
  103. conf["package"]["package_name"] = Gs.packageName.Trim();
  104. conf["package"]["package_version_code"] = Gs.bundleVersionCode;
  105. conf["package"]["package_version_name"] = Gs.bundleVersion;
  106. File.WriteAllText(filepath, JsonMapper.ToJson(conf));
  107. }
  108. }
  109. }