Skip to content

Commit 755a76a

Browse files
Merge pull request #194 from jeffreylanters/feature/rewrite-for-react-hooks-support
Updated documentation with Hooks and implemented remove event listener method
2 parents 6e851ff + a4767f8 commit 755a76a

File tree

8 files changed

+269
-189
lines changed

8 files changed

+269
-189
lines changed

README.md

Lines changed: 225 additions & 174 deletions
Large diffs are not rendered by default.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-unity-webgl",
3-
"version": "8.3.9",
3+
"version": "8.4.0",
44
"description": "React Unity WebGL provides an easy solution for embedding Unity WebGL builds in your React application, with two-way communication between your React and Unity application with advanced API's.",
55
"keywords": [
66
"React",

source/components/unity.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import "../declarations/unity-instance";
2-
import "../declarations/global";
31
import { createElement, PureComponent } from "react";
42
import IUnityProps from "../interfaces/unity-props";
53
import UnityContext from "../models/unity-context";
64
import UnityLoaderService from "../services/unity-loader-service";
75
import IUnityInstanceParameters from "../interfaces/unity-instance-parameters";
86

7+
// TODO turn into functional component
98
export default class Unity extends PureComponent<IUnityProps, {}> {
109
/**
1110
* The UnityContext passed by the props.
@@ -92,7 +91,8 @@ export default class Unity extends PureComponent<IUnityProps, {}> {
9291
if (this.props.devicePixelRatio !== undefined)
9392
_unityInstanceParameters.devicePixelRatio = this.props.devicePixelRatio;
9493
if (this.props.matchWebGLToCanvasSize !== undefined)
95-
_unityInstanceParameters.matchWebGLToCanvasSize = this.props.matchWebGLToCanvasSize;
94+
_unityInstanceParameters.matchWebGLToCanvasSize =
95+
this.props.matchWebGLToCanvasSize;
9696
const _unityInstance = await createUnityInstance(
9797
this.htmlCanvasElementReference!,
9898
_unityInstanceParameters,

source/models/unity-context.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import "../declarations/global";
21
import IUnityConfig from "../interfaces/unity-config";
32
import IUnityEvent from "../interfaces/unity-event";
43
import IUnityContextEventMap from "../interfaces/unity-context-event-map";
54

5+
// TODO remove from dir
66
/**
77
* The Unity Context.
88
*/
@@ -78,9 +78,8 @@ export default class UnityContext {
7878
* system events like when the player is initialized or loader and
7979
* your custom events from Unity.
8080
* @public
81-
* @param {string} eventName the event name
82-
* @param {Function} eventListener the event function
83-
* @returns {any} The Function
81+
* @param {string} eventName the event's name
82+
* @param {Function} eventListener the event's function
8483
*/
8584
public on<MapKey extends keyof IUnityContextEventMap | (string & {})>(
8685
eventName: keyof IUnityContextEventMap | (MapKey & {}),
@@ -96,11 +95,38 @@ export default class UnityContext {
9695
eventListener(...parameters);
9796
}
9897

98+
/**
99+
* Removes all the Event Listeners with a specific Event Name.
100+
* @public
101+
* @param {string} eventName the event's name
102+
* @example unityContext.removeEventListener("progress");
103+
*/
104+
public removeEventListener(eventName: string): void {
105+
// TODO refactor to "off"?
106+
for (let _i = 0; _i < this.unityEvents.length; _i++)
107+
if (this.unityEvents[_i].eventName === eventName)
108+
this.unityEvents.splice(_i, 1);
109+
delete window.ReactUnityWebGL[eventName];
110+
}
111+
112+
/**
113+
* Removes all the Event Listeners.
114+
* @public
115+
* @example unityContext.removeAllEventListeners();
116+
*/
117+
public removeAllEventListeners(): void {
118+
// TODO refactor to "off"?
119+
for (let _i = 0; _i < this.unityEvents.length; _i++)
120+
delete window.ReactUnityWebGL[this.unityEvents[_i].eventName];
121+
this.unityEvents = [];
122+
}
123+
99124
/**
100125
* Dispatches an event listener that has been registered using the on method.
101126
* @public
102-
* @param {string} eventName the event name
103-
* @param {any} eventValue the event value
127+
* @param {string} eventName the event's name
128+
* @param {any} eventValue the event's value
129+
* @example unityContext.dispatchEventListener("gameOver", 180);
104130
*/
105131
public dispatchEventListener(eventName: string, eventValue?: any): void {
106132
for (let _unityEvent of this.unityEvents)
@@ -109,7 +135,7 @@ export default class UnityContext {
109135
}
110136

111137
/**
112-
* Enables or disabled the fullscreen mode of the UnityInstance.
138+
* Enables or disabled the Fullscreen mode of the Unity Instance.
113139
* @public
114140
* @param {boolean} enabled
115141
*/

source/services/unity-loader-service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// TODO turn into hook
12
export default class UnityLoaderService {
23
/**
34
* A reference to all UnityLoader script tags and their respective urls.

source/declarations/global.ts renamed to typings/global.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import IUnityInstanceParameters from "../interfaces/unity-instance-parameters";
1+
import IUnityInstanceParameters from "../source/interfaces/unity-instance-parameters";
22

33
/**
44
* Type declaration for global types.
55
*/
66
declare global {
77
/**
8-
* Type declaration for the ReactUnityWebGL object.
8+
* Type declaration for the ReactUnityWebGL Object.
99
*/
10-
var ReactUnityWebGL: { [eventName: string]: Function };
10+
var ReactUnityWebGL: {
11+
[eventName: string]: Function;
12+
};
1113

1214
/**
1315
* Creates a new UnityInstance.
File renamed without changes.

0 commit comments

Comments
 (0)