From 34f4485d4d919ed99db885a841d6d992ddfdd024 Mon Sep 17 00:00:00 2001 From: Alexander Hjorth Date: Thu, 20 Feb 2025 16:46:24 +0100 Subject: [PATCH 1/4] initial multithread --- module/source/hooks/use-unity-arguments.ts | 1 + module/source/types/unity-arguments.ts | 7 +++++++ module/source/types/unity-config.ts | 1 + 3 files changed, 9 insertions(+) diff --git a/module/source/hooks/use-unity-arguments.ts b/module/source/hooks/use-unity-arguments.ts index 657dd04a..0f41d6ec 100644 --- a/module/source/hooks/use-unity-arguments.ts +++ b/module/source/hooks/use-unity-arguments.ts @@ -16,6 +16,7 @@ const useUnityArguments = (unityProps: UnityProps): UnityArguments => { dataUrl: unityProps.unityProvider.unityConfig.dataUrl, frameworkUrl: unityProps.unityProvider.unityConfig.frameworkUrl, codeUrl: unityProps.unityProvider.unityConfig.codeUrl, + workerUrl: unityProps.unityProvider.unityConfig.workerUrl || undefined, // Assigns the optional streaming assets URL, memory URL, and symbols URL // to the Unity arguments object. diff --git a/module/source/types/unity-arguments.ts b/module/source/types/unity-arguments.ts index a70ef9df..57e76125 100644 --- a/module/source/types/unity-arguments.ts +++ b/module/source/types/unity-arguments.ts @@ -29,6 +29,13 @@ type UnityArguments = { */ readonly codeUrl: string; + /** + * The url to the web worker file generated by Unity. When using a relative url, + * keep in mind this is relative from the path where your html file is served. + * It is also possible to use an absolute url, for example when using a CDN. + */ + readonly workerUrl?: string; + /** * The url where the streaming assets can be found. When using a relative url, * keep in mind this is relative from the path where your html file is served. diff --git a/module/source/types/unity-config.ts b/module/source/types/unity-config.ts index 061a10a3..a4ee8b35 100644 --- a/module/source/types/unity-config.ts +++ b/module/source/types/unity-config.ts @@ -10,6 +10,7 @@ type ConfigurableUnityArguments = Pick< | "dataUrl" | "frameworkUrl" | "codeUrl" + | "workerUrl" | "streamingAssetsUrl" | "memoryUrl" | "symbolsUrl" From 468225b381a10b9e15655e0acb05da42e3398c0e Mon Sep 17 00:00:00 2001 From: Alexander Hjorth Date: Thu, 20 Feb 2025 21:49:41 +0100 Subject: [PATCH 2/4] documentation --- documentation/docs/api/web-worker.md | 33 ++++++++++++++++++++++++++++ documentation/sidebars.json | 5 +++++ 2 files changed, 38 insertions(+) create mode 100644 documentation/docs/api/web-worker.md diff --git a/documentation/docs/api/web-worker.md b/documentation/docs/api/web-worker.md new file mode 100644 index 00000000..92a2ffca --- /dev/null +++ b/documentation/docs/api/web-worker.md @@ -0,0 +1,33 @@ +# Using a Web Worker + +When using a web worker, you can provide the URL to the Unity Web Worker file. +Web Workers are particularly useful when your Unity application performs heavy computations or processing that could potentially block the main thread. By moving these operations to a separate thread, your application remains responsive to user interactions. + +## Type Definition + +```ts title="Type Definition" +type UnityConfig = { + readonly workerUrl?: string; +}; +``` + +## Example Usage + +Here's a basic implementation showing how to configure the web worker. In this example, we'll set the worker URL to point to the Unity worker file in the build directory. + +```jsx showLineNumbers title="App.jsx" +import React from "react"; +import { Unity, useUnityContext } from "react-unity-webgl"; + +function App() { + const { unityProvider } = useUnityContext({ + loaderUrl: "build/myunityapp.loader.js", + dataUrl: "build/myunityapp.data", + frameworkUrl: "build/myunityapp.framework.js", + codeUrl: "build/myunityapp.wasm", + workerUrl: "build/myunityapp.worker.js", + }); + + return ; +} +``` diff --git a/documentation/sidebars.json b/documentation/sidebars.json index 00812539..829f71ea 100644 --- a/documentation/sidebars.json +++ b/documentation/sidebars.json @@ -201,6 +201,11 @@ "type": "doc", "id": "api/auto-sync-persistent-data-path", "label": "Auto Sync Persistent Data Path" + }, + { + "type": "doc", + "id": "api/web-worker", + "label": "Using a Web Worker" } ] } From 559c0ce5f691925c7cd1823a16ec32f41d655b9c Mon Sep 17 00:00:00 2001 From: Alexander Hjorth Date: Thu, 20 Feb 2025 21:59:24 +0100 Subject: [PATCH 3/4] update argument --- module/source/hooks/use-unity-arguments.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/module/source/hooks/use-unity-arguments.ts b/module/source/hooks/use-unity-arguments.ts index 0f41d6ec..706c91b3 100644 --- a/module/source/hooks/use-unity-arguments.ts +++ b/module/source/hooks/use-unity-arguments.ts @@ -16,14 +16,14 @@ const useUnityArguments = (unityProps: UnityProps): UnityArguments => { dataUrl: unityProps.unityProvider.unityConfig.dataUrl, frameworkUrl: unityProps.unityProvider.unityConfig.frameworkUrl, codeUrl: unityProps.unityProvider.unityConfig.codeUrl, - workerUrl: unityProps.unityProvider.unityConfig.workerUrl || undefined, - // Assigns the optional streaming assets URL, memory URL, and symbols URL - // to the Unity arguments object. + // Assigns the optional streaming assets URL, memory URL, symbols URL, + // and worker URL to the Unity arguments object streamingAssetsUrl: unityProps.unityProvider.unityConfig.streamingAssetsUrl, memoryUrl: unityProps.unityProvider.unityConfig.memoryUrl, symbolsUrl: unityProps.unityProvider.unityConfig.symbolsUrl, + workerUrl: unityProps.unityProvider.unityConfig.workerUrl, // Assigns the optional company name, product name, and product version to // the Unity arguments object. From 4a26bff78929e1a0f7d739a4210111b4fc31fcdf Mon Sep 17 00:00:00 2001 From: Alexander Hjorth Date: Thu, 20 Feb 2025 22:00:40 +0100 Subject: [PATCH 4/4] missed punctuation --- module/source/hooks/use-unity-arguments.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module/source/hooks/use-unity-arguments.ts b/module/source/hooks/use-unity-arguments.ts index 706c91b3..6e080026 100644 --- a/module/source/hooks/use-unity-arguments.ts +++ b/module/source/hooks/use-unity-arguments.ts @@ -18,7 +18,7 @@ const useUnityArguments = (unityProps: UnityProps): UnityArguments => { codeUrl: unityProps.unityProvider.unityConfig.codeUrl, // Assigns the optional streaming assets URL, memory URL, symbols URL, - // and worker URL to the Unity arguments object + // and worker URL to the Unity arguments object. streamingAssetsUrl: unityProps.unityProvider.unityConfig.streamingAssetsUrl, memoryUrl: unityProps.unityProvider.unityConfig.memoryUrl,