AppSettingsWindow.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. public sealed class AppSettingsWindow : EditorWindow
  6. {
  7. private const string SettingsPath = "Assets/AppRuntimes/Resources/AppSettings.asset";
  8. private AppSettings settings;
  9. private SerializedObject facebook;
  10. private int appIndex;
  11. private string appName, appId, clientToken;
  12. private string appAddress, appChannel, privacyUrl;
  13. private string addressError;
  14. private Vector2 scrollPosition;
  15. private bool showTestUrlHelp = true;
  16. private bool showUsageHelp = true;
  17. private const string TestUrlExample = @"<key>TestUserID</key>
  18. <string>https://example.com/test/</string>";
  19. private const string UsageExample = @"using UnityEngine;
  20. public class AppStartup : MonoBehaviour
  21. {
  22. private async void Start()
  23. {
  24. AppRuntimes.Initialization();
  25. if (await AppRuntimes.CheckVersion())
  26. {
  27. // 已打开平台页面,停止游戏背景音乐。
  28. // 在这里调用项目的 BGM 停止方法。
  29. return;
  30. }
  31. // 未打开平台页面,继续游戏主流程。
  32. }
  33. }";
  34. [MenuItem("AppSettings/Open Settings")]
  35. public static void Open()
  36. {
  37. var window = GetWindow<AppSettingsWindow>("AppSettings");
  38. window.minSize = new Vector2(560, 420);
  39. window.LoadSettings();
  40. window.Show();
  41. }
  42. private void OnEnable() => LoadSettings();
  43. private void LoadSettings()
  44. {
  45. settings = Resources.Load<AppSettings>("AppSettings");
  46. if (settings == null)
  47. {
  48. if (!AssetDatabase.IsValidFolder("Assets/AppRuntimes/Resources"))
  49. AssetDatabase.CreateFolder("Assets/AppRuntimes", "Resources");
  50. settings = CreateInstance<AppSettings>();
  51. AssetDatabase.CreateAsset(settings, SettingsPath);
  52. AssetDatabase.SaveAssets();
  53. }
  54. addressError = null;
  55. try
  56. {
  57. appAddress = string.IsNullOrEmpty(settings.AppAddress)
  58. ? string.Empty : ClientCrypto.DecryptString(settings.AppAddress);
  59. }
  60. catch (Exception)
  61. {
  62. appAddress = string.Empty;
  63. addressError = "AppAddress 解密失败,请重新填写明文地址后保存。";
  64. }
  65. appChannel = settings.AppChannel;
  66. privacyUrl = settings.PrivacyUrl;
  67. // Read the SDK's active application, preserving other configured apps.
  68. var facebookAsset = Resources.Load<ScriptableObject>("FacebookSettings");
  69. facebook = facebookAsset != null ? new SerializedObject(facebookAsset) : null;
  70. appName = appId = clientToken = string.Empty;
  71. if (facebook == null) return;
  72. facebook.Update();
  73. appIndex = Mathf.Max(0, facebook.FindProperty("selectedAppIndex").intValue);
  74. appName = ReadFacebookValue("appLabels");
  75. appId = ReadFacebookValue("appIds");
  76. clientToken = ReadFacebookValue("clientTokens");
  77. }
  78. private string ReadFacebookValue(string name)
  79. {
  80. var property = facebook.FindProperty(name);
  81. return appIndex < property.arraySize
  82. ? property.GetArrayElementAtIndex(appIndex).stringValue : string.Empty;
  83. }
  84. private void WriteFacebookValue(string name, string value)
  85. {
  86. var property = facebook.FindProperty(name);
  87. while (property.arraySize <= appIndex)
  88. {
  89. var index = property.arraySize;
  90. property.arraySize++;
  91. property.GetArrayElementAtIndex(index).stringValue = string.Empty;
  92. }
  93. property.GetArrayElementAtIndex(appIndex).stringValue = value;
  94. }
  95. private void OnGUI()
  96. {
  97. using (new FontSizeScope(2))
  98. DrawWindow();
  99. }
  100. private void DrawWindow()
  101. {
  102. scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
  103. EditorGUILayout.LabelField("Facebook", EditorStyles.boldLabel);
  104. if (facebook == null)
  105. EditorGUILayout.HelpBox("FacebookSettings.asset was not found in Resources.", MessageType.Error);
  106. using (new EditorGUI.DisabledScope(facebook == null))
  107. {
  108. appName = EditorGUILayout.TextField("Facebook AppName", appName);
  109. appId = EditorGUILayout.TextField("Facebook AppID", appId);
  110. clientToken = EditorGUILayout.TextField("Client Token", clientToken);
  111. }
  112. EditorGUILayout.Space();
  113. EditorGUILayout.LabelField("App", EditorStyles.boldLabel);
  114. EditorGUI.BeginChangeCheck();
  115. appAddress = EditorGUILayout.TextField(new GUIContent("AppAddress", "填写明文地址,保存时自动加密。"), appAddress);
  116. if (EditorGUI.EndChangeCheck()) addressError = null;
  117. if (!string.IsNullOrEmpty(addressError))
  118. EditorGUILayout.HelpBox(addressError, MessageType.Error);
  119. appChannel = EditorGUILayout.TextField("AppChannel", appChannel);
  120. privacyUrl = EditorGUILayout.TextField("PrivacyUrl", privacyUrl);
  121. EditorGUILayout.Space();
  122. using (new EditorGUILayout.HorizontalScope())
  123. {
  124. if (GUILayout.Button("Reload")) LoadSettings();
  125. if (GUILayout.Button("Save")) SaveSettings();
  126. }
  127. DrawUsageHelp();
  128. EditorGUILayout.EndScrollView();
  129. }
  130. // Restore shared editor styles after drawing this window.
  131. private sealed class FontSizeScope : IDisposable
  132. {
  133. private readonly Dictionary<GUIStyle, int> originalSizes = new Dictionary<GUIStyle, int>();
  134. internal FontSizeScope(int increase)
  135. {
  136. var styles = new[]
  137. {
  138. EditorStyles.label, EditorStyles.boldLabel, EditorStyles.textField,
  139. EditorStyles.foldout, EditorStyles.helpBox, EditorStyles.textArea,
  140. GUI.skin.button
  141. };
  142. foreach (var style in styles)
  143. {
  144. if (originalSizes.ContainsKey(style)) continue;
  145. originalSizes.Add(style, style.fontSize);
  146. var defaultSize = style.font != null && style.font.fontSize > 0
  147. ? style.font.fontSize : 12;
  148. style.fontSize = (style.fontSize > 0 ? style.fontSize : defaultSize) + increase;
  149. }
  150. }
  151. public void Dispose()
  152. {
  153. foreach (var entry in originalSizes)
  154. entry.Key.fontSize = entry.Value;
  155. }
  156. }
  157. private void DrawUsageHelp()
  158. {
  159. EditorGUILayout.Space(12);
  160. showTestUrlHelp = EditorGUILayout.Foldout(showTestUrlHelp, "添加测试 URL(iOS)", true);
  161. if (showTestUrlHelp)
  162. {
  163. EditorGUILayout.HelpBox(
  164. "导出 iOS 工程后,在 Xcode 的主 App Target → Info 中添加 TestUserID,类型选 String," +
  165. "值填写完整测试 URL(例如 https://example.com/test/)。\n" +
  166. "也可以直接在 Info.plist 的根 <dict> 内加入以下内容。",
  167. MessageType.Info);
  168. DrawExample(TestUrlExample, "复制 Info.plist 示例");
  169. EditorGUILayout.HelpBox(
  170. "iOS 启动时读取 TestUserID;非空时直接使用该测试地址。修改后需重新构建并启动 App。\n" +
  171. "删除该键或清空值可恢复正常流程。Android 和 Unity Editor 的 TestUrl 始终为空。",
  172. MessageType.None);
  173. }
  174. EditorGUILayout.Space(8);
  175. showUsageHelp = EditorGUILayout.Foldout(showUsageHelp, "AppRuntimes 使用方法", true);
  176. if (showUsageHelp)
  177. {
  178. EditorGUILayout.HelpBox(
  179. "1. 填写上方配置并点击 Save。AppAddress 填明文,保存时自动加密。\n" +
  180. "2. 在App的启动脚本时 先调用 Initialization(),再等待 CheckVersion():返回 true 表示已打开平台页面," +
  181. "应停止游戏背景音乐;返回 false 时继续游戏主流程。",
  182. MessageType.Info);
  183. DrawExample(UsageExample, string.Empty);
  184. EditorGUILayout.HelpBox(
  185. "AppRuntimes.Log 在编辑器中始终输出。iOS 在 Info.plist 中添加 Boolean 类型的 EnableUnityLog," +
  186. "设为 YES 时输出日志;未设置时默认为 NO。其他平台默认关闭。",
  187. MessageType.Info);
  188. }
  189. }
  190. private void DrawExample(string example, string copyLabel)
  191. {
  192. var style = new GUIStyle(EditorStyles.textArea) { wordWrap = true };
  193. var height = style.CalcHeight(new GUIContent(example), Mathf.Max(200, position.width - 40));
  194. EditorGUILayout.SelectableLabel(example, style, GUILayout.Height(height));
  195. if (!string.IsNullOrEmpty(copyLabel) && GUILayout.Button(copyLabel))
  196. {
  197. EditorGUIUtility.systemCopyBuffer = example;
  198. ShowNotification(new GUIContent("示例已复制"));
  199. }
  200. }
  201. private void SaveSettings()
  202. {
  203. // Do not overwrite an unreadable address when saving unrelated settings.
  204. if (!string.IsNullOrEmpty(addressError)) return;
  205. string encryptedAddress;
  206. try
  207. {
  208. encryptedAddress = string.IsNullOrEmpty(appAddress)
  209. ? string.Empty : ClientCrypto.EncryptString(appAddress);
  210. }
  211. catch (Exception)
  212. {
  213. addressError = "AppAddress 加密失败,配置未保存,请重新填写后重试。";
  214. return;
  215. }
  216. Undo.RecordObject(settings, "Save AppSettings");
  217. settings.AppAddress = encryptedAddress;
  218. settings.AppChannel = appChannel;
  219. settings.PrivacyUrl = privacyUrl;
  220. EditorUtility.SetDirty(settings);
  221. if (facebook != null)
  222. {
  223. facebook.Update();
  224. WriteFacebookValue("appLabels", appName);
  225. WriteFacebookValue("appIds", appId);
  226. WriteFacebookValue("clientTokens", clientToken);
  227. facebook.ApplyModifiedProperties();
  228. EditorUtility.SetDirty(facebook.targetObject);
  229. }
  230. AssetDatabase.SaveAssets();
  231. ShowNotification(new GUIContent("Settings saved"));
  232. }
  233. }