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" } ] } diff --git a/module/source/hooks/use-unity-arguments.ts b/module/source/hooks/use-unity-arguments.ts index 657dd04a..6e080026 100644 --- a/module/source/hooks/use-unity-arguments.ts +++ b/module/source/hooks/use-unity-arguments.ts @@ -17,12 +17,13 @@ const useUnityArguments = (unityProps: UnityProps): UnityArguments => { frameworkUrl: unityProps.unityProvider.unityConfig.frameworkUrl, codeUrl: unityProps.unityProvider.unityConfig.codeUrl, - // 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. 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"