Skip to content

Commit ba5202b

Browse files
author
Jeffrey Lanters
authored
Merge pull request #10 from jeffreylanters/development
6.2.0
2 parents 3e2b075 + 21cd6f9 commit ba5202b

File tree

7 files changed

+114
-54
lines changed

7 files changed

+114
-54
lines changed

README.md

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,41 @@ When building content for the web, you might need to communicate with other elem
33

44
<img src="http://react-etc.net/files/2016-07/logo-578x270.png" height="50px"/> <img src="http://gamepadable.com/wp-content/uploads/2016/01/Official_unity_logo.png" height="50px"/>
55

6+
<img src="https://media.giphy.com/media/3o6fIRrpHHfMXwxMSk/giphy.gif" width="300px">
7+
8+
9+
10+
11+
12+
- [Installation](#installation)
13+
- [Usage](#usage)
14+
- [Optional attributes](#optional-attributes)
15+
- [Width and height](#width-and-height)
16+
- [Tracking progression](#tracking-progression)
17+
- [Modules](#modules)
18+
- [Calling Unity scripts functions from JavaScript in React](#calling-unity-scripts-functions-from-javascript-in-react)
19+
- [Calling JavaScript functions within React from Unity scripts](#calling-javascript-functions-within-react-from-unity-scripts)
20+
- [Notes](#notes)
21+
- [5.x to 6.x Upgrade note](#5x-to-6x-upgrade-note)
22+
- [Contributing](#contributing)
23+
624

725

826

927

1028
# Installation
11-
Install using npm. Make sure you download the release matching with your Unity version. I try to update this plugin in case of need as fast as possible. Check the [releases on GitHub](https://github.com/jeffreylanters/react-unity-webgl/releases) for the corresponding version.
29+
Install using npm. Make sure you download the release matching with your Unity version. I try to update this plugin in case of need as fast as possible. Check the [releases on GitHub](https://github.com/jeffreylanters/react-unity-webgl/releases) for the corresponding version or [view on NPM](https://www.npmjs.com/package/react-unity-webgl).
1230

1331
```sh
14-
$ npm install --save react-unity-webgl
32+
$ npm install react-unity-webgl
1533
```
1634

1735

1836

1937

2038

2139
# Usage
22-
To get started import the default Unity class from react-unity-webgl.
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.
2341

2442
```js
2543
import React from 'react';
@@ -28,20 +46,22 @@ import Unity from 'react-unity-webgl';
2846
export class App extends React.Component {
2947
render () {
3048
return <Unity
31-
src='Build/myGame.json'
32-
loader='Build/UnityLoader.js' />
49+
src='Public/Build/myGame.json'
50+
loader='Public/Build/UnityLoader.js' />
3351
}
3452
}
3553
```
3654
## Optional attributes
3755

38-
### Overruling the module
56+
### Width and height
57+
The default width and height is 100%
3958
```js
40-
this.myCustomModule = { ... }
41-
<Unity ... module={ this.myCustomModule } />
59+
<Unity ... width='500px' height='350px' />
60+
<Unity ... width='50%' height='50%' />
4261
```
4362

4463
### Tracking progression
64+
The loading progression of the Unity player will be a value between 0 and 1
4565
```js
4666
<Unity ... onProgress={ this.onProgress } />
4767
onProgress (progression) {
@@ -51,6 +71,13 @@ onProgress (progression) {
5171
}
5272
```
5373

74+
### Modules
75+
Overrides the module object
76+
```js
77+
this.myCustomModule = { ... }
78+
<Unity ... module={ this.myCustomModule } />
79+
```
80+
5481

5582

5683

@@ -132,7 +159,7 @@ Legacy ways of calling JavaScript code from Unity. You can use the Application.E
132159

133160

134161
# Notes
135-
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 time.
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.
136163
## 5.x to 6.x Upgrade note
137164
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.
138165

lib/Styles.js

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

lib/Unity.js

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ var _UnityLoaderService = require('./UnityLoaderService');
1414

1515
var _UnityLoaderService2 = _interopRequireDefault(_UnityLoaderService);
1616

17+
var _Styles = require('./Styles');
18+
19+
var _Styles2 = _interopRequireDefault(_Styles);
20+
1721
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1822

1923
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
@@ -31,8 +35,6 @@ var Unity = function (_Component) {
3135
var _this = _possibleConstructorReturn(this, (Unity.__proto__ || Object.getPrototypeOf(Unity)).call(this, props));
3236

3337
_this.state = {
34-
progress: 0,
35-
loaded: false,
3638
error: null
3739
};
3840
_this.unityLoaderService = new _UnityLoaderService2.default();
@@ -44,6 +46,11 @@ var Unity = function (_Component) {
4446
value: function componentDidMount() {
4547
this.instantiate();
4648
}
49+
}, {
50+
key: 'componentWillUnmount',
51+
value: function componentWillUnmount() {
52+
this.unityLoaderService.unmount();
53+
}
4754
}, {
4855
key: 'instantiate',
4956
value: function instantiate() {
@@ -59,51 +66,41 @@ var Unity = function (_Component) {
5966
this.setState({ error: error });
6067
} else {
6168
this.unityLoaderService.append(this.props.loader).then(function () {
62-
module.exports.UnityInstance = UnityLoader.instantiate('unity-container', _this2.props.src, {
63-
onProgress: function onProgress(gameInstance, progress) {
64-
_this2.setState({
65-
loaded: progress === 1,
66-
progress: progress
67-
});
68-
},
69+
var unityInstance = UnityLoader.instantiate('unity', _this2.props.src, {
70+
onProgress: _this2.onProgress.bind(_this2),
6971
Module: _this2.props.module
7072
});
73+
module.exports.UnityInstance = unityInstance;
7174
});
7275
}
7376
}
77+
}, {
78+
key: 'onProgress',
79+
value: function onProgress(unityInstance, progression) {
80+
if (typeof this.props.onProgress !== 'undefined') {
81+
this.props.onProgress(progression);
82+
}
83+
}
84+
}, {
85+
key: 'getContainerStyles',
86+
value: function getContainerStyles() {
87+
return {
88+
width: this.props.width || '100%',
89+
height: this.props.height || '100%'
90+
};
91+
}
7492
}, {
7593
key: 'render',
7694
value: function render() {
77-
if (this.state.error !== null) {
78-
return _react2.default.createElement(
79-
'div',
80-
{ className: 'unity' },
81-
_react2.default.createElement(
82-
'b',
83-
null,
84-
'React-Unity-Webgl error'
85-
),
86-
' ',
87-
this.state.error
88-
);
89-
}
9095
return _react2.default.createElement(
9196
'div',
92-
{ className: 'unity' },
93-
_react2.default.createElement(
94-
'div',
97+
{ className: 'unity', style: this.getContainerStyles() },
98+
this.state.error !== null ? _react2.default.createElement(
99+
'b',
95100
null,
96-
_react2.default.createElement('div', { className: 'unity-container', id: 'unity-container' })
97-
),
98-
this.state.loaded === false && _react2.default.createElement(
99-
'div',
100-
{ className: 'unity-loader' },
101-
_react2.default.createElement(
102-
'div',
103-
{ className: 'bar' },
104-
_react2.default.createElement('div', { className: 'fill', style: { width: this.state.progress * 100 + '%' } })
105-
)
106-
)
101+
'React-Unity-Webgl error ',
102+
this.state.error
103+
) : _react2.default.createElement('div', { style: _Styles2.default.unity, id: 'unity' })
107104
);
108105
}
109106
}]);

lib/UnityLoaderService.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,34 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
1111
var UnityLoaderServer = function () {
1212
function UnityLoaderServer() {
1313
_classCallCheck(this, UnityLoaderServer);
14+
15+
this.documentHead = document.getElementsByTagName('head')[0];
16+
this.unityLoaderScript = null;
1417
}
1518

1619
_createClass(UnityLoaderServer, [{
1720
key: 'append',
1821
value: function append(src) {
22+
var _this = this;
23+
1924
return new Promise(function (resolve, reject) {
20-
var unityLoaderScript = document.createElement('script');
21-
unityLoaderScript.type = 'text/javascript';
22-
unityLoaderScript.async = true;
23-
unityLoaderScript.src = src;
24-
unityLoaderScript.onload = function () {
25+
_this.unityLoaderScript = document.createElement('script');
26+
_this.unityLoaderScript.type = 'text/javascript';
27+
_this.unityLoaderScript.async = true;
28+
_this.unityLoaderScript.src = src;
29+
_this.unityLoaderScript.onload = function () {
2530
resolve();
2631
};
27-
document.getElementsByTagName('head')[0].appendChild(unityLoaderScript);
32+
_this.documentHead.appendChild(_this.unityLoaderScript);
2833
});
2934
}
35+
}, {
36+
key: 'unmount',
37+
value: function unmount() {
38+
if (this.unityLoaderScript !== null) {
39+
this.documentHead.removeChild(this.unityLoaderScript);
40+
}
41+
}
3042
}]);
3143

3244
return UnityLoaderServer;

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": "6.1.0",
3+
"version": "6.2.0",
44
"description": "A Unity WebGL component for your React application",
55
"main": "lib/index.js",
66
"scripts": {

source/Styles.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default {
2+
unity: {
3+
width: '100%',
4+
height: '100%'
5+
}
6+
}

source/Unity.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { Component } from 'react'
22
import UnityLoaderService from './UnityLoaderService'
3+
import Styles from './Styles'
34

45
export default class Unity extends Component {
56
constructor (props) {
@@ -42,13 +43,19 @@ export default class Unity extends Component {
4243
this.props.onProgress (progression)
4344
}
4445
}
46+
getContainerStyles () {
47+
return {
48+
width: this.props.width || '100%',
49+
height: this.props.height || '100%'
50+
}
51+
}
4552
render () {
4653
return (
47-
<div className='unity'>
54+
<div className='unity' style={this.getContainerStyles ()}>
4855
{this.state.error !== null ? (
4956
<b>React-Unity-Webgl error {this.state.error}</b>
5057
):(
51-
<div id='unity'></div>
58+
<div style={Styles.unity} id='unity'></div>
5259
)}
5360
</div>
5461
)

0 commit comments

Comments
 (0)