Skip to content

Commit 0f4406c

Browse files
author
Jeffrey Lanters
authored
Merge pull request #20 from jeffreylanters/development
UnityEvents
2 parents 93318b2 + 64fdc9a commit 0f4406c

14 files changed

+337
-102
lines changed

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules
2+
.npm
3+
.node_repl_history
4+
logs
5+
*.log
6+
npm-debug.log*
7+
source

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# React Unity WebGL
1+
# React Unity WebGL · [![license](https://img.shields.io/badge/license-MIT-red.svg)]() [![npm](https://img.shields.io/npm/v/react-unity-webgl.svg)]() [![npm](https://img.shields.io/badge/react-v12%3E-blue.svg)]() [![npm](https://img.shields.io/badge/build-passing-brightgreen.svg)]() [![npm](https://img.shields.io/npm/dt/react-unity-webgl.svg)]()
2+
23
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.
34

45
<img src="https://raw.githubusercontent.com/jeffreylanters/react-unity-webgl/master/resources/readme/logo.png" width="300px"><br />
@@ -85,28 +86,28 @@ this.myCustomModule = { ... }
8586

8687
<br/><br/><br/>
8788
# Calling Unity scripts functions from JavaScript in React
88-
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.
89+
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.
8990

9091
```js
91-
SendMessage (objectName: String, methodName: String, value: Object): void;
92+
UnityEvent (objectName: String, methodName: String);
9293
```
9394

