diff --git a/.npmignore b/.npmignore
new file mode 100644
index 00000000..4ec17ca2
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,7 @@
+node_modules
+.npm
+.node_repl_history
+logs
+*.log
+npm-debug.log*
+source
diff --git a/README.md b/README.md
index c6b1f437..2030888b 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,5 @@
-# React Unity WebGL
+# React Unity WebGL · []() []() []() []() []()
+
When building content for the web, you might need to communicate with other elements on React Application. Or you might want to implement functionality using Web APIs which [Unity](https://unity3d.com) does not currently expose by default. In both cases, you need to directly interface with the browser’s JavaScript engine. React Unity WebGL provides an easy library for Unity 5.6 / 2017 or newer with different methods to do this.

@@ -85,28 +86,28 @@ this.myCustomModule = { ... }
# Calling Unity scripts functions from JavaScript in React
-Sometimes you need to send some data or notification to the Unity script from the browser’s JavaScript. The recommended way of doing it is to call methods on GameObjects in your content. To get started import the function SendMessage from react-unity-webgl.
+Sometimes you need to send some data or notification to the Unity script from the browser’s JavaScript. The recommended way of doing it is to call methods on GameObjects in your content. To get started import the class UnityEvent from react-unity-webgl.
```js
-SendMessage (objectName: String, methodName: String, value: Object): void;
+UnityEvent (objectName: String, methodName: String);
```
-Where objectName is the name of an object in your scene; methodName is the name of a method in the script, currently attached to that object; value can be a string, a number, or can be empty. For example:
+Where objectName is the name of an object in your scene; methodName is the name of a method in the script, currently attached to that object. When you've created a new UnityEvent, you can call the 'emit' function to fire it into Unity. You can pass an optional parameter value.
```js
import React from 'react'
-import { SendMessage } from 'react-unity-webgl'
+import { UnityEvent } from 'react-unity-webgl'
export class App extends React.Component {
- spawnEnemy (count) {
- SendMessage ('SpawnBehaviour', 'SpawnEnemies', count)
+ constructor () {
+ this.spawnEnemies = new UnityEvent ('SpawnBehaviour', 'SpawnEnemies')
}
render () {
- return
+ return
Click to Spawn 5 Enemies
}
}
```
-While in Unity, for example:
+While in Unity the following script is attached the a game object named 'SpawnBehaviour'.
```cs
using UnityEngine;
@@ -187,4 +188,4 @@ Simple numeric types can be passed to JavaScript in function parameters without
# Contributing
-When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change. Before commiting, please compile your code using `npm run compile` and open a pull request. Thank you very much!
\ No newline at end of file
+When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change. Before commiting, please compile your code using `npm run compile` and open a pull request. Thank you very much!
diff --git a/library/index.js b/library/index.js
index 833a7241..186862e3 100644
--- a/library/index.js
+++ b/library/index.js
@@ -3,7 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
-exports.SendMessage = exports.RegisterExternalListener = undefined;
+exports.UnityEvent = exports.SendMessage = exports.RegisterExternalListener = undefined;
var _Unity = require('./components/Unity');
@@ -13,8 +13,11 @@ var _RegisterExternalListener = require('./modules/RegisterExternalListener');
var _SendMessage = require('./modules/SendMessage');
+var _UnityEvent = require('./modules/UnityEvent');
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.default = _Unity2.default;
exports.RegisterExternalListener = _RegisterExternalListener.RegisterExternalListener;
-exports.SendMessage = _SendMessage.SendMessage;
\ No newline at end of file
+exports.SendMessage = _SendMessage.SendMessage;
+exports.UnityEvent = _UnityEvent.UnityEvent;
\ No newline at end of file
diff --git a/library/modules/RegisterExternalEmitter.js b/library/modules/RegisterExternalEmitter.js
new file mode 100644
index 00000000..22bf6cc4
--- /dev/null
+++ b/library/modules/RegisterExternalEmitter.js
@@ -0,0 +1,9 @@
+"use strict";
+
+Object.defineProperty(exports, "__esModule", {
+ value: true
+});
+exports.UnityEvent = UnityEvent;
+function UnityEvent(gameObjectName, functionName) {
+ return new function (parameter) {}();
+}
\ No newline at end of file
diff --git a/library/modules/RegisterExternalListener.js b/library/modules/RegisterExternalListener.js
index 84627386..b2c8b3ae 100644
--- a/library/modules/RegisterExternalListener.js
+++ b/library/modules/RegisterExternalListener.js
@@ -4,14 +4,7 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports.RegisterExternalListener = RegisterExternalListener;
-/**
- * Registers a listener to this window. When a message is sent
- * from Unity using 'CallExternal', the listener will forward it
- * into your React Application.
- * @param {string} functionName
- * @param {function} callback
- */
-function RegisterExternalListener(functionName, callback) {
+function RegisterExternalListener(methodName, callback) {
/**
* LEGACY
* bind the function to the window to allow
@@ -19,8 +12,8 @@ function RegisterExternalListener(functionName, callback) {
* Application.ExternalCall and
* Application.ExternalEval.
*/
- window[functionName] = function (paramterValue) {
- callback(paramterValue);
+ window[methodName] = function (parameter) {
+ callback(parameter);
};
/**
@@ -29,7 +22,7 @@ function RegisterExternalListener(functionName, callback) {
* to make direct calls into React.
*/
if (typeof window.ReactUnityWebGL === 'undefined') window.ReactUnityWebGL = {};
- window.ReactUnityWebGL[functionName] = function (paramterValue) {
- callback(paramterValue);
+ window.ReactUnityWebGL[methodName] = function (parameter) {
+ callback(parameter);
};
}
\ No newline at end of file
diff --git a/library/modules/SendMessage.js b/library/modules/SendMessage.js
index 294880e1..c73df32b 100644
--- a/library/modules/SendMessage.js
+++ b/library/modules/SendMessage.js
@@ -7,16 +7,7 @@ exports.SendMessage = SendMessage;
var _Unity = require('../components/Unity');
-/**
- * Sends a message to the Unity content. This works the same
- * as Unity's internal 'SendMessage' system. The paramaterValue
- * is an optional field.
- * @param {string} gameObjectName
- * @param {string} methodName
- * @param {object} paramterValue
- */
function SendMessage(gameObjectName, methodName, paramterValue) {
- if (typeof paramterValue === 'undefined') paramterValue = '';
-
- if (typeof _Unity.UnityInstance !== 'undefined') _Unity.UnityInstance.SendMessage(gameObjectName, methodName, paramterValue);else console.warn('Wait for Unity to be instantiated before sending a message to \'' + gameObjectName + '\'');
+ console.warn('SendMessage is deprecated since version 6.4.0, use UnityEvent instead.');
+ if (typeof _Unity.UnityInstance !== 'undefined') _Unity.UnityInstance.SendMessage(gameObjectName, methodName, paramterValue || '');else console.warn('Wait for Unity to be instantiated before sending a message to \'' + gameObjectName + '\'');
}
\ No newline at end of file
diff --git a/library/modules/UnityEvent.js b/library/modules/UnityEvent.js
new file mode 100644
index 00000000..b6747570
--- /dev/null
+++ b/library/modules/UnityEvent.js
@@ -0,0 +1,35 @@
+'use strict';
+
+Object.defineProperty(exports, "__esModule", {
+ value: true
+});
+exports.UnityEvent = undefined;
+
+var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
+
+var _Unity = require('../components/Unity');
+
+function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
+
+var UnityEvent = exports.UnityEvent = function () {
+ function UnityEvent(gameObjectName, methodName) {
+ _classCallCheck(this, UnityEvent);
+
+ this.gameObjectName = gameObjectName;
+ this.methodName = methodName;
+ }
+
+ _createClass(UnityEvent, [{
+ key: 'emit',
+ value: function emit(parameter) {
+ if (this.canEmit() === true) _Unity.UnityInstance.SendMessage(this.gameObjectName, this.methodName, parameter || '');else console.warn('Wait for Unity to be instantiated before sending an event \'' + this.methodName + '\'');
+ }
+ }, {
+ key: 'canEmit',
+ value: function canEmit() {
+ return typeof _Unity.UnityInstance !== 'undefined';
+ }
+ }]);
+
+ return UnityEvent;
+}();
\ No newline at end of file
diff --git a/package.json b/package.json
index a78c74ec..c787c0cd 100644
--- a/package.json
+++ b/package.json
@@ -1,9 +1,9 @@
{
"name": "react-unity-webgl",
- "version": "6.3.1",
+ "version": "6.4.0",
"description": "A Unity WebGL component for your React application",
"main": "library/index.js",
- "types": "source/types.d.ts",
+ "types": "./types.d.ts",
"scripts": {
"compile": "babel --presets react source --out-dir library"
},
diff --git a/source/index.js b/source/index.js
index a851979d..139de465 100644
--- a/source/index.js
+++ b/source/index.js
@@ -1,6 +1,7 @@
import Unity from './components/Unity'
import { RegisterExternalListener } from './modules/RegisterExternalListener'
import { SendMessage } from './modules/SendMessage'
+import { UnityEvent } from './modules/UnityEvent'
export default Unity
-export { RegisterExternalListener, SendMessage }
\ No newline at end of file
+export { RegisterExternalListener, SendMessage, UnityEvent }
\ No newline at end of file
diff --git a/source/modules/RegisterExternalListener.js b/source/modules/RegisterExternalListener.js
index a430ebeb..eed8a57f 100644
--- a/source/modules/RegisterExternalListener.js
+++ b/source/modules/RegisterExternalListener.js
@@ -1,11 +1,4 @@
-/**
- * Registers a listener to this window. When a message is sent
- * from Unity using 'CallExternal', the listener will forward it
- * into your React Application.
- * @param {string} functionName
- * @param {function} callback
- */
-export function RegisterExternalListener (functionName, callback) {
+export function RegisterExternalListener (methodName, callback) {
/**
* LEGACY
* bind the function to the window to allow
@@ -13,8 +6,8 @@ export function RegisterExternalListener (functionName, callback) {
* Application.ExternalCall and
* Application.ExternalEval.
*/
- window[functionName] = paramterValue => {
- callback (paramterValue)
+ window[methodName] = parameter => {
+ callback (parameter)
}
/**
@@ -24,7 +17,7 @@ export function RegisterExternalListener (functionName, callback) {
*/
if (typeof window.ReactUnityWebGL === 'undefined')
window.ReactUnityWebGL = {}
- window.ReactUnityWebGL[functionName] = paramterValue => {
- callback (paramterValue)
+ window.ReactUnityWebGL[methodName] = parameter => {
+ callback (parameter)
}
}
\ No newline at end of file
diff --git a/source/modules/SendMessage.js b/source/modules/SendMessage.js
index bbf47098..06c7c179 100644
--- a/source/modules/SendMessage.js
+++ b/source/modules/SendMessage.js
@@ -1,19 +1,12 @@
import { UnityInstance } from '../components/Unity'
-/**
- * Sends a message to the Unity content. This works the same
- * as Unity's internal 'SendMessage' system. The paramaterValue
- * is an optional field.
- * @param {string} gameObjectName
- * @param {string} methodName
- * @param {object} paramterValue
- */
export function SendMessage (gameObjectName, methodName, paramterValue) {
- if (typeof paramterValue === 'undefined')
- paramterValue = ''
-
+ console.warn (`SendMessage is deprecated since version 6.4.0, use UnityEvent instead.`)
if (typeof UnityInstance !== 'undefined')
- UnityInstance.SendMessage (gameObjectName, methodName, paramterValue)
+ UnityInstance.SendMessage (
+ gameObjectName,
+ methodName,
+ paramterValue || '')
else
console.warn (`Wait for Unity to be instantiated before sending a message to '${gameObjectName}'`)
}
\ No newline at end of file
diff --git a/source/modules/UnityEvent.js b/source/modules/UnityEvent.js
new file mode 100644
index 00000000..1857e5ee
--- /dev/null
+++ b/source/modules/UnityEvent.js
@@ -0,0 +1,20 @@
+import { UnityInstance } from '../components/Unity'
+
+export class UnityEvent {
+ constructor (gameObjectName, methodName) {
+ this.gameObjectName = gameObjectName
+ this.methodName = methodName
+ }
+ emit (parameter) {
+ if (this.canEmit () === true)
+ UnityInstance.SendMessage (
+ this.gameObjectName,
+ this.methodName,
+ parameter || '')
+ else
+ console.warn (`Wait for Unity to be instantiated before sending an event '${this.methodName}'`)
+ }
+ canEmit () {
+ return typeof UnityInstance !== 'undefined'
+ }
+}
\ No newline at end of file
diff --git a/source/types.d.ts b/source/types.d.ts
deleted file mode 100644
index 0f730ed5..00000000
--- a/source/types.d.ts
+++ /dev/null
@@ -1,40 +0,0 @@
-export default class Unity extends React.Component
{ }
-
-export function SendMessage (
- gameObjectName: string,
- methodName: string,
- paramterValue?: any
-): void;
-
-export function RegisterExternalListener (
- functionName: string,
- callback: (e: any) => void
-): void;
-
-interface UnityProps {
- src: string;
- loader: string;
- onProgress?: (progression: number) => void;
- width?: number;
- height?: number;
- module?: Module;
-}
-
-interface UnityState {
- error?: string;
-}
-
-interface Module {
- [x: string]: any;
- preRun: any[];
- postRun: any[];
- print: (e: any) => void;
- printErr: (e: any) => void;
- Jobs: {
- [x: string]: any;
- };
- buildDownloadProgress: {
- [x: string]: any;
- };
- resolveBuildUrl: (e: any) => any;
-}
\ No newline at end of file
diff --git a/types.d.ts b/types.d.ts
new file mode 100644
index 00000000..fdfeafb4
--- /dev/null
+++ b/types.d.ts
@@ -0,0 +1,229 @@
+import * as React from 'react';
+
+/**
+ * Unity component will render the WebGL
+ * player.
+ * @type {React.Component}
+ */
+export default class Unity extends React.Component { }
+
+/**
+ * Sends a message to the Unity content.
+ * This works the same as Unity's internal
+ * 'SendMessage' system. The paramaterValue
+ * is an optional field.
+ * @deprecated since version 6.4.0
+ * @type {function}
+ * @param {string} gameObjectName
+ * @param {string} methodName
+ * @param {object} paramterValue
+ * @returns {void} void
+ */
+export function SendMessage (
+ /**
+ * The gameObject name within Unity
+ * within the hierarchy.
+ * @type {string}
+ */
+ gameObjectName: string,
+
+ /**
+ * The name of the method you want
+ * to call. The method must be on
+ * any script attached tot the
+ * game object and must be public.
+ * @type {string}
+ */
+ methodName: string,
+
+ /**
+ * Optional parameter value which
+ * will be passed to the opbject.
+ * @type {string}
+ */
+ paramterValue?: any
+): void;
+
+/**
+ * UnityEvent created an emittable event
+ * which can trigger a function within
+ * Unity with a given parameter.
+ * @type {class}
+ */
+export class UnityEvent {
+ /**
+ * Creates a new UnityEvent.
+ * @type {constructor}
+ * @param {string} gameObjectName
+ * @param {string} methodName
+ */
+ constructor (
+ /**
+ * The gameObject name within Unity
+ * within the hierarchy.
+ * @type {string}
+ */
+ gameObjectName: string,
+
+ /**
+ * The name of the method you want
+ * to call. The method must be on
+ * any script attached tot the
+ * game object and must be public.
+ * @type {string}
+ */
+ methodName: string
+ );
+
+ /**
+ * The gameObject name within Unity
+ * within the hierarchy.
+ * @type {string}
+ */
+ gameObjectName: string;
+
+ /**
+ * The name of the method you want
+ * to call. The method must be on
+ * any script attached tot the
+ * game object and must be public.
+ * @type {string}
+ */
+ methodName: string;
+
+ /**
+ * Emits the event to the defined
+ * gameObject with the given
+ * parameter.
+ * @type {function}
+ * @returns {void} void
+ */
+ emit: (
+ /**
+ * Optional parameter value which
+ * will be passed to the opbject.
+ */
+ parameter?: any
+ ) => void;
+
+ /**
+ * Check wether the event can be
+ * emitted to the unityContent.
+ * @type {function}
+ * @returns {boolean} canEmit
+ */
+ canEmit: () => boolean;
+}
+
+/**
+ * Registers a listener to this window.
+ * When a message is sent from Unity using
+ * 'CallExternal', the listener will
+ * forward it into your React Application.
+ * @type {function}
+ * @param {string} functionName
+ * @param {function} callback
+ */
+export function RegisterExternalListener (
+ /**
+ * The methodname which will be available
+ * from outside of react. This will be the
+ * method name you can use from your JSLib
+ * or CallExternal.
+ * @type {string}
+ */
+ methodName: string,
+
+ /**
+ * The callback event which will be triggered
+ * by unity. You can bind a function or create
+ * one.
+ * @type {function}
+ * @returns {void} void
+ */
+ callback: (
+ /**
+ * The parameter passed from the Unity
+ * content.
+ * @type {any}
+ */
+ parameter: any
+ ) => void
+): void;
+
+/**
+ * Props for the Unity Component
+ * @type {object}
+ */
+interface UnityProps {
+ /**
+ * Public path to the by Unity generated
+ * json file. This is in most cases relative
+ * from your HTML file.
+ * @type {string}
+ */
+ src: string;
+
+ /**
+ * Public path to the by Unity generated
+ * loader file. This is in most cases relative
+ * from your HTML file.
+ * @type {string}
+ */
+ loader: string;
+
+ /**
+ * Function which will be called every time
+ * the loading process updates.
+ * @type {function}
+ */
+ onProgress?: (
+ /**
+ * The number of progression. This will be
+ * a number between 0 and 1. When the
+ * content is loaded, 1 will be passed.
+ * @type {number}
+ */
+ progression: number
+ ) => void;
+
+ /**
+ * The width of the Unity component. If none
+ * is passed, the player have a width of 100%.
+ * @type {number}
+ */
+ width?: number;
+
+ /**
+ * The height of the Unity component. If none
+ * is passed, the player have a height of 100%.
+ * @type {number}
+ */
+ height?: number;
+
+ /**
+ * Optional modules which will override Unity's
+ * default module object.
+ * @type {Module}
+ */
+ module?: Module;
+}
+
+/**
+ * The state of the Unity compontent.
+ * @type {object}
+ */
+interface UnityState {
+ /**
+ * The error thrown by the compontent.
+ * If there is no error, error will be
+ * null.
+ * @type {string}
+ */
+ error?: string;
+}
+
+/**
+ * Unity Module Object
+ */
+interface Module { }
\ No newline at end of file