Skip to content

Commit 48f6130

Browse files
committed
UnityContent: detect instance events
Events which are dispatched via triggerUnityEvents() are instance specific events that do not need a global event handler attached to ReactUnityWebGL object. All other event names are assumed to be global ones. Fixes jeffreylanters#109
1 parent a293feb commit 48f6130

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

source/UnityContent.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import "./declarations/UnityInstance";
66
import "./declarations/ReactUnityWebGL";
77
import { loggingService } from "./services/LoggingService";
88

9+
// event names on which this.triggerUnityEvent() is called
10+
const InstanceEventNames: string[] = [
11+
'error',
12+
'loaded',
13+
'progress',
14+
'quitted',
15+
'resized',
16+
];
17+
918
export default class UnityContent {
1019
/**
1120
* the relative path to the build json file generated by Unity.
@@ -176,12 +185,19 @@ export default class UnityContent {
176185
* @public
177186
*/
178187
public on(eventName: string, eventCallback: Function): any {
179-
this.unityInstanceEvents.AddEventListener(eventName, eventCallback);
180-
181-
UnityContent.unityGlobalEvents.AddEventListener(eventName, eventCallback);
182-
if (typeof (window as any).ReactUnityWebGL[eventName] === 'undefined') {
183-
(window as any).ReactUnityWebGL[eventName] = (parameter: any) =>
184-
UnityContent.unityGlobalEvents.DispatchEvent(eventName, parameter);
188+
if (InstanceEventNames.find(name => name === eventName)) {
189+
// instance event - add to instance events
190+
this.unityInstanceEvents.AddEventListener(eventName, eventCallback);
191+
192+
} else {
193+
// ReactUnityWebGL event - add to class events
194+
UnityContent.unityGlobalEvents.AddEventListener(eventName, eventCallback);
195+
196+
// install global event handler (if necessary)
197+
if (typeof (window as any).ReactUnityWebGL[eventName] === 'undefined') {
198+
(window as any).ReactUnityWebGL[eventName] = (parameter: any) =>
199+
UnityContent.unityGlobalEvents.DispatchEvent(eventName, parameter);
200+
}
185201
}
186202
}
187203

0 commit comments

Comments
 (0)