94-
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:
95+
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.
9596
```js
9697
import React from 'react'
97-
import { SendMessage } from 'react-unity-webgl'
98+
import { UnityEvent } from 'react-unity-webgl'
9899

99100
export class App extends React.Component {
100-
spawnEnemy (count) {
101-
SendMessage ('SpawnBehaviour', 'SpawnEnemies', count)
101+
constructor () {
102+
this.spawnEnemies = new UnityEvent ('SpawnBehaviour', 'SpawnEnemies')
102103
}
103104
render () {
104-
return <div onClick={ this.spawnEnemy.bind(this, 5) }>
105+
return <div onClick={ this.spawnEnemies.emit (5) }>
105106
Click to Spawn 5 Enemies</div>
106107
}
107108
}
108109
```
109-
While in Unity, for example:
110+
While in Unity the following script is attached the a game object named 'SpawnBehaviour'.
110111
```cs
111112
using UnityEngine;
112113

@@ -187,4 +188,4 @@ Simple numeric types can be passed to JavaScript in function parameters without
187188

188189
<br/><br/><br/>
189190
# Contributing
190-
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!
191+
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!

library/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6-
exports.SendMessage = exports.RegisterExternalListener = undefined;
6+
exports.UnityEvent = exports.SendMessage = exports.RegisterExternalListener = undefined;
77

88
var _Unity = require('./components/Unity');
99

@@ -13,8 +13,11 @@ var _RegisterExternalListener = require('./modules/RegisterExternalListener');
1313

1414
var _SendMessage = require('./modules/SendMessage');
1515

16+
var _UnityEvent = require('./modules/UnityEvent');
17+
1618
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1719

1820
exports.default = _Unity2.default;
1921
exports.RegisterExternalListener = _RegisterExternalListener.RegisterExternalListener;
20-
exports.SendMessage = _SendMessage.SendMessage;
22+
exports.SendMessage = _SendMessage.SendMessage;
23+
exports.UnityEvent = _UnityEvent.UnityEvent;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.UnityEvent = UnityEvent;
7+
function UnityEvent(gameObjectName, functionName) {
8+
return new function (parameter) {}();
9+
}

library/modules/RegisterExternalListener.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,16 @@ Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
66
exports.RegisterExternalListener = RegisterExternalListener;
7-
/**
8-
* Registers a listener to this window. When a message is sent
9-
* from Unity using 'CallExternal', the listener will forward it
10-
* into your React Application.
11-
* @param {string} functionName
12-
* @param {function} callback
13-
*/
14-
function RegisterExternalListener(functionName, callback) {
7+
function RegisterExternalListener(methodName, callback) {
158
/**
169
* LEGACY
1710
* bind the function to the window to allow
1811
* the user to use the legacy functions
1912
* Application.ExternalCall and
2013
* Application.ExternalEval.
2114
*/
22-
window[functionName] = function (paramterValue) {
23-
callback(paramterValue);
15+
window[methodName] = function (parameter) {
16+
callback(parameter);
2417
};
2518

2619
/**
@@ -29,7 +22,7 @@ function RegisterExternalListener(functionName, callback) {
2922
* to make direct calls into React.
3023
*/
3124
if (typeof window.ReactUnityWebGL === 'undefined') window.ReactUnityWebGL = {};
32-
window.ReactUnityWebGL[functionName] = function (paramterValue) {
33-
callback(paramterValue);
25+
window.ReactUnityWebGL[methodName] = function (parameter) {
26+
callback(parameter);
3427
};
3528
}

library/modules/SendMessage.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,7 @@ exports.SendMessage = SendMessage;
77

88
var _Unity = require('../components/Unity');
99

10-
/**
11-
* Sends a message to the Unity content. This works the same
12-
* as Unity's internal 'SendMessage' system. The paramaterValue
13-
* is an optional field.
14-
* @param {string} gameObjectName
15-
* @param {string} methodName
16-
* @param {object} paramterValue
17-
*/
1810
function SendMessage(gameObjectName, methodName, paramterValue) {
19-
if (typeof paramterValue === 'undefined') paramterValue = '';
20-
21-
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 + '\'');
11+
console.warn('SendMessage is deprecated since version 6.4.0, use UnityEvent instead.');
12+
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 + '\'');
2213
}

library/modules/UnityEvent.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.UnityEvent = undefined;
7+
8+
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; }; }();
9+
10+
var _Unity = require('../components/Unity');
11+
12+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
13+
14+
var UnityEvent = exports.UnityEvent = function () {
15+
function UnityEvent(gameObjectName, methodName) {
16+
_classCallCheck(this, UnityEvent);
17+
18+
this.gameObjectName = gameObjectName;
19+
this.methodName = methodName;
20+
}
21+
22+
_createClass(UnityEvent, [{
23+
key: 'emit',
24+
value: function emit(parameter) {
25+
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 + '\'');
26+
}
27+
}, {
28+
key: 'canEmit',
29+
value: function canEmit() {
30+
return typeof _Unity.UnityInstance !== 'undefined';
31+
}
32+
}]);
33+
34+
return UnityEvent;
35+
}();

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "react-unity-webgl",
3-
"version": "6.3.1",
3+
"version": "6.4.0",
44
"description": "A Unity WebGL component for your React application",
55
"main": "library/index.js",
6-
"types": "source/types.d.ts",
6+
"types": "./types.d.ts",
77
"scripts": {
88
"compile": "babel --presets react source --out-dir library"
99
},

source/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Unity from './components/Unity'
22
import { RegisterExternalListener } from './modules/RegisterExternalListener'
33
import { SendMessage } from './modules/SendMessage'
4+
import { UnityEvent } from './modules/UnityEvent'
45

56
export default Unity
6-
export { RegisterExternalListener, SendMessage }
7+
export { RegisterExternalListener, SendMessage, UnityEvent }
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
/**
2-
* Registers a listener to this window. When a message is sent
3-
* from Unity using 'CallExternal', the listener will forward it
4-
* into your React Application.
5-
* @param {string} functionName
6-
* @param {function} callback
7-
*/
8-
export function RegisterExternalListener (functionName, callback) {
1+
export function RegisterExternalListener (methodName, callback) {
92
/**
103
* LEGACY
114
* bind the function to the window to allow
125
* the user to use the legacy functions
136
* Application.ExternalCall and
147
* Application.ExternalEval.
158
*/
16-
window[functionName] = paramterValue => {
17-
callback (paramterValue)
9+
window[methodName] = parameter => {
10+
callback (parameter)
1811
}
1912

2013
/**
@@ -24,7 +17,7 @@ export function RegisterExternalListener (functionName, callback) {
2417
*/
2518
if (typeof window.ReactUnityWebGL === 'undefined')
2619
window.ReactUnityWebGL = {}
27-
window.ReactUnityWebGL[functionName] = paramterValue => {
28-
callback (paramterValue)
20+
window.ReactUnityWebGL[methodName] = parameter => {
21+
callback (parameter)
2922
}
3023
}

0 commit comments

Comments
 (0)