Skip to content

Add support for workerUrl (multithreading) #575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions documentation/docs/api/web-worker.md
Original file line number Diff line number Diff line change
@@ -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 <Unity unityProvider={unityProvider} />;
}
```
5 changes: 5 additions & 0 deletions documentation/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
Expand Down
5 changes: 3 additions & 2 deletions module/source/hooks/use-unity-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions module/source/types/unity-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions module/source/types/unity-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type ConfigurableUnityArguments = Pick<
| "dataUrl"
| "frameworkUrl"
| "codeUrl"
| "workerUrl"
| "streamingAssetsUrl"
| "memoryUrl"
| "symbolsUrl"
Expand Down
Loading