Skip to content

Commit 3e2b075

Browse files
author
Jeffrey Lanters
authored
Merge pull request #8 from jeffreylanters/development
Version 6.1.0
2 parents f1d2a49 + 4424ed4 commit 3e2b075

File tree

4 files changed

+46
-52
lines changed

4 files changed

+46
-52
lines changed

README.md

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,22 @@ export class App extends React.Component {
3535
```
3636
## Optional attributes
3737

38+
### Overruling the module
3839
```js
39-
// Overruling the module
40+
this.myCustomModule = { ... }
4041
<Unity ... module={ this.myCustomModule } />
4142
```
4243

44+
### Tracking progression
45+
```js
46+
<Unity ... onProgress={ this.onProgress } />
47+
onProgress (progression) {
48+
console.log (`Loading ${(progression * 100)} % ...`)
49+
if (progression === 1)
50+
console.log (`Loading done!`)
51+
}
52+
```
53+
4354

4455

4556

@@ -120,25 +131,10 @@ Legacy ways of calling JavaScript code from Unity. You can use the Application.E
120131

121132

122133

123-
# Styling
124-
The following hierarchy will be applied to the React Unity WebGL component. Feel free to apply any styles to the component.
125-
126-
```scss
127-
.unity {
128-
.unity-container {
129-
canvas {
130-
/* don't forget to set my width and height! */
131-
}
132-
}
133-
.unity-loader {
134-
.loading-bar {
135-
.loading-fill {
136-
/* the width will be set by the component */
137-
}
138-
}
139-
}
140-
}
141-
```
134+
# 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.
136+
## 5.x to 6.x Upgrade note
137+
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.
142138

143139

144140

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

source/Unity.js

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ export default class Unity extends Component {
55
constructor (props) {
66
super (props)
77
this.state = {
8-
progress: 0,
9-
loaded: false,
108
error: null
119
}
1210
this.unityLoaderService = new UnityLoaderService ()
1311
}
1412
componentDidMount () {
1513
this.instantiate ()
1614
}
15+
componentWillUnmount () {
16+
this.unityLoaderService.unmount ()
17+
}
1718
instantiate () {
1819
let error = null
1920

@@ -28,36 +29,27 @@ export default class Unity extends Component {
2829
}
2930
else {
3031
this.unityLoaderService.append (this.props.loader).then (() => {
31-
module.exports.UnityInstance = UnityLoader.instantiate ('unity-container', this.props.src, {
32-
onProgress: ((gameInstance, progress) => {
33-
this.setState({
34-
loaded: progress === 1,
35-
progress: progress
36-
})
37-
}),
32+
let unityInstance = UnityLoader.instantiate ('unity', this.props.src, {
33+
onProgress: this.onProgress.bind (this),
3834
Module : this.props.module
3935
})
36+
module.exports.UnityInstance = unityInstance
4037
})
4138
}
4239
}
40+
onProgress (unityInstance, progression) {
41+
if (typeof this.props.onProgress !== 'undefined') {
42+
this.props.onProgress (progression)
43+
}
44+
}
4345
render () {
44-
if (this.state.error !== null) { return (
45-
<div className='unity'>
46-
<b>React-Unity-Webgl error</b> {this.state.error}
47-
</div>
48-
)}
4946
return (
5047
<div className='unity'>
51-
<div>
52-
<div className='unity-container' id='unity-container'></div>
53-
</div>
54-
{this.state.loaded === false &&
55-
<div className='unity-loader'>
56-
<div className='bar'>
57-
<div className='fill' style={{ width:`${(this.state.progress * 100)}%`}}></div>
58-
</div>
59-
</div>
60-
}
48+
{this.state.error !== null ? (
49+
<b>React-Unity-Webgl error {this.state.error}</b>
50+
):(
51+
<div id='unity'></div>
52+
)}
6153
</div>
6254
)
6355
}

source/UnityLoaderService.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
export default class UnityLoaderServer {
22
constructor () {
3-
3+
this.documentHead = document.getElementsByTagName ('head')[0]
4+
this.unityLoaderScript = null
45
}
56
append (src) {
67
return new Promise ((resolve, reject) => {
7-
let unityLoaderScript = document.createElement ('script')
8-
unityLoaderScript.type = 'text/javascript'
9-
unityLoaderScript.async = true
10-
unityLoaderScript.src = src
11-
unityLoaderScript.onload = () => {
8+
this.unityLoaderScript = document.createElement ('script')
9+
this.unityLoaderScript.type = 'text/javascript'
10+
this.unityLoaderScript.async = true
11+
this.unityLoaderScript.src = src
12+
this.unityLoaderScript.onload = () => {
1213
resolve ()
1314
}
14-
document.getElementsByTagName ('head')[0].appendChild (unityLoaderScript)
15+
this.documentHead.appendChild (this.unityLoaderScript)
1516
})
1617
}
18+
unmount () {
19+
if (this.unityLoaderScript !== null) {
20+
this.documentHead.removeChild (this.unityLoaderScript)
21+
}
22+
}
1723
}

0 commit comments

Comments
 (0)