Skip to content
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

UnityContent: support multiple event handlers #110

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
distribution
distribution
react-unity-webgl-*.tgz
55 changes: 38 additions & 17 deletions source/UnityContent.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import IUnityConfig from "./interfaces/IUnityConfig";
import IUnityEvent from "./interfaces/IUnityEvent";
import UnityComponent from "./components/Unity";
import UnityEvents from "./UnityEvents";
import "./declarations/UnityLoader";
import "./declarations/UnityInstance";
import "./declarations/ReactUnityWebgl";
import "./declarations/ReactUnityWebGL";
import { loggingService } from "./services/LoggingService";

// event names on which this.triggerUnityEvent() is called
const InstanceEventNames: string[] = [
'error',
'loaded',
'progress',
'quitted',
'resized',
];

export default class UnityContent {
/**
* the relative path to the build json file generated by Unity.
Expand Down Expand Up @@ -43,11 +52,18 @@ export default class UnityContent {
public unityConfig: IUnityConfig;

/**
* The registered Unity Events.
* @type {IUnityEvent[]}
* @public
* The registered instance Unity Events.
* @type {UnityEvents}
* @private
*/
private unityEvents: IUnityEvent[];
private unityInstanceEvents: UnityEvents;

/**
* The registered global Unity Events associated with ReactUnityWebGL object.
* @type {UnityEvents}
* @private
*/
static unityGlobalEvents: UnityEvents = new UnityEvents();

/**
* The unique ID helps seperating multiple
Expand Down Expand Up @@ -82,7 +98,7 @@ export default class UnityContent {
this.buildJsonPath = buildJsonPath;
this.unityLoaderJsPath = unityLoaderJsPath;
this.uniqueID = ++UnityContent.uniqueID;
this.unityEvents = [];
this.unityInstanceEvents = new UnityEvents();
this.unityConfig = {
modules: _unityConfig.modules || {},
unityVersion: _unityConfig.unityVersion || "undefined",
Expand Down Expand Up @@ -169,13 +185,20 @@ export default class UnityContent {
* @public
*/
public on(eventName: string, eventCallback: Function): any {
this.unityEvents.push({
eventName: eventName,
eventCallback: eventCallback
});
(window as any).ReactUnityWebGL[eventName] = (parameter: any) => {
return eventCallback(parameter);
};
if (InstanceEventNames.find(name => name === eventName)) {
// instance event - add to instance events
this.unityInstanceEvents.AddEventListener(eventName, eventCallback);

} else {
// ReactUnityWebGL event - add to class events
UnityContent.unityGlobalEvents.AddEventListener(eventName, eventCallback);

// install global event handler (if necessary)
if (typeof (window as any).ReactUnityWebGL[eventName] === 'undefined') {
(window as any).ReactUnityWebGL[eventName] = (parameter: any) =>
UnityContent.unityGlobalEvents.DispatchEvent(eventName, parameter);
}
}
}

/**
Expand All @@ -186,8 +209,6 @@ export default class UnityContent {
* @public
*/
public triggerUnityEvent(eventName: string, eventValue?: any): void {
for (let _i = 0; _i < this.unityEvents.length; _i++)
if (this.unityEvents[_i].eventName === eventName)
this.unityEvents[_i].eventCallback(eventValue);
this.unityInstanceEvents.DispatchEvent(eventName, eventValue);
}
}
49 changes: 49 additions & 0 deletions source/UnityEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
export default class UnityEvents {
/**
* Mapping event name to list of callbacks
* @type {Map<string, Function[]>}
* @private
*/
private map: Map<string, Function[]>;

constructor() {
this.map = new Map<string, Function[]>();
}

/**
* Adds a callback for eventName
* @param eventName the event name.
* @param callback false the public method name.
*/
public AddEventListener(
eventName: string,
callback: Function
): void {
if (!this.map.has(eventName)) {
this.map.set(eventName, []);
}
this.map.get(eventName)!.push(callback);
}

/**
* Dispatch eventName to all registered callbacks
* @param eventName the event name
* @param parameter the event parameter
*/
public DispatchEvent(
eventName: string,
parameter: any
): void {
if (!this.map.has(eventName)) return;

// the first callack that returns true aborts dispatch
this.map.get(eventName)!.find(callback => {
try {
return callback(parameter);
} catch (e) {
// ignore errors and keep on dispatching the event
return;
}
});
}
}
2 changes: 1 addition & 1 deletion source/components/Unity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import UnityContent from "../UnityContent";
import UnityLoaderService from "../services/UnityLoaderService";
import "../declarations/UnityLoader";
import "../declarations/UnityInstance";
import "../declarations/ReactUnityWebgl";
import "../declarations/ReactUnityWebGL";

export default class Unity extends React.Component<IUnityProps, IUnityState> {
/**
Expand Down
14 changes: 0 additions & 14 deletions source/interfaces/IUnityEvent.ts

This file was deleted.