Skip to content

Commit 830a626

Browse files
authored
Add support for workerUrl (multithreading) (#575)
* initial multithread * documentation * update argument * missed punctuation
1 parent f0a55f2 commit 830a626

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

documentation/docs/api/web-worker.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Using a Web Worker
2+
3+
When using a web worker, you can provide the URL to the Unity Web Worker file.
4+
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.
5+
6+
## Type Definition
7+
8+
```ts title="Type Definition"
9+
type UnityConfig = {
10+
readonly workerUrl?: string;
11+
};
12+
```
13+
14+
## Example Usage
15+
16+
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.
17+
18+
```jsx showLineNumbers title="App.jsx"
19+
import React from "react";
20+
import { Unity, useUnityContext } from "react-unity-webgl";
21+
22+
function App() {
23+
const { unityProvider } = useUnityContext({
24+
loaderUrl: "build/myunityapp.loader.js",
25+
dataUrl: "build/myunityapp.data",
26+
frameworkUrl: "build/myunityapp.framework.js",
27+
codeUrl: "build/myunityapp.wasm",
28+
workerUrl: "build/myunityapp.worker.js",
29+
});
30+
31+
return <Unity unityProvider={unityProvider} />;
32+
}
33+
```

documentation/sidebars.json

+5
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@
201201
"type": "doc",
202202
"id": "api/auto-sync-persistent-data-path",
203203
"label": "Auto Sync Persistent Data Path"
204+
},
205+
{
206+
"type": "doc",
207+
"id": "api/web-worker",
208+
"label": "Using a Web Worker"
204209
}
205210
]
206211
}

module/source/hooks/use-unity-arguments.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ const useUnityArguments = (unityProps: UnityProps): UnityArguments => {
1717
frameworkUrl: unityProps.unityProvider.unityConfig.frameworkUrl,
1818
codeUrl: unityProps.unityProvider.unityConfig.codeUrl,
1919

20-
// Assigns the optional streaming assets URL, memory URL, and symbols URL
21-
// to the Unity arguments object.
20+
// Assigns the optional streaming assets URL, memory URL, symbols URL,
21+
// and worker URL to the Unity arguments object.
2222
streamingAssetsUrl:
2323
unityProps.unityProvider.unityConfig.streamingAssetsUrl,
2424
memoryUrl: unityProps.unityProvider.unityConfig.memoryUrl,
2525
symbolsUrl: unityProps.unityProvider.unityConfig.symbolsUrl,
26+
workerUrl: unityProps.unityProvider.unityConfig.workerUrl,
2627

2728
// Assigns the optional company name, product name, and product version to
2829
// the Unity arguments object.

module/source/types/unity-arguments.ts

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ type UnityArguments = {
2929
*/
3030
readonly codeUrl: string;
3131

32+
/**
33+
* The url to the web worker file generated by Unity. When using a relative url,
34+
* keep in mind this is relative from the path where your html file is served.
35+
* It is also possible to use an absolute url, for example when using a CDN.
36+
*/
37+
readonly workerUrl?: string;
38+
3239
/**
3340
* The url where the streaming assets can be found. When using a relative url,
3441
* keep in mind this is relative from the path where your html file is served.

module/source/types/unity-config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type ConfigurableUnityArguments = Pick<
1010
| "dataUrl"
1111
| "frameworkUrl"
1212
| "codeUrl"
13+
| "workerUrl"
1314
| "streamingAssetsUrl"
1415
| "memoryUrl"
1516
| "symbolsUrl"

0 commit comments

Comments
 (0)