UniWebViewEditorSettings.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. internal static UniWebViewEditorSettings GetOrCreateSettings() {
  91. var settings = AssetDatabase.LoadAssetAtPath<UniWebViewEditorSettings>(AssetPath);
  92. if (settings == null) {
  93. settings = ScriptableObject.CreateInstance<UniWebViewEditorSettings>();
  94. Directory.CreateDirectory("Assets/Editor/UniWebView/");
  95. AssetDatabase.CreateAsset(settings, AssetPath);
  96. AssetDatabase.SaveAssets();
  97. }
  98. return settings;
  99. }
  100. internal static SerializedObject GetSerializedSettings() {
  101. return new SerializedObject(GetOrCreateSettings());
  102. }
  103. }
  104. static class UniWebViewSettingsProvider {
  105. static SerializedObject settings;
  106. #if UNITY_2018_3_OR_NEWER
  107. private class Provider : SettingsProvider {
  108. public Provider(string path, SettingsScope scope = SettingsScope.User): base(path, scope) {}
  109. public override void OnGUI(string searchContext) {
  110. DrawPref();
  111. }
  112. }
  113. [SettingsProvider]
  114. static SettingsProvider UniWebViewPref() {
  115. return new Provider("Preferences/UniWebView");
  116. }
  117. #else
  118. [PreferenceItem("UniWebView")]
  119. #endif
  120. static void DrawPref() {
  121. EditorGUIUtility.labelWidth = 320;
  122. EditorGUIUtility.fieldWidth = 20;
  123. if (settings == null) {
  124. settings = UniWebViewEditorSettings.GetSerializedSettings();
  125. }
  126. settings.Update();
  127. EditorGUI.BeginChangeCheck();
  128. // Manifest
  129. EditorGUILayout.Space();
  130. EditorGUILayout.BeginVertical();
  131. EditorGUILayout.LabelField("Android Manifest", EditorStyles.boldLabel);
  132. EditorGUI.indentLevel++;
  133. EditorGUILayout.PropertyField(settings.FindProperty("usesCleartextTraffic"));
  134. DrawDetailLabel("If you need to load plain HTTP content.");
  135. EditorGUILayout.PropertyField(settings.FindProperty("writeExternalStorage"));
  136. DrawDetailLabel("If you need to download an image from web page.");
  137. EditorGUILayout.PropertyField(settings.FindProperty("accessFineLocation"));
  138. DrawDetailLabel("If you need to enable location support in web view.");
  139. EditorGUI.indentLevel--;
  140. EditorGUILayout.EndVertical();
  141. // Gradle
  142. EditorGUILayout.Space();
  143. EditorGUILayout.BeginVertical();
  144. EditorGUILayout.LabelField("Gradle Build", EditorStyles.boldLabel);
  145. EditorGUI.indentLevel++;
  146. EditorGUILayout.PropertyField(settings.FindProperty("addsKotlin"));
  147. DrawDetailLabel("Turn off this if another library is already adding Kotlin runtime.");
  148. var addingKotlin = settings.FindProperty("addsKotlin").boolValue;
  149. if (addingKotlin) {
  150. EditorGUI.indentLevel++;
  151. EditorGUILayout.PropertyField(settings.FindProperty("kotlinVersion"), GUILayout.Width(400));
  152. DrawDetailLabel("If not specified, use the default version: " + UniWebViewEditorSettings.defaultKotlinVersion);
  153. EditorGUI.indentLevel--;
  154. }
  155. EditorGUILayout.PropertyField(settings.FindProperty("addsAndroidBrowser"));
  156. DrawDetailLabel("Turn off this if another library is already adding 'androidx.browser:browser'.");
  157. var addingBrowser = settings.FindProperty("addsAndroidBrowser").boolValue;
  158. if (addingBrowser) {
  159. EditorGUI.indentLevel++;
  160. EditorGUILayout.PropertyField(settings.FindProperty("androidBrowserVersion"), GUILayout.Width(400));
  161. DrawDetailLabel("If not specified, use the default version: " + UniWebViewEditorSettings.defaultAndroidBrowserVersion);
  162. EditorGUI.indentLevel--;
  163. }
  164. if (!addingBrowser) {
  165. EditorGUILayout.BeginVertical("box");
  166. 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);
  167. EditorGUILayout.PropertyField(settings.FindProperty("addsAndroidXCore"));
  168. DrawDetailLabel("Turn on this if you disabled `Adds Android Browser` and there is no other library adding 'androidx.core:core'.");
  169. var addingCore = settings.FindProperty("addsAndroidXCore").boolValue;
  170. if (addingCore) {
  171. EditorGUI.indentLevel++;
  172. EditorGUILayout.PropertyField(settings.FindProperty("androidXCoreVersion"), GUILayout.Width(400));
  173. DrawDetailLabel("If not specified, use the default version: " + UniWebViewEditorSettings.defaultAndroidXCoreVersion);
  174. EditorGUI.indentLevel--;
  175. }
  176. EditorGUILayout.EndVertical();
  177. }
  178. EditorGUILayout.PropertyField(settings.FindProperty("enableJetifier"));
  179. DrawDetailLabel("Turn off this if you do not need Jetifier (for converting other legacy support dependencies to Android X).");
  180. EditorGUI.indentLevel--;
  181. EditorGUILayout.EndVertical();
  182. // Auth callbacks
  183. EditorGUILayout.Space();
  184. EditorGUILayout.BeginVertical();
  185. EditorGUILayout.LabelField("Auth Callbacks", EditorStyles.boldLabel);
  186. EditorGUI.indentLevel++;
  187. EditorGUILayout.PropertyField(settings.FindProperty("authCallbackUrls"), true);
  188. DrawDetailLabel("Adds all available auth callback URLs here to use UniWebView's auth support.");
  189. EditorGUILayout.Space();
  190. EditorGUILayout.PropertyField(settings.FindProperty("supportLINELogin"));
  191. DrawDetailLabel("LINE Login is using a custom fixed scheme. If you want to support LINE Login, turn on this.");
  192. EditorGUI.indentLevel--;
  193. EditorGUILayout.EndVertical();
  194. // Android Assets
  195. EditorGUILayout.Space();
  196. EditorGUILayout.BeginVertical();
  197. EditorGUILayout.LabelField("Android Assets", EditorStyles.boldLabel);
  198. EditorGUI.indentLevel++;
  199. EditorGUILayout.PropertyField(settings.FindProperty("androidAssetsFolders"), true);
  200. DrawDetailLabel("Asset folders to copy to Android assets (relative to Assets directory). Leave empty to disable.");
  201. EditorGUI.indentLevel--;
  202. EditorGUILayout.EndVertical();
  203. EditorGUILayout.Space();
  204. EditorGUILayout.BeginHorizontal();
  205. EditorGUI.indentLevel++;
  206. EditorGUILayout.HelpBox("Read the help page to know more about all UniWebView preferences detail.", MessageType.Info);
  207. var style = new GUIStyle(GUI.skin.label);
  208. style.normal.textColor = Color.blue;
  209. if (GUILayout.Button("Help Page", style)) {
  210. Application.OpenURL("https://docs.uniwebview.com/guide/installation.html#optional-steps");
  211. }
  212. EditorGUILayout.Space();
  213. EditorGUI.indentLevel--;
  214. EditorGUILayout.EndHorizontal();
  215. if (EditorGUI.EndChangeCheck()) {
  216. settings.ApplyModifiedProperties();
  217. AssetDatabase.SaveAssets();
  218. }
  219. EditorGUIUtility.labelWidth = 0;
  220. }
  221. static void DrawDetailLabel(string text) {
  222. EditorGUI.indentLevel++;
  223. EditorGUILayout.LabelField(text, EditorStyles.miniLabel);
  224. EditorGUI.indentLevel--;
  225. }
  226. }