Skip to content

Commit

Permalink
Add support for allowing Unity to build the project on unsupported pl…
Browse files Browse the repository at this point in the history
…atforms

This doesn't mean that UWB will run on them!
  • Loading branch information
Voltstro committed Jan 26, 2024
1 parent 1c3185d commit 3591def
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 21 deletions.
10 changes: 10 additions & 0 deletions src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ internal WebBrowserClient()
/// <exception cref="FileNotFoundException"></exception>
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}'...");
Expand Down Expand Up @@ -949,6 +956,9 @@ private void ReleaseResources()
}

//Dispose of buffers
if (resizeLock == null)
return;

lock (resizeLock)
{
if (nextTextureData.IsCreated)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#if UNITY_EDITOR

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -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);
Expand Down
40 changes: 21 additions & 19 deletions src/Packages/UnityWebBrowser/Runtime/Helper/WebBrowserUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
};

/// <summary>
/// Gets the main directory where logs and cache may be stored
/// </summary>
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -137,6 +149,15 @@ public static bool GetScreenPointToLocalPositionDeltaOnImage(Graphic graphic, Ve

return true;
}

/// <summary>
/// Checks if UWB is running on a supported platform
/// </summary>
/// <returns></returns>
public static bool IsRunningOnSupportedPlatform()
{
return supportedPlatforms.Any(x => x == UnityEngine.Device.Application.platform);
}

/// <summary>
/// Converts a <see cref="Color32" /> to hex
Expand Down Expand Up @@ -208,24 +229,5 @@ internal static void SetAllTextureColorToOne(Texture2D texture, Color32 color)
texture.SetPixels32(colors);
texture.Apply();
}

/// <summary>
/// Copies a <see cref="ReadOnlyMemory{T}" /> to a <see cref="NativeArray{T}" />
/// </summary>
/// <param name="copyFrom"></param>
/// <param name="copyTo"></param>
/// <param name="token"></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void CopySpanToNativeArray<T>(ReadOnlySpan<T> copyFrom, NativeArray<T> copyTo,
CancellationToken token) where T : struct
{
for (int i = 0; i < copyFrom.Length; i++)
{
if (token.IsCancellationRequested)
return;

copyTo[i] = copyFrom[i];
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions src/UnityWebBrowser.UnityProject/Assets/Scripts/UWBPrjDebugUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ private void Start()
unformattedConsoleItems = new List<string> { startMessage };
formattedConsoleItems = new List<string> { startMessage };
popups = new List<WebBrowserPopupInfo>();

if(webBrowserUIBasic.browserClient.HasDisposed)
return;

webBrowserUIBasic.browserClient.processLogHandler.OnProcessOutputLog += HandleOutputLogMessage;
webBrowserUIBasic.browserClient.processLogHandler.OnProcessErrorLog += HandleErrorLogMessage;

Expand Down Expand Up @@ -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;
Expand All @@ -103,6 +110,9 @@ private void Update()
private void OnDestroy()
{
UImGuiUtility.Layout -= OnImGuiLayout;

if(webBrowserUIBasic.browserClient.HasDisposed)
return;

getPixelsMarker.Dispose();
applyTextureMarker.Dispose();
Expand All @@ -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)
Expand Down

0 comments on commit 3591def

Please sign in to comment.