UniWebViewEditorSettings.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. private const string MinBrowserVersion = defaultAndroidBrowserVersion;
  10. private const string MinKotlinVersion = defaultKotlinVersion;
  11. [SerializeField]
  12. internal bool usesCleartextTraffic = false;
  13. [SerializeField]
  14. internal bool writeExternalStorage = false;
  15. [SerializeField]
  16. internal bool accessFineLocation = false;
  17. [SerializeField]
  18. internal bool addsKotlin = true;
  19. [SerializeField]
  20. internal string kotlinVersion = null;
  21. [SerializeField]
  22. internal bool addsAndroidBrowser = true;
  23. [SerializeField]
  24. internal string androidBrowserVersion = null;
  25. [SerializeField]
  26. internal bool addsAndroidXCore = false;
  27. [SerializeField]
  28. internal string androidXCoreVersion = null;
  29. [SerializeField]
  30. internal bool enableJetifier = true;
  31. [SerializeField]
  32. internal string[] authCallbackUrls = { };
  33. [SerializeField]
  34. internal bool supportLINELogin = false;
  35. [SerializeField]
  36. internal string[] androidAssetsFolders = { };
  37. internal const string defaultKotlinVersion = "1.8.22";
  38. internal const string defaultAndroidBrowserVersion = "1.5.0";
  39. internal const string defaultAndroidXCoreVersion = "1.5.0";
  40. private void OnValidate() {
  41. androidBrowserVersion = ClampBrowserVersion(androidBrowserVersion);
  42. kotlinVersion = ClampKotlinVersion(kotlinVersion);
  43. }
  44. private string ClampBrowserVersion(string value) {
  45. if (string.IsNullOrWhiteSpace(value)) {
  46. return "";
  47. }
  48. if (TryParseVersion(value, out var parsedCandidate) &&
  49. TryParseVersion(MinBrowserVersion, out var parsedMin) &&
  50. parsedCandidate < parsedMin) {
  51. Debug.LogWarning($"<UniWebView> Browser version {value} lower than supported minimum {MinBrowserVersion}, resetting to {MinBrowserVersion}.");
  52. return MinBrowserVersion;
  53. }
  54. return value;
  55. }
  56. private string ClampKotlinVersion(string value) {
  57. if (string.IsNullOrWhiteSpace(value)) {
  58. return "";
  59. }
  60. if (TryParseVersion(value, out var parsedCandidate) &&
  61. TryParseVersion(MinKotlinVersion, out var parsedMin) &&
  62. parsedCandidate < parsedMin) {
  63. Debug.LogWarning($"<UniWebView> Kotlin version {value} lower than supported minimum {MinKotlinVersion}, resetting to {MinKotlinVersion}.");
  64. return MinKotlinVersion;
  65. }
  66. return value;
  67. }
  68. internal static bool TryParseVersion(string versionString, out Version version) {
  69. version = null;
  70. if (string.IsNullOrWhiteSpace(versionString)) {
  71. return false;
  72. }
  73. // Strip suffix like -alpha05 / +build before parsing.
  74. var trimmed = versionString.Trim();
  75. var dashIndex = trimmed.IndexOfAny(new[] { '-', '+' });
  76. if (dashIndex > 0) {
  77. trimmed = trimmed.Substring(0, dashIndex);
  78. }
  79. if (Version.TryParse(trimmed, out var parsed)) {
  80. // Normalize negative build/revision to zero for comparison safety.
  81. version = new Version(
  82. parsed.Major,
  83. parsed.Minor,
  84. parsed.Build < 0 ? 0 : parsed.Build,
  85. parsed.Revision < 0 ? 0 : parsed.Revision);
  86. return true;
  87. }
  88. return false;
  89. }
  90. public static UniWebViewEditorSettings GetOrCreateSettings() {
  91. var settings = AssetDatabase.LoadAssetAtPath<UniWebViewEditorSettings>(AssetPath);
  92. if (settings == null) {
  93. if (File.Exists(AssetPath))
  94. throw new InvalidOperationException("UniWebView settings exist but could not be loaded: " + AssetPath);
  95. // Use built-in defaults; no package lookup is needed now that UniWebView lives in Assets.
  96. if (!AssetDatabase.IsValidFolder("Assets/Editor"))
  97. AssetDatabase.CreateFolder("Assets", "Editor");
  98. if (!AssetDatabase.IsValidFolder("Assets/Editor/UniWebView"))
  99. AssetDatabase.CreateFolder("Assets/Editor", "UniWebView");
  100. settings = ScriptableObject.CreateInstance<UniWebViewEditorSettings>();
  101. AssetDatabase.CreateAsset(settings, AssetPath);
  102. AssetDatabase.SaveAssets();
  103. settings = AssetDatabase.LoadAssetAtPath<UniWebViewEditorSettings>(AssetPath);
  104. if (settings == null)
  105. throw new InvalidOperationException("New UniWebView settings could not be imported: " + AssetPath);
  106. }
  107. return settings;
  108. }
  109. internal static SerializedObject GetSerializedSettings() {
  110. return new SerializedObject(GetOrCreateSettings());
  111. }
  112. }
  113. static class UniWebViewSettingsProvider {
  114. static SerializedObject settings;
  115. #if UNITY_2018_3_OR_NEWER
  116. private class Provider : SettingsProvider {
  117. public Provider(string path, SettingsScope scope = SettingsScope.User): base(path, scope) {}
  118. public override void OnGUI(string searchContext) {
  119. DrawPref();
  120. }
  121. }
  122. [SettingsProvider]
  123. static SettingsProvider UniWebViewPref() {
  124. return new Provider("Preferences/UniWebView");
  125. }
  126. #else
  127. [PreferenceItem("UniWebView")]
  128. #endif
  129. static void DrawPref() {
  130. EditorGUIUtility.labelWidth = 320;
  131. EditorGUIUtility.fieldWidth = 20;
  132. if (settings == null) {
  133. settings = UniWebViewEditorSettings.GetSerializedSettings();
  134. }
  135. settings.Update();
  136. EditorGUI.BeginChangeCheck();
  137. // Manifest
  138. EditorGUILayout.Space();
  139. EditorGUILayout.BeginVertical();
  140. EditorGUILayout.LabelField("Android Manifest", EditorStyles.boldLabel);
  141. EditorGUI.indentLevel++;
  142. EditorGUILayout.PropertyField(settings.FindProperty("usesCleartextTraffic"));
  143. DrawDetailLabel("If you need to load plain HTTP content.");
  144. EditorGUILayout.PropertyField(settings.FindProperty("writeExternalStorage"));
  145. DrawDetailLabel("If you need to download an image from web page.");
  146. EditorGUILayout.PropertyField(settings.FindProperty("accessFineLocation"));
  147. DrawDetailLabel("If you need to enable location support in web view.");
  148. EditorGUI.indentLevel--;
  149. EditorGUILayout.EndVertical();
  150. // Gradle
  151. EditorGUILayout.Space();
  152. EditorGUILayout.BeginVertical();
  153. EditorGUILayout.LabelField("Gradle Build", EditorStyles.boldLabel);
  154. EditorGUI.indentLevel++;
  155. EditorGUILayout.PropertyField(settings.FindProperty("addsKotlin"));
  156. DrawDetailLabel("Turn off this if another library is already adding Kotlin runtime.");
  157. var addingKotlin = settings.FindProperty("addsKotlin").boolValue;
  158. if (addingKotlin) {
  159. EditorGUI.indentLevel++;
  160. EditorGUILayout.PropertyField(settings.FindProperty("kotlinVersion"), GUILayout.Width(400));
  161. DrawDetailLabel("If not specified, use the default version: " + UniWebViewEditorSettings.defaultKotlinVersion);
  162. EditorGUI.indentLevel--;
  163. }
  164. EditorGUILayout.PropertyField(settings.FindProperty("addsAndroidBrowser"));
  165. DrawDetailLabel("Turn off this if another library is already adding 'androidx.browser:browser'.");
  166. var addingBrowser = settings.FindProperty("addsAndroidBrowser").boolValue;
  167. if (addingBrowser) {
  168. EditorGUI.indentLevel++;
  169. EditorGUILayout.PropertyField(settings.FindProperty("androidBrowserVersion"), GUILayout.Width(400));
  170. DrawDetailLabel("If not specified, use the default version: " + UniWebViewEditorSettings.defaultAndroidBrowserVersion);
  171. EditorGUI.indentLevel--;
  172. }
  173. if (!addingBrowser) {
  174. EditorGUILayout.BeginVertical("box");
  175. 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);
  176. EditorGUILayout.PropertyField(settings.FindProperty("addsAndroidXCore"));
  177. DrawDetailLabel("Turn on this if you disabled `Adds Android Browser` and there is no other library adding 'androidx.core:core'.");
  178. var addingCore = settings.FindProperty("addsAndroidXCore").boolValue;
  179. if (addingCore) {
  180. EditorGUI.indentLevel++;
  181. EditorGUILayout.PropertyField(settings.FindProperty("androidXCoreVersion"), GUILayout.Width(400));
  182. DrawDetailLabel("If not specified, use the default version: " + UniWebViewEditorSettings.defaultAndroidXCoreVersion);
  183. EditorGUI.indentLevel--;
  184. }
  185. EditorGUILayout.EndVertical();
  186. }
  187. EditorGUILayout.PropertyField(settings.FindProperty("enableJetifier"));
  188. DrawDetailLabel("Turn off this if you do not need Jetifier (for converting other legacy support dependencies to Android X).");
  189. EditorGUI.indentLevel--;
  190. EditorGUILayout.EndVertical();
  191. // Auth callbacks
  192. EditorGUILayout.Space();
  193. EditorGUILayout.BeginVertical();
  194. EditorGUILayout.LabelField("Auth Callbacks", EditorStyles.boldLabel);
  195. EditorGUI.indentLevel++;
  196. EditorGUILayout.PropertyField(settings.FindProperty("authCallbackUrls"), true);
  197. DrawDetailLabel("Adds all available auth callback URLs here to use UniWebView's auth support.");
  198. EditorGUILayout.Space();
  199. EditorGUILayout.PropertyField(settings.FindProperty("supportLINELogin"));
  200. DrawDetailLabel("LINE Login is using a custom fixed scheme. If you want to support LINE Login, turn on this.");
  201. EditorGUI.indentLevel--;
  202. EditorGUILayout.EndVertical();
  203. // Android Assets
  204. EditorGUILayout.Space();
  205. EditorGUILayout.BeginVertical();
  206. EditorGUILayout.LabelField("Android Assets", EditorStyles.boldLabel);
  207. EditorGUI.indentLevel++;
  208. EditorGUILayout.PropertyField(settings.FindProperty("androidAssetsFolders"), true);
  209. DrawDetailLabel("Asset folders to copy to Android assets (relative to Assets directory). Leave empty to disable.");
  210. EditorGUI.indentLevel--;
  211. EditorGUILayout.EndVertical();
  212. EditorGUILayout.Space();
  213. EditorGUILayout.BeginHorizontal();
  214. EditorGUI.indentLevel++;
  215. EditorGUILayout.HelpBox("Read the help page to know more about all UniWebView preferences detail.", MessageType.Info);
  216. var style = new GUIStyle(GUI.skin.label);
  217. style.normal.textColor = Color.blue;
  218. if (GUILayout.Button("Help Page", style)) {
  219. Application.OpenURL("https://docs.uniwebview.com/guide/installation.html#optional-steps");
  220. }
  221. EditorGUILayout.Space();
  222. EditorGUI.indentLevel--;
  223. EditorGUILayout.EndHorizontal();
  224. if (EditorGUI.EndChangeCheck()) {
  225. settings.ApplyModifiedProperties();
  226. AssetDatabase.SaveAssets();
  227. }
  228. EditorGUIUtility.labelWidth = 0;
  229. }
  230. static void DrawDetailLabel(string text) {
  231. EditorGUI.indentLevel++;
  232. EditorGUILayout.LabelField(text, EditorStyles.miniLabel);
  233. EditorGUI.indentLevel--;
  234. }
  235. }