You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> This project is heavily inspired by [react-unity-webgl](https://github.com/elraccoone/react-unity-webgl) made by Jeffrey Lanters. This implementation uses function components + hooks, is getting tested continously and has strict linting and formatting rules which are always enforced.
21
28
22
29
## Installation
23
30
@@ -49,7 +56,7 @@ import {
49
56
UnityContext,
50
57
UnityRenderer,
51
58
UnityLoaderConfig,
52
-
} from'react-unity-webgl';
59
+
} from'react-unity-renderer';
53
60
54
61
// get those URLs from your Unity WebGL build.
55
62
// you *could* put a JSON in your WebGL template containing this information
:warning: It is recommended to store the `UnityContext`, as well as the progress, ready and error states in a global state. This way you can keep track of the game state in every part of your application. Consider [zustand](https://github.com/pmndrs/zustand) as a lightweight alternative to Redux, MobX & co., as it has every feature needed for this use case and takes way less effort to implement.
103
+
101
104
## Mitigating the "keyboard capturing issue"
102
105
103
106
By default, Unity WebGL builds capture the keyboard as soon as they are loaded. This means that all keyboard input on the website is captured by the game, and rendering all `<input>`, `<textarea>` and similar input methods useless.
@@ -189,24 +192,93 @@ export async function fetchLoaderConfig(
189
192
190
193
You can then use it to construct a `UnityContext` and pass this context to your `UnityRenderer` via the `context` prop.
191
194
192
-
## Sending events from Unity
195
+
## Receiving events from Unity
196
+
197
+
### On the Unity side
193
198
194
-
In order to send events from Unity to the react application, use the global method for that in your `*.jslib` mapping file:
199
+
In order to send events from Unity to the React application, use the global method for that in your `*.jslib` mapping file:
If the event name has no registered event handlers, the `UnityBridge(event: string)` function will log a warning via `console.warn(...)`.
209
219
220
+
:warning: Please note that returning values from the `UnityBridge()` method is not supported, as it may call multiple event handlers internally from different `UnityContext`s that are listening for a certain event, e.g. when having two or more renderers in your application. The preferred way to handle this is to emit a message to the correct Unity instance, which this library also supports. This also helps making the communication paths simpler: **Events only go from Unity to JavaScript, Messages only go from JavaScript to Unity.**
@@ -227,23 +299,24 @@ In order to make use of TypeScript to its fullest extent, you can augment an Int
227
299
Put this either in a file importing `react-unity-renderer` or create a new `unity.d.ts` somewhere in your `src` or (if you have that) `typings` directory:
228
300
229
301
```typescript
230
-
//must be imported, else the module will be redefined,
231
-
//and this causes all sorts of errors.
232
-
import'react-unity-renderer';
302
+
//The "{} from" part just imports the TypeScript definitions, so
303
+
//we do not re-define the whole module, but just augment it.
304
+
import{} from'react-unity-renderer';
233
305
234
306
// module augmentation
235
307
declaremodule'react-unity-renderer' {
236
308
// this is the interface providing autocompletion
237
309
interfaceEventSignatures {
238
310
// "info" is the event name
239
-
//the type on the right side is anything that would match TypeScript's
240
-
// Parameters<> helper type
311
+
//The type on the right side is anything that would match TypeScript's
312
+
// Parameters<> helper type.
241
313
info: [message: string];
242
314
243
315
// also possible:
244
316
info: [string];
317
+
// Note that all parameter names are just labels, so they are fully optional.
318
+
// Though, they are displayed when autocompleting, so labels are quite helpful here.
245
319
'some-event': [number, debug: string];
246
-
// note that all parametrs names are just labels, so they are fully optional.
0 commit comments