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
Copy file name to clipboardExpand all lines: README.md
+54Lines changed: 54 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -181,3 +181,57 @@ export async function fetchLoaderConfig(
181
181
```
182
182
183
183
You can then use it to construct a `UnityContext` and pass this context to your `UnityRenderer` via the `context` prop.
184
+
185
+
## Module augmentation
186
+
187
+
Take the following example:
188
+
189
+
```typescript
190
+
// create some context
191
+
const ctx =newUnityContext({ ... });
192
+
193
+
// handles some "info" event with one parameter of type string
194
+
ctx.on('info', (message:string) => {
195
+
console.log(message);
196
+
});
197
+
```
198
+
199
+
The parameter `message` has to be explicitly defined as `string` each time a handler of for the event name `info` would be registered.
200
+
In order to make use of TypeScript to its fullest extent, you can augment an Interface of the library to get autocompletion and type-safety features here.
201
+
202
+
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:
203
+
204
+
```typescript
205
+
// must be imported, else the module will be redefined,
206
+
// and this causes all sorts of errors.
207
+
import'react-unity-renderer';
208
+
209
+
// module augmentation
210
+
declaremodule'react-unity-renderer' {
211
+
// this is the interface providing autocompletion
212
+
interfaceEventSignatures {
213
+
// "info" is the event name
214
+
// the type on the right side is anything that would match TypeScript's
215
+
// Parameters<> helper type
216
+
info: [message: string];
217
+
218
+
// also possible:
219
+
info: [string];
220
+
'some-event': [number, debug: string];
221
+
// note that all parametrs names are just labels, so they are fully optional.
222
+
}
223
+
}
224
+
```
225
+
226
+
Now, any defined event will be auto-completed with its types for `UnityContext.on(...)`:
0 commit comments