Skip to content

Commit 6bb38b9

Browse files
author
Jeffrey Lanters
authored
Merge pull request #14 from jeffreylanters/development
Development
2 parents f2f0440 + 6c6c6a7 commit 6bb38b9

File tree

17 files changed

+116
-51
lines changed

17 files changed

+116
-51
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": ["env"]
2+
"presets": ["react", "env"]
33
}

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ When building content for the web, you might need to communicate with other elem
1919
- [Calling JavaScript functions within React from Unity scripts](#calling-javascript-functions-within-react-from-unity-scripts)
2020
- [Notes](#notes)
2121
- [5.x to 6.x Upgrade note](#5x-to-6x-upgrade-note)
22+
- [Best practices for adding the src and loader files on a public path](#best-practices-for-adding-the-src-and-loader-files-on-a-public-path)
2223
- [Contributing](#contributing)
2324

2425

@@ -37,7 +38,7 @@ $ npm install react-unity-webgl
3738

3839

3940
# Usage
40-
To get started import the default Unity class from react-unity-webgl and include it in your render while giving the public path to your src and loader files.
41+
To get started import the default Unity class from react-unity-webgl and include it in your render while giving the public path to your src and loader files. [Best practices for adding the src and loader files on a public path](#best-practices-for-adding-the-src-and-loader-files-on-a-public-path).
4142

4243
```js
4344
import React from 'react'
@@ -150,6 +151,16 @@ public class MenuController: MonoBehaviour {
150151
}
151152
}
152153
```
154+
Or using the **legacy** way in Unity, for example:
155+
```cs
156+
using UnityEngine;
157+
158+
public class MenuController: MonoBehaviour {
159+
public void OpenReactMenuById (string menuId) {
160+
Application.ExternalCall ("OpenMenu", menuId);
161+
}
162+
}
163+
```
153164
Simple numeric types can be passed to JavaScript in function parameters without requiring any conversion. Other data types will be passed as a pointer in the emscripten heap (which is really just a big array in JavaScript). For strings, you can use the Pointer_stringify helper function to convert to a JavaScript string. To return a string value you need to call _malloc_ to allocate some memory and the writeStringToMemory helper function to write a JavaScript string to it. If the string is a return value, then the il2cpp runtime will take care of freeing the memory for you. For arrays of primitive types, emscripten provides different ArrayBufferViews into it’s heap for different sizes of integer, unsigned integer or floating point representations of memory: HEAP8, HEAPU8, HEAP16, HEAPU16, HEAP32, HEAPU32, HEAPF32, HEAPF64. To access a texture in WebGL, emscripten provides the GL.textures array which maps native texture IDs from Unity to WebGL texture objects. WebGL functions can be called on emscripten’s WebGL context, GLctx.
154165

155166
Legacy ways of calling JavaScript code from Unity. You can use the Application.ExternalCall () and Application.ExternalEval () functions to invoke JavaScript code on the embedding web page. Note that expressions are evaluated in the local scope of the build. If you would like to execute JavaScript code in the global scope, see the Code Visibility section below.
@@ -159,7 +170,8 @@ Legacy ways of calling JavaScript code from Unity. You can use the Application.E
159170

160171

161172
# Notes
162-
Make sure your Unity build is in your public folder, this is due to the component **and** Unity itself will load files in Runtime and not Compile/Bundle time.
173+
## Best practices for adding the src and loader files on a public path
174+
Make sure your Unity build is in your public folder, this is due to the component **and** Unity itself will load files in Runtime and not Compile/Bundle time. The public folder means that the folder should be accesible via a public web adress. The path within your `src` and `loader` should be relative to the html file your app is running in.
163175
## 5.x to 6.x Upgrade note
164176
When upgrading from 5.x to 6.x, make sure you add the `loader` prop to the Unity component and remove the script tag from your HTML page refering to the UnityLoader.js file. See [Usage](#usage) for further details.
165177

@@ -168,4 +180,4 @@ When upgrading from 5.x to 6.x, make sure you add the `loader` prop to the Unity
168180

169181

170182
# Contributing
171-
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!
183+
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/components/Styles.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
/**
7+
* Static styles used by the Unity component
8+
*/
9+
exports.default = {
10+
unity: {
11+
width: '100%',
12+
height: '100%'
13+
}
14+
};

lib/Unity.js renamed to library/components/Unity.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ var _react = require('react');
1010

1111
var _react2 = _interopRequireDefault(_react);
1212

13-
var _UnityLoaderService = require('./UnityLoaderService');
13+
var _UnityLoaderService = require('../services/UnityLoaderService');
1414

1515
var _UnityLoaderService2 = _interopRequireDefault(_UnityLoaderService);
1616

17-
var _Styles = require('./Styles');
17+
var _Unity = require('./Unity.styles');
1818

19-
var _Styles2 = _interopRequireDefault(_Styles);
19+
var _Unity2 = _interopRequireDefault(_Unity);
2020

2121
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2222

@@ -37,53 +37,52 @@ var Unity = function (_Component) {
3737
_this.state = {
3838
error: null
3939
};
40-
_this.unityLoaderService = new _UnityLoaderService2.default();
40+
_this._unityLoaderService = new _UnityLoaderService2.default();
4141
return _this;
4242
}
4343

4444
_createClass(Unity, [{
4545
key: 'componentDidMount',
4646
value: function componentDidMount() {
47-
this.instantiate();
47+
this._instantiate();
4848
}
4949
}, {
5050
key: 'componentWillUnmount',
5151
value: function componentWillUnmount() {
52-
this.unityLoaderService.unmount();
52+
this._unityLoaderService.unmount();
5353
}
5454
}, {
55-
key: 'instantiate',
56-
value: function instantiate() {
55+
key: '_instantiate',
56+
value: function _instantiate() {
5757
var _this2 = this;
5858

5959
var error = null;
60-
6160
if (typeof this.props.loader === 'undefined') error = 'Please provide Unity with a path to the UnityLoader in the loader prop.';
6261
if (typeof this.props.src === 'undefined') error = 'Please provide Unity with a path to a valid JSON in the src prop.';
6362

6463
if (error !== null) {
6564
console.error(error);
6665
this.setState({ error: error });
6766
} else {
68-
this.unityLoaderService.append(this.props.loader).then(function () {
67+
this._unityLoaderService.append(this.props.loader).then(function () {
6968
var unityInstance = UnityLoader.instantiate('unity', _this2.props.src, {
70-
onProgress: _this2.onProgress.bind(_this2),
69+
onProgress: _this2._onProgress.bind(_this2),
7170
Module: _this2.props.module
7271
});
7372
module.exports.UnityInstance = unityInstance;
7473
});
7574
}
7675
}
7776
}, {
78-
key: 'onProgress',
79-
value: function onProgress(unityInstance, progression) {
77+
key: '_onProgress',
78+
value: function _onProgress(unityInstance, progression) {
8079
if (typeof this.props.onProgress !== 'undefined') {
8180
this.props.onProgress(progression);
8281
}
8382
}
8483
}, {
85-
key: 'getContainerStyles',
86-
value: function getContainerStyles() {
84+
key: '_getContainerStyles',
85+
value: function _getContainerStyles() {
8786
return {
8887
width: this.props.width || '100%',
8988
height: this.props.height || '100%'
@@ -94,13 +93,13 @@ var Unity = function (_Component) {
9493
value: function render() {
9594
return _react2.default.createElement(
9695
'div',
97-
{ className: 'unity', style: this.getContainerStyles() },
96+
{ className: 'unity', style: this._getContainerStyles() },
9897
this.state.error !== null ? _react2.default.createElement(
9998
'b',
10099
null,
101100
'React-Unity-Webgl error ',
102101
this.state.error
103-
) : _react2.default.createElement('div', { style: _Styles2.default.unity, id: 'unity' })
102+
) : _react2.default.createElement('div', { style: _Unity2.default.unity, id: 'unity' })
104103
);
105104
}
106105
}]);
File renamed without changes.

lib/index.js renamed to library/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ Object.defineProperty(exports, "__esModule", {
55
});
66
exports.SendMessage = exports.RegisterExternalListener = undefined;
77

8-
var _Unity = require('./Unity');
8+
var _Unity = require('./components/Unity');
99

1010
var _Unity2 = _interopRequireDefault(_Unity);
1111

12-
var _RegisterExternalListener = require('./RegisterExternalListener');
12+
var _RegisterExternalListener = require('./modules/RegisterExternalListener');
1313

14-
var _SendMessage = require('./SendMessage');
14+
var _SendMessage = require('./modules/SendMessage');
1515

1616
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1717

File renamed without changes.

lib/SendMessage.js renamed to library/modules/SendMessage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
55
});
66
exports.SendMessage = SendMessage;
77

8-
var _Unity = require('./Unity');
8+
var _Unity = require('../components/Unity');
99

1010
/**
1111
* Sends a message to the Unity content. This works the same

lib/UnityLoaderService.js renamed to library/services/UnityLoaderService.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ var _createClass = function () { function defineProperties(target, props) { for
88

99
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
1010

11-
var UnityLoaderServer = function () {
12-
function UnityLoaderServer() {
13-
_classCallCheck(this, UnityLoaderServer);
11+
var UnityLoaderService = function () {
12+
function UnityLoaderService() {
13+
_classCallCheck(this, UnityLoaderService);
1414

1515
this.documentHead = document.getElementsByTagName('head')[0];
1616
this.unityLoaderScript = null;
1717
}
1818

19-
_createClass(UnityLoaderServer, [{
19+
_createClass(UnityLoaderService, [{
2020
key: 'append',
2121
value: function append(src) {
2222
var _this = this;
@@ -41,7 +41,7 @@ var UnityLoaderServer = function () {
4141
}
4242
}]);
4343

44-
return UnityLoaderServer;
44+
return UnityLoaderService;
4545
}();
4646

47-
exports.default = UnityLoaderServer;
47+
exports.default = UnityLoaderService;

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "react-unity-webgl",
3-
"version": "6.2.2",
3+
"version": "6.2.3",
44
"description": "A Unity WebGL component for your React application",
5-
"main": "lib/index.js",
5+
"main": "library/index.js",
6+
"types": "source/types.d.ts",
67
"scripts": {
7-
"compile": "babel --presets react source --out-dir lib"
8+
"compile": "babel --presets react source --out-dir library"
89
},
910
"repository": {
1011
"type": "git",

0 commit comments

Comments
 (0)