From b3715b9e573bb02589338180d247a0ddbcc527ad Mon Sep 17 00:00:00 2001 From: Jeffrey Lanters Date: Thu, 25 Jan 2018 19:54:12 +0100 Subject: [PATCH] Temp fix for 'Component doesn't unmount properly #22' --- library/components/Unity.js | 9 ++--- library/services/UnityLoaderService.js | 8 +++-- package.json | 4 +-- source/components/Unity.js | 50 +++++++++++++------------- source/components/Unity.styles.js | 6 ---- source/services/UnityLoaderService.js | 24 +++++++------ 6 files changed, 48 insertions(+), 53 deletions(-) delete mode 100644 source/components/Unity.styles.js diff --git a/library/components/Unity.js b/library/components/Unity.js index 8b8fa83e..f5ef9c40 100644 --- a/library/components/Unity.js +++ b/library/components/Unity.js @@ -14,10 +14,6 @@ var _UnityLoaderService = require('../services/UnityLoaderService'); var _UnityLoaderService2 = _interopRequireDefault(_UnityLoaderService); -var _Unity = require('./Unity.styles'); - -var _Unity2 = _interopRequireDefault(_Unity); - function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } @@ -65,11 +61,12 @@ var Unity = function (_Component) { this.setState({ error: error }); } else { this._unityLoaderService.append(this.props.loader).then(function () { - var unityInstance = UnityLoader.instantiate('unity', _this2.props.src, { + var unityInstance = UnityLoader.instantiate('__ruw', _this2.props.src, { onProgress: _this2._onProgress.bind(_this2), Module: _this2.props.module }); module.exports.UnityInstance = unityInstance; + window._ui = unityInstance; }); } } @@ -99,7 +96,7 @@ var Unity = function (_Component) { null, 'React-Unity-Webgl error ', this.state.error - ) : _react2.default.createElement('div', { style: _Unity2.default.unity, id: 'unity' }) + ) : _react2.default.createElement('div', { style: { width: '100%', height: '100%' }, id: '__ruw' }) ); } }]); diff --git a/library/services/UnityLoaderService.js b/library/services/UnityLoaderService.js index d2a4e9ee..a95f5346 100644 --- a/library/services/UnityLoaderService.js +++ b/library/services/UnityLoaderService.js @@ -35,9 +35,11 @@ var UnityLoaderService = function () { }, { key: 'unmount', value: function unmount() { - if (this.unityLoaderScript !== null) { - this.documentHead.removeChild(this.unityLoaderScript); - } + /// unmounting is disabled due to bug + /// https://github.com/jeffreylanters/react-unity-webgl/issues/22 + // if (this.unityLoaderScript !== null) { + // this.documentHead.removeChild(this.unityLoaderScript) + // } } }]); diff --git a/package.json b/package.json index b14ed4b7..9772b269 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-unity-webgl", - "version": "6.2.3", + "version": "6.2.4", "description": "A Unity WebGL component for your React application", "main": "library/index.js", "types": "source/types.d.ts", @@ -34,4 +34,4 @@ "babel-preset-env": "1.6.1", "babel-preset-react": "6.24.1" } -} +} \ No newline at end of file diff --git a/source/components/Unity.js b/source/components/Unity.js index cc599892..b7a19b49 100644 --- a/source/components/Unity.js +++ b/source/components/Unity.js @@ -1,22 +1,21 @@ import React, { Component } from 'react' import UnityLoaderService from '../services/UnityLoaderService' -import Styles from './Unity.styles' export default class Unity extends Component { - constructor (props) { - super (props) + constructor(props) { + super(props) this.state = { error: null } - this._unityLoaderService = new UnityLoaderService () + this._unityLoaderService = new UnityLoaderService() } - componentDidMount () { - this._instantiate () + componentDidMount() { + this._instantiate() } - componentWillUnmount () { - this._unityLoaderService.unmount () + componentWillUnmount() { + this._unityLoaderService.unmount() } - _instantiate () { + _instantiate() { let error = null if (typeof this.props.loader === 'undefined') error = 'Please provide Unity with a path to the UnityLoader in the loader prop.' @@ -24,38 +23,39 @@ export default class Unity extends Component { error = 'Please provide Unity with a path to a valid JSON in the src prop.' if (error !== null) { - console.error (error) - this.setState ({ error: error }) - } + console.error(error) + this.setState({ error: error }) + } else { - this._unityLoaderService.append (this.props.loader).then (() => { - let unityInstance = UnityLoader.instantiate ('unity', this.props.src, { - onProgress: this._onProgress.bind (this), - Module : this.props.module + this._unityLoaderService.append(this.props.loader).then(() => { + let unityInstance = UnityLoader.instantiate('__ruw', this.props.src, { + onProgress: this._onProgress.bind(this), + Module: this.props.module }) module.exports.UnityInstance = unityInstance + window._ui = unityInstance; }) } } - _onProgress (unityInstance, progression) { + _onProgress(unityInstance, progression) { if (typeof this.props.onProgress !== 'undefined') { - this.props.onProgress (progression) + this.props.onProgress(progression) } } - _getContainerStyles () { + _getContainerStyles() { return { width: this.props.width || '100%', height: this.props.height || '100%' } } - render () { + render() { return ( -
- {this.state.error !== null ? ( +
+ {this.state.error !== null ? React-Unity-Webgl error {this.state.error} - ):( -
- )} + : +
+ }
) } diff --git a/source/components/Unity.styles.js b/source/components/Unity.styles.js deleted file mode 100644 index 82ea4cf9..00000000 --- a/source/components/Unity.styles.js +++ /dev/null @@ -1,6 +0,0 @@ -export default { - unity: { - width: '100%', - height: '100%' - } -} \ No newline at end of file diff --git a/source/services/UnityLoaderService.js b/source/services/UnityLoaderService.js index 189ae500..e409992f 100644 --- a/source/services/UnityLoaderService.js +++ b/source/services/UnityLoaderService.js @@ -1,23 +1,25 @@ export default class UnityLoaderService { - constructor () { - this.documentHead = document.getElementsByTagName ('head')[0] + constructor() { + this.documentHead = document.getElementsByTagName('head')[0] this.unityLoaderScript = null } - append (src) { - return new Promise ((resolve, reject) => { - this.unityLoaderScript = document.createElement ('script') + append(src) { + return new Promise((resolve, reject) => { + this.unityLoaderScript = document.createElement('script') this.unityLoaderScript.type = 'text/javascript' this.unityLoaderScript.async = true this.unityLoaderScript.src = src this.unityLoaderScript.onload = () => { - resolve () + resolve() } - this.documentHead.appendChild (this.unityLoaderScript) + this.documentHead.appendChild(this.unityLoaderScript) }) } - unmount () { - if (this.unityLoaderScript !== null) { - this.documentHead.removeChild (this.unityLoaderScript) - } + unmount() { + /// unmounting is disabled due to bug + /// https://github.com/jeffreylanters/react-unity-webgl/issues/22 + // if (this.unityLoaderScript !== null) { + // this.documentHead.removeChild(this.unityLoaderScript) + // } } } \ No newline at end of file