From 3591def89c0df94154b28e45e89fc95d36438314 Mon Sep 17 00:00:00 2001 From: Voltstro Date: Fri, 26 Jan 2024 15:52:19 +1000 Subject: [PATCH] Add support for allowing Unity to build the project on unsupported platforms This doesn't mean that UWB will run on them! --- .../Runtime/Core/WebBrowserClient.cs | 10 +++++ .../EngineManagerPostprocess.cs | 12 +++++- .../Runtime/Helper/WebBrowserUtils.cs | 40 ++++++++++--------- .../VoltstroStudios.UnityWebBrowser.asmdef | 18 ++++++++- .../Assets/Scripts/UWBPrjDebugUI.cs | 13 ++++++ 5 files changed, 72 insertions(+), 21 deletions(-) diff --git a/src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs b/src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs index a0b536e0..1cefd95c 100644 --- a/src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs +++ b/src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs @@ -265,6 +265,13 @@ internal WebBrowserClient() /// internal void Init() { + if (!WebBrowserUtils.IsRunningOnSupportedPlatform()) + { + logger.Warn("UWB is not supported on the current runtime platform! Not running."); + Dispose(); + return; + } + //Get the path to the UWB process we are using and make sure it exists string browserEnginePath = WebBrowserUtils.GetBrowserEngineProcessPath(engine); logger.Debug($"Starting browser engine process from '{browserEnginePath}'..."); @@ -949,6 +956,9 @@ private void ReleaseResources() } //Dispose of buffers + if (resizeLock == null) + return; + lock (resizeLock) { if (nextTextureData.IsCreated) diff --git a/src/Packages/UnityWebBrowser/Runtime/Editor/EngineManagement/EngineManagerPostprocess.cs b/src/Packages/UnityWebBrowser/Runtime/Editor/EngineManagement/EngineManagerPostprocess.cs index 748d543f..d1202538 100644 --- a/src/Packages/UnityWebBrowser/Runtime/Editor/EngineManagement/EngineManagerPostprocess.cs +++ b/src/Packages/UnityWebBrowser/Runtime/Editor/EngineManagement/EngineManagerPostprocess.cs @@ -5,6 +5,7 @@ #if UNITY_EDITOR +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -32,7 +33,16 @@ public void OnPostprocessBuild(BuildReport report) #endif BuildTarget buildTarget = report.summary.platform; - Platform buildPlatform = buildTarget.UnityBuildTargetToPlatform(); + Platform buildPlatform; + try + { + buildPlatform = buildTarget.UnityBuildTargetToPlatform(); + } + catch (ArgumentOutOfRangeException) + { + Debug.LogWarning("UWB engine will not be copied! Unsupported platform."); + return; + } string buildFullOutputPath = report.summary.outputPath; string buildAppName = Path.GetFileNameWithoutExtension(buildFullOutputPath); diff --git a/src/Packages/UnityWebBrowser/Runtime/Helper/WebBrowserUtils.cs b/src/Packages/UnityWebBrowser/Runtime/Helper/WebBrowserUtils.cs index b832432a..fe8a18b1 100644 --- a/src/Packages/UnityWebBrowser/Runtime/Helper/WebBrowserUtils.cs +++ b/src/Packages/UnityWebBrowser/Runtime/Helper/WebBrowserUtils.cs @@ -18,6 +18,7 @@ using System; using System.Diagnostics; using System.IO; +using System.Linq; using System.Runtime.CompilerServices; using System.Threading; using Unity.Collections; @@ -41,6 +42,15 @@ namespace VoltstroStudios.UnityWebBrowser.Helper [Preserve] public static class WebBrowserUtils { + private static RuntimePlatform[] supportedPlatforms = new[] + { + RuntimePlatform.WindowsPlayer, + //RuntimePlatform.WindowsEditor, + RuntimePlatform.LinuxPlayer, + RuntimePlatform.LinuxEditor, + RuntimePlatform.OSXEditor + }; + /// /// Gets the main directory where logs and cache may be stored /// @@ -71,6 +81,8 @@ public static string GetBrowserEnginePath(Engine engine) return Path.GetFullPath($"{Application.dataPath}/Resources/Data/UWB/"); #elif UNITY_STANDALONE return Path.GetFullPath($"{Application.dataPath}/UWB/"); +#else //Unsupported platform, UWB shouldn't run anyway + return null; #endif } @@ -137,6 +149,15 @@ public static bool GetScreenPointToLocalPositionDeltaOnImage(Graphic graphic, Ve return true; } + + /// + /// Checks if UWB is running on a supported platform + /// + /// + public static bool IsRunningOnSupportedPlatform() + { + return supportedPlatforms.Any(x => x == UnityEngine.Device.Application.platform); + } /// /// Converts a to hex @@ -208,24 +229,5 @@ internal static void SetAllTextureColorToOne(Texture2D texture, Color32 color) texture.SetPixels32(colors); texture.Apply(); } - - /// - /// Copies a to a - /// - /// - /// - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static void CopySpanToNativeArray(ReadOnlySpan copyFrom, NativeArray copyTo, - CancellationToken token) where T : struct - { - for (int i = 0; i < copyFrom.Length; i++) - { - if (token.IsCancellationRequested) - return; - - copyTo[i] = copyFrom[i]; - } - } } } \ No newline at end of file diff --git a/src/Packages/UnityWebBrowser/Runtime/VoltstroStudios.UnityWebBrowser.asmdef b/src/Packages/UnityWebBrowser/Runtime/VoltstroStudios.UnityWebBrowser.asmdef index 0ed21388..3eefdc7c 100644 --- a/src/Packages/UnityWebBrowser/Runtime/VoltstroStudios.UnityWebBrowser.asmdef +++ b/src/Packages/UnityWebBrowser/Runtime/VoltstroStudios.UnityWebBrowser.asmdef @@ -9,10 +9,26 @@ "UniTask" ], "includePlatforms": [ + "Android", "Editor", + "EmbeddedLinux", + "GameCoreScarlett", + "GameCoreXboxOne", + "iOS", "LinuxStandalone64", + "CloudRendering", + "Lumin", "macOSStandalone", - "WindowsStandalone64" + "PS4", + "PS5", + "Stadia", + "Switch", + "tvOS", + "WSA", + "WebGL", + "WindowsStandalone32", + "WindowsStandalone64", + "XboxOne" ], "excludePlatforms": [], "allowUnsafeCode": false, diff --git a/src/UnityWebBrowser.UnityProject/Assets/Scripts/UWBPrjDebugUI.cs b/src/UnityWebBrowser.UnityProject/Assets/Scripts/UWBPrjDebugUI.cs index 1ac64fea..6183c85c 100644 --- a/src/UnityWebBrowser.UnityProject/Assets/Scripts/UWBPrjDebugUI.cs +++ b/src/UnityWebBrowser.UnityProject/Assets/Scripts/UWBPrjDebugUI.cs @@ -55,6 +55,10 @@ private void Start() unformattedConsoleItems = new List { startMessage }; formattedConsoleItems = new List { startMessage }; popups = new List(); + + if(webBrowserUIBasic.browserClient.HasDisposed) + return; + webBrowserUIBasic.browserClient.processLogHandler.OnProcessOutputLog += HandleOutputLogMessage; webBrowserUIBasic.browserClient.processLogHandler.OnProcessErrorLog += HandleErrorLogMessage; @@ -90,6 +94,9 @@ private void Start() private void Update() { + if(webBrowserUIBasic.browserClient.HasDisposed) + return; + fps = (int)(1f / Time.unscaledDeltaTime); if (!(Time.unscaledTime > timer)) return; @@ -103,6 +110,9 @@ private void Update() private void OnDestroy() { UImGuiUtility.Layout -= OnImGuiLayout; + + if(webBrowserUIBasic.browserClient.HasDisposed) + return; getPixelsMarker.Dispose(); applyTextureMarker.Dispose(); @@ -113,6 +123,9 @@ private void OnImGuiLayout(UImGui.UImGui uImGui) if(hide) return; + if(webBrowserUIBasic.browserClient.HasDisposed) + return; + ImGui.Begin("UWB Debug UI"); { if (!webBrowserUIBasic.browserClient.ReadySignalReceived || webBrowserUIBasic.browserClient.HasDisposed)