您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

219 行
8.6 KiB

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. using System;
  5. using System.IO;
  6. public class UniWebViewEditorSettings: ScriptableObject
  7. {
  8. private const string AssetPath = "Assets/Editor/UniWebView/settings.asset";
  9. [SerializeField]
  10. internal bool usesCleartextTraffic = false;
  11. [SerializeField]
  12. internal bool writeExternalStorage = false;
  13. [SerializeField]
  14. internal bool accessFineLocation = false;
  15. [SerializeField]
  16. internal bool addsKotlin = true;
  17. [SerializeField]
  18. internal string kotlinVersion = null;
  19. [SerializeField]
  20. internal bool addsAndroidBrowser = true;
  21. [SerializeField]
  22. internal string androidBrowserVersion = null;
  23. [SerializeField]
  24. internal bool addsAndroidXCore = false;
  25. [SerializeField]
  26. internal string androidXCoreVersion = null;
  27. [SerializeField]
  28. internal bool enableJetifier = true;
  29. [SerializeField]
  30. internal string[] authCallbackUrls = { };
  31. [SerializeField]
  32. internal bool supportLINELogin = false;
  33. internal static string defaultKotlinVersion = "1.6.21";
  34. internal static string defaultAndroidBrowserVersion = "1.2.0";
  35. internal static string defaultAndroidXCoreVersion = "1.5.0";
  36. internal static UniWebViewEditorSettings GetOrCreateSettings() {
  37. var settings = AssetDatabase.LoadAssetAtPath<UniWebViewEditorSettings>(AssetPath);
  38. if (settings == null) {
  39. settings = ScriptableObject.CreateInstance<UniWebViewEditorSettings>();
  40. Directory.CreateDirectory("Assets/Editor/UniWebView/");
  41. AssetDatabase.CreateAsset(settings, AssetPath);
  42. AssetDatabase.SaveAssets();
  43. }
  44. return settings;
  45. }
  46. internal static SerializedObject GetSerializedSettings() {
  47. return new SerializedObject(GetOrCreateSettings());
  48. }
  49. }
  50. // UniWebViewEditorSettings is not working well with AndroidProjectFilesModifier.
  51. // (reading it requires main thread, but the OnModifyAndroidProjectFiles is not in main thread)
  52. [Serializable]
  53. public class UniWebViewEditorSettingsReading {
  54. public bool usesCleartextTraffic = false;
  55. public bool writeExternalStorage = false;
  56. public bool accessFineLocation = false;
  57. public bool addsKotlin = true;
  58. public string kotlinVersion = null;
  59. public bool addsAndroidBrowser = true;
  60. public string androidBrowserVersion = null;
  61. public bool addsAndroidXCore = false;
  62. public string androidXCoreVersion = null;
  63. public bool enableJetifier = true;
  64. public string[] authCallbackUrls = { };
  65. public bool supportLINELogin = false;
  66. }
  67. static class UniWebViewSettingsProvider {
  68. static SerializedObject settings;
  69. #if UNITY_2018_3_OR_NEWER
  70. private class Provider : SettingsProvider {
  71. public Provider(string path, SettingsScope scope = SettingsScope.User): base(path, scope) {}
  72. public override void OnGUI(string searchContext) {
  73. DrawPref();
  74. }
  75. }
  76. [SettingsProvider]
  77. static SettingsProvider UniWebViewPref() {
  78. return new Provider("Preferences/UniWebView");
  79. }
  80. #else
  81. [PreferenceItem("UniWebView")]
  82. #endif
  83. static void DrawPref() {
  84. EditorGUIUtility.labelWidth = 320;
  85. EditorGUIUtility.fieldWidth = 20;
  86. if (settings == null) {
  87. settings = UniWebViewEditorSettings.GetSerializedSettings();
  88. }
  89. settings.Update();
  90. EditorGUI.BeginChangeCheck();
  91. // Manifest
  92. EditorGUILayout.Space();
  93. EditorGUILayout.BeginVertical();
  94. EditorGUILayout.LabelField("Android Manifest", EditorStyles.boldLabel);
  95. EditorGUI.indentLevel++;
  96. EditorGUILayout.PropertyField(settings.FindProperty("usesCleartextTraffic"));
  97. DrawDetailLabel("If you need to load plain HTTP content.");
  98. EditorGUILayout.PropertyField(settings.FindProperty("writeExternalStorage"));
  99. DrawDetailLabel("If you need to download an image from web page.");
  100. EditorGUILayout.PropertyField(settings.FindProperty("accessFineLocation"));
  101. DrawDetailLabel("If you need to enable location support in web view.");
  102. EditorGUI.indentLevel--;
  103. EditorGUILayout.EndVertical();
  104. // Gradle
  105. EditorGUILayout.Space();
  106. EditorGUILayout.BeginVertical();
  107. EditorGUILayout.LabelField("Gradle Build", EditorStyles.boldLabel);
  108. EditorGUI.indentLevel++;
  109. EditorGUILayout.PropertyField(settings.FindProperty("addsKotlin"));
  110. DrawDetailLabel("Turn off this if another library is already adding Kotlin runtime.");
  111. var addingKotlin = settings.FindProperty("addsKotlin").boolValue;
  112. if (addingKotlin) {
  113. EditorGUI.indentLevel++;
  114. EditorGUILayout.PropertyField(settings.FindProperty("kotlinVersion"), GUILayout.Width(400));
  115. DrawDetailLabel("If not specified, use the default version: " + UniWebViewEditorSettings.defaultKotlinVersion);
  116. EditorGUI.indentLevel--;
  117. }
  118. EditorGUILayout.PropertyField(settings.FindProperty("addsAndroidBrowser"));
  119. DrawDetailLabel("Turn off this if another library is already adding 'androidx.browser:browser'.");
  120. var addingBrowser = settings.FindProperty("addsAndroidBrowser").boolValue;
  121. if (addingBrowser) {
  122. EditorGUI.indentLevel++;
  123. EditorGUILayout.PropertyField(settings.FindProperty("androidBrowserVersion"), GUILayout.Width(400));
  124. DrawDetailLabel("If not specified, use the default version: " + UniWebViewEditorSettings.defaultAndroidBrowserVersion);
  125. EditorGUI.indentLevel--;
  126. }
  127. if (!addingBrowser) {
  128. EditorGUILayout.BeginVertical("box");
  129. EditorGUILayout.HelpBox("UniWebView at least requires `androidx.core` to run. Without it, your game will crash when launching.\nIf you do not have another `androidx.core` package in the project, enable the option below.", MessageType.Warning);
  130. EditorGUILayout.PropertyField(settings.FindProperty("addsAndroidXCore"));
  131. DrawDetailLabel("Turn on this if you disabled `Adds Android Browser` and there is no other library adding 'androidx.core:core'.");
  132. var addingCore = settings.FindProperty("addsAndroidXCore").boolValue;
  133. if (addingCore) {
  134. EditorGUI.indentLevel++;
  135. EditorGUILayout.PropertyField(settings.FindProperty("androidXCoreVersion"), GUILayout.Width(400));
  136. DrawDetailLabel("If not specified, use the default version: " + UniWebViewEditorSettings.defaultAndroidXCoreVersion);
  137. EditorGUI.indentLevel--;
  138. }
  139. EditorGUILayout.EndVertical();
  140. }
  141. EditorGUILayout.PropertyField(settings.FindProperty("enableJetifier"));
  142. DrawDetailLabel("Turn off this if you do not need Jetifier (for converting other legacy support dependencies to Android X).");
  143. EditorGUI.indentLevel--;
  144. EditorGUILayout.EndVertical();
  145. // Auth callbacks
  146. EditorGUILayout.Space();
  147. EditorGUILayout.BeginVertical();
  148. EditorGUILayout.LabelField("Auth Callbacks", EditorStyles.boldLabel);
  149. EditorGUI.indentLevel++;
  150. EditorGUILayout.PropertyField(settings.FindProperty("authCallbackUrls"), true);
  151. DrawDetailLabel("Adds all available auth callback URLs here to use UniWebView's auth support.");
  152. EditorGUILayout.Space();
  153. EditorGUILayout.PropertyField(settings.FindProperty("supportLINELogin"));
  154. DrawDetailLabel("LINE Login is using a custom fixed scheme. If you want to support LINE Login, turn on this.");
  155. EditorGUI.indentLevel--;
  156. EditorGUILayout.EndVertical();
  157. EditorGUILayout.Space();
  158. EditorGUILayout.BeginHorizontal();
  159. EditorGUI.indentLevel++;
  160. EditorGUILayout.HelpBox("Read the help page to know more about all UniWebView preferences detail.", MessageType.Info);
  161. var style = new GUIStyle(GUI.skin.label);
  162. style.normal.textColor = Color.blue;
  163. if (GUILayout.Button("Help Page", style)) {
  164. Application.OpenURL("https://docs.uniwebview.com/guide/installation.html#optional-steps");
  165. }
  166. EditorGUILayout.Space();
  167. EditorGUI.indentLevel--;
  168. EditorGUILayout.EndHorizontal();
  169. if (EditorGUI.EndChangeCheck()) {
  170. settings.ApplyModifiedProperties();
  171. AssetDatabase.SaveAssets();
  172. }
  173. EditorGUIUtility.labelWidth = 0;
  174. }
  175. static void DrawDetailLabel(string text) {
  176. EditorGUI.indentLevel++;
  177. EditorGUILayout.LabelField(text, EditorStyles.miniLabel);
  178. EditorGUI.indentLevel--;
  179. }
  180. }