using UnityEngine; using UnityEditor; using System.Collections.Generic; using System; using System.IO; public class UniWebViewEditorSettings: ScriptableObject { private const string AssetPath = "Assets/Editor/UniWebView/settings.asset"; private const string MinBrowserVersion = defaultAndroidBrowserVersion; private const string MinKotlinVersion = defaultKotlinVersion; [SerializeField] internal bool usesCleartextTraffic = false; [SerializeField] internal bool writeExternalStorage = false; [SerializeField] internal bool accessFineLocation = false; [SerializeField] internal bool addsKotlin = true; [SerializeField] internal string kotlinVersion = null; [SerializeField] internal bool addsAndroidBrowser = true; [SerializeField] internal string androidBrowserVersion = null; [SerializeField] internal bool addsAndroidXCore = false; [SerializeField] internal string androidXCoreVersion = null; [SerializeField] internal bool enableJetifier = true; [SerializeField] internal string[] authCallbackUrls = { }; [SerializeField] internal bool supportLINELogin = false; [SerializeField] internal string[] androidAssetsFolders = { }; internal const string defaultKotlinVersion = "1.8.22"; internal const string defaultAndroidBrowserVersion = "1.5.0"; internal const string defaultAndroidXCoreVersion = "1.5.0"; private void OnValidate() { androidBrowserVersion = ClampBrowserVersion(androidBrowserVersion); kotlinVersion = ClampKotlinVersion(kotlinVersion); } private string ClampBrowserVersion(string value) { if (string.IsNullOrWhiteSpace(value)) { return ""; } if (TryParseVersion(value, out var parsedCandidate) && TryParseVersion(MinBrowserVersion, out var parsedMin) && parsedCandidate < parsedMin) { Debug.LogWarning($" Browser version {value} lower than supported minimum {MinBrowserVersion}, resetting to {MinBrowserVersion}."); return MinBrowserVersion; } return value; } private string ClampKotlinVersion(string value) { if (string.IsNullOrWhiteSpace(value)) { return ""; } if (TryParseVersion(value, out var parsedCandidate) && TryParseVersion(MinKotlinVersion, out var parsedMin) && parsedCandidate < parsedMin) { Debug.LogWarning($" Kotlin version {value} lower than supported minimum {MinKotlinVersion}, resetting to {MinKotlinVersion}."); return MinKotlinVersion; } return value; } internal static bool TryParseVersion(string versionString, out Version version) { version = null; if (string.IsNullOrWhiteSpace(versionString)) { return false; } // Strip suffix like -alpha05 / +build before parsing. var trimmed = versionString.Trim(); var dashIndex = trimmed.IndexOfAny(new[] { '-', '+' }); if (dashIndex > 0) { trimmed = trimmed.Substring(0, dashIndex); } if (Version.TryParse(trimmed, out var parsed)) { // Normalize negative build/revision to zero for comparison safety. version = new Version( parsed.Major, parsed.Minor, parsed.Build < 0 ? 0 : parsed.Build, parsed.Revision < 0 ? 0 : parsed.Revision); return true; } return false; } internal static UniWebViewEditorSettings GetOrCreateSettings() { var settings = AssetDatabase.LoadAssetAtPath(AssetPath); if (settings == null) { settings = ScriptableObject.CreateInstance(); Directory.CreateDirectory("Assets/Editor/UniWebView/"); AssetDatabase.CreateAsset(settings, AssetPath); AssetDatabase.SaveAssets(); } return settings; } internal static SerializedObject GetSerializedSettings() { return new SerializedObject(GetOrCreateSettings()); } } static class UniWebViewSettingsProvider { static SerializedObject settings; #if UNITY_2018_3_OR_NEWER private class Provider : SettingsProvider { public Provider(string path, SettingsScope scope = SettingsScope.User): base(path, scope) {} public override void OnGUI(string searchContext) { DrawPref(); } } [SettingsProvider] static SettingsProvider UniWebViewPref() { return new Provider("Preferences/UniWebView"); } #else [PreferenceItem("UniWebView")] #endif static void DrawPref() { EditorGUIUtility.labelWidth = 320; EditorGUIUtility.fieldWidth = 20; if (settings == null) { settings = UniWebViewEditorSettings.GetSerializedSettings(); } settings.Update(); EditorGUI.BeginChangeCheck(); // Manifest EditorGUILayout.Space(); EditorGUILayout.BeginVertical(); EditorGUILayout.LabelField("Android Manifest", EditorStyles.boldLabel); EditorGUI.indentLevel++; EditorGUILayout.PropertyField(settings.FindProperty("usesCleartextTraffic")); DrawDetailLabel("If you need to load plain HTTP content."); EditorGUILayout.PropertyField(settings.FindProperty("writeExternalStorage")); DrawDetailLabel("If you need to download an image from web page."); EditorGUILayout.PropertyField(settings.FindProperty("accessFineLocation")); DrawDetailLabel("If you need to enable location support in web view."); EditorGUI.indentLevel--; EditorGUILayout.EndVertical(); // Gradle EditorGUILayout.Space(); EditorGUILayout.BeginVertical(); EditorGUILayout.LabelField("Gradle Build", EditorStyles.boldLabel); EditorGUI.indentLevel++; EditorGUILayout.PropertyField(settings.FindProperty("addsKotlin")); DrawDetailLabel("Turn off this if another library is already adding Kotlin runtime."); var addingKotlin = settings.FindProperty("addsKotlin").boolValue; if (addingKotlin) { EditorGUI.indentLevel++; EditorGUILayout.PropertyField(settings.FindProperty("kotlinVersion"), GUILayout.Width(400)); DrawDetailLabel("If not specified, use the default version: " + UniWebViewEditorSettings.defaultKotlinVersion); EditorGUI.indentLevel--; } EditorGUILayout.PropertyField(settings.FindProperty("addsAndroidBrowser")); DrawDetailLabel("Turn off this if another library is already adding 'androidx.browser:browser'."); var addingBrowser = settings.FindProperty("addsAndroidBrowser").boolValue; if (addingBrowser) { EditorGUI.indentLevel++; EditorGUILayout.PropertyField(settings.FindProperty("androidBrowserVersion"), GUILayout.Width(400)); DrawDetailLabel("If not specified, use the default version: " + UniWebViewEditorSettings.defaultAndroidBrowserVersion); EditorGUI.indentLevel--; } if (!addingBrowser) { EditorGUILayout.BeginVertical("box"); 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); EditorGUILayout.PropertyField(settings.FindProperty("addsAndroidXCore")); DrawDetailLabel("Turn on this if you disabled `Adds Android Browser` and there is no other library adding 'androidx.core:core'."); var addingCore = settings.FindProperty("addsAndroidXCore").boolValue; if (addingCore) { EditorGUI.indentLevel++; EditorGUILayout.PropertyField(settings.FindProperty("androidXCoreVersion"), GUILayout.Width(400)); DrawDetailLabel("If not specified, use the default version: " + UniWebViewEditorSettings.defaultAndroidXCoreVersion); EditorGUI.indentLevel--; } EditorGUILayout.EndVertical(); } EditorGUILayout.PropertyField(settings.FindProperty("enableJetifier")); DrawDetailLabel("Turn off this if you do not need Jetifier (for converting other legacy support dependencies to Android X)."); EditorGUI.indentLevel--; EditorGUILayout.EndVertical(); // Auth callbacks EditorGUILayout.Space(); EditorGUILayout.BeginVertical(); EditorGUILayout.LabelField("Auth Callbacks", EditorStyles.boldLabel); EditorGUI.indentLevel++; EditorGUILayout.PropertyField(settings.FindProperty("authCallbackUrls"), true); DrawDetailLabel("Adds all available auth callback URLs here to use UniWebView's auth support."); EditorGUILayout.Space(); EditorGUILayout.PropertyField(settings.FindProperty("supportLINELogin")); DrawDetailLabel("LINE Login is using a custom fixed scheme. If you want to support LINE Login, turn on this."); EditorGUI.indentLevel--; EditorGUILayout.EndVertical(); // Android Assets EditorGUILayout.Space(); EditorGUILayout.BeginVertical(); EditorGUILayout.LabelField("Android Assets", EditorStyles.boldLabel); EditorGUI.indentLevel++; EditorGUILayout.PropertyField(settings.FindProperty("androidAssetsFolders"), true); DrawDetailLabel("Asset folders to copy to Android assets (relative to Assets directory). Leave empty to disable."); EditorGUI.indentLevel--; EditorGUILayout.EndVertical(); EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); EditorGUI.indentLevel++; EditorGUILayout.HelpBox("Read the help page to know more about all UniWebView preferences detail.", MessageType.Info); var style = new GUIStyle(GUI.skin.label); style.normal.textColor = Color.blue; if (GUILayout.Button("Help Page", style)) { Application.OpenURL("https://docs.uniwebview.com/guide/installation.html#optional-steps"); } EditorGUILayout.Space(); EditorGUI.indentLevel--; EditorGUILayout.EndHorizontal(); if (EditorGUI.EndChangeCheck()) { settings.ApplyModifiedProperties(); AssetDatabase.SaveAssets(); } EditorGUIUtility.labelWidth = 0; } static void DrawDetailLabel(string text) { EditorGUI.indentLevel++; EditorGUILayout.LabelField(text, EditorStyles.miniLabel); EditorGUI.indentLevel--; } }