Skip to content

Commit f1b1639

Browse files
committed
Merge branch 'feature/headless' into 'master'
Headless See merge request voltstro-studios/uwb/unitywebbrowser!16
2 parents 83689b3 + f5052ec commit f1b1639

7 files changed

Lines changed: 1244 additions & 25 deletions

File tree

docs/articles/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
href: user/javascript.md
2424
- name: Client Events
2525
href: user/events.md
26+
- name: Runtime Creation
27+
href: user/runtime.md
2628
- name: Player Build
2729
href: user/player-build.md
2830

docs/articles/user/runtime.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Runtime Creation
2+
3+
The <xref:VoltstroStudios.UnityWebBrowser.Core.WebBrowserClient> can be created manually at runtime without the usage of <xref:VoltstroStudios.UnityWebBrowser.WebBrowserUIBasic> or <xref:VoltstroStudios.UnityWebBrowser.WebBrowserUIFull>.
4+
5+
When instantiating the class, you will need to set all required properties yourself.
6+
7+
> [!NOTE]
8+
> <xref:VoltstroStudios.UnityWebBrowser.Communication.CommunicationLayer> and <xref:VoltstroStudios.UnityWebBrowser.Core.Engines.Engine> objects are Unity's [ScriptableObject](https://docs.unity3d.com/2021.3/Documentation/Manual/class-ScriptableObject.html). The associated properties on <xref:VoltstroStudios.UnityWebBrowser.Core.WebBrowserClient> must be set to a ScriptableObject instance, or created at runtime through the [`ScriptableObject.CreateInstance()`](https://docs.unity3d.com/2021.3/Documentation/ScriptReference/ScriptableObject.CreateInstance.html) method.
9+
10+
There is a sample script that creates everything including the canvas, the <xref:VoltstroStudios.UnityWebBrowser.Core.WebBrowserClient.engine>, the <xref:VoltstroStudios.UnityWebBrowser.Core.WebBrowserClient.communicationLayer> and the <xref:VoltstroStudios.UnityWebBrowser.Core.WebBrowserClient> instance it self. You can import the script straight into your Unity project via the sample sections of the UWB package, or you can [view the script in GitHub](https://github.com/Voltstro-Studios/UnityWebBrowser/blob/master/src/Packages/UnityWebBrowser/Samples~/Runtime/Scripts/UWBRuntime.cs).
11+
12+
## Headless Mode
13+
14+
<xref:VoltstroStudios.UnityWebBrowser.Core.WebBrowserClient> constructor includes a `headless` parameter. When set to `true`, UWB will run in headless mode.
15+
16+
Headless mode will not create a <xref:VoltstroStudios.UnityWebBrowser.Core.WebBrowserClient.BrowserTexture>, and it will not run UWB's internal pixel data thread. All other methods that the <xref:VoltstroStudios.UnityWebBrowser.Core.WebBrowserClient> has are available.

src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ public Resolution Resolution
203203
public bool ignoreLogProcessJsonErrors = false;
204204

205205
/// <summary>
206-
/// Texture that the browser will paint to
206+
/// Texture that the browser will paint to.
207+
/// <para>In headless mode this will be null</para>
207208
/// </summary>
208209
public Texture2D BrowserTexture { get; private set; }
209210

@@ -294,6 +295,8 @@ public IWebBrowserLogger Logger
294295

295296
#endregion
296297

298+
private bool headless;
299+
private bool hasInitialized;
297300
private EngineProcess engineProcess;
298301
private WebBrowserCommunicationsManager communicationsManager;
299302
private CancellationTokenSource cancellationSource;
@@ -302,16 +305,33 @@ public IWebBrowserLogger Logger
302305
private NativeArray<byte> textureData;
303306
internal NativeArray<byte> nextTextureData;
304307

305-
internal WebBrowserClient()
308+
/// <summary>
309+
/// Creates a new <see cref="WebBrowserClient"/> instance
310+
/// </summary>
311+
/// <param name="headless">
312+
/// Creates the browser client in headless mode.
313+
/// Headless mode will not create a <see cref="BrowserTexture"/> for you to use
314+
/// </param>
315+
public WebBrowserClient(bool headless = false)
306316
{
317+
this.headless = headless;
307318
}
308319

309320
/// <summary>
310-
/// Inits the browser client
321+
/// Inits the browser client.
322+
/// In normal operation you do not need to call this method.
311323
/// </summary>
312324
/// <exception cref="FileNotFoundException"></exception>
313-
internal void Init()
325+
/// <exception cref="InitializationException"></exception>
326+
public void Init()
314327
{
328+
//Initialized check
329+
if (hasInitialized)
330+
throw new InitializationException("The browser client has already been initialized!");
331+
332+
hasInitialized = true;
333+
334+
//OS support check
315335
if (!WebBrowserUtils.IsRunningOnSupportedPlatform())
316336
{
317337
logger.Warn("UWB is not supported on the current runtime platform! Not running.");
@@ -335,14 +355,16 @@ internal void Init()
335355
communicationLayer.IsInUse = true;
336356

337357
//Setup texture
338-
BrowserTexture = new Texture2D((int)resolution.Width, (int)resolution.Height, TextureFormat.BGRA32, false,
339-
false);
340-
WebBrowserUtils.SetAllTextureColorToOne(BrowserTexture, backgroundColor);
341-
342-
resizeLock = new object();
343-
textureData = BrowserTexture.GetRawTextureData<byte>();
344-
nextTextureData = new NativeArray<byte>(textureData.ToArray(), Allocator.Persistent);
345-
358+
if (!headless)
359+
{
360+
BrowserTexture = new Texture2D((int)resolution.Width, (int)resolution.Height, TextureFormat.BGRA32, false,
361+
false);
362+
WebBrowserUtils.SetAllTextureColorToOne(BrowserTexture, backgroundColor);
363+
resizeLock = new object();
364+
textureData = BrowserTexture.GetRawTextureData<byte>();
365+
nextTextureData = new NativeArray<byte>(textureData.ToArray(), Allocator.Persistent);
366+
}
367+
346368
string browserEngineMainDir = WebBrowserUtils.GetAdditionFilesDirectory();
347369

348370
//Start to build our arguments
@@ -418,11 +440,6 @@ internal void Init()
418440
argsBuilder.AppendArgument("ignore-ssl-errors-domains", string.Join(",", ignoreSslErrorsDomains));
419441
}
420442

421-
//Make sure not to include this, its for testing
422-
#if UWB_ENGINE_PRJ //Define for backup, cause I am dumb as fuck and gonna accidentally include this in a release build one day
423-
//argsBuilder.AppendArgument("start-delay", 2000);
424-
#endif
425-
426443
//Disable sandbox
427444
if (noSandbox)
428445
{
@@ -542,6 +559,10 @@ internal async UniTaskVoid EngineReady()
542559
}
543560
}
544561

562+
//Create pixel data loop in headless mode
563+
if(headless)
564+
return;
565+
545566
Thread pixelDataLoopThread = new(PixelDataLoop)
546567
{
547568
Name = "UWB Pixel Data Loop Thread"
@@ -955,17 +976,20 @@ public void Resize(Resolution newResolution)
955976
{
956977
CheckIfIsReadyAndConnected();
957978

958-
lock (resizeLock)
979+
if (!headless)
959980
{
960-
BrowserTexture.Reinitialize((int)newResolution.Width, (int)newResolution.Height);
961-
textureData = BrowserTexture.GetRawTextureData<byte>();
962-
communicationsManager.Resize(newResolution);
981+
lock (resizeLock)
982+
{
983+
BrowserTexture.Reinitialize((int)newResolution.Width, (int)newResolution.Height);
984+
textureData = BrowserTexture.GetRawTextureData<byte>();
963985

964-
nextTextureData.Dispose();
965-
nextTextureData = new NativeArray<byte>(textureData.ToArray(), Allocator.Persistent);
966-
communicationsManager.pixelsEventTypeReader.SetPixelDataArray(nextTextureData);
986+
nextTextureData.Dispose();
987+
nextTextureData = new NativeArray<byte>(textureData.ToArray(), Allocator.Persistent);
988+
communicationsManager.pixelsEventTypeReader.SetPixelDataArray(nextTextureData);
989+
}
967990
}
968-
991+
992+
communicationsManager.Resize(newResolution);
969993
logger.Debug($"Resized to {newResolution}.");
970994
}
971995

0 commit comments

Comments
 (0)