Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 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)]() [![npm](https://img.shields.io/badge/typescript-supported-2a507e.svg)]()
# 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/build-passing-brightgreen.svg)]() [![npm](https://img.shields.io/npm/dt/react-unity-webgl.svg)]() [![npm](https://img.shields.io/badge/typescript-supported-2a507e.svg)]()

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.

Expand Down
2 changes: 1 addition & 1 deletion library/modules/UnityEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var UnityEvent = exports.UnityEvent = function () {
_createClass(UnityEvent, [{
key: 'emit',
value: function emit(parameter) {
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 + '\'');
if (this.canEmit() === true) _Unity.UnityInstance.SendMessage(this.gameObjectName, this.methodName, typeof parameter !== 'undefined' ? parameter : '');else console.warn('Wait for Unity to be instantiated before sending an event \'' + this.methodName + '\'');
}
}, {
key: 'canEmit',
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-unity-webgl",
"version": "6.4.1",
"version": "6.4.2",
"description": "A Unity WebGL component for your React application",
"main": "library/index.js",
"types": "./types.d.ts",
Expand Down Expand Up @@ -34,4 +34,4 @@
"babel-preset-env": "1.6.1",
"babel-preset-react": "6.24.1"
}
}
}
46 changes: 23 additions & 23 deletions source/components/Unity.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,59 @@ 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.'
if (typeof this.props.src === 'undefined')
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('unity', this.props.src, {
onProgress: this._onProgress.bind(this),
Module: this.props.module
})
module.exports.UnityInstance = 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 (
<div className='unity' style={this._getContainerStyles ()}>
<div className='unity' style={this._getContainerStyles()}>
{this.state.error !== null ? (
<b>React-Unity-Webgl error {this.state.error}</b>
):(
<div style={Styles.unity} id='unity'></div>
)}
) : (
<div style={Styles.unity} id='unity'></div>
)}
</div>
)
}
Expand Down
6 changes: 3 additions & 3 deletions source/modules/RegisterExternalListener.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function RegisterExternalListener (methodName, callback) {
export function RegisterExternalListener(methodName, callback) {
/**
* LEGACY
* bind the function to the window to allow
Expand All @@ -7,7 +7,7 @@ export function RegisterExternalListener (methodName, callback) {
* Application.ExternalEval.
*/
window[methodName] = parameter => {
callback (parameter)
callback(parameter)
}

/**
Expand All @@ -18,6 +18,6 @@ export function RegisterExternalListener (methodName, callback) {
if (typeof window.ReactUnityWebGL === 'undefined')
window.ReactUnityWebGL = {}
window.ReactUnityWebGL[methodName] = parameter => {
callback (parameter)
callback(parameter)
}
}
12 changes: 6 additions & 6 deletions source/modules/SendMessage.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { UnityInstance } from '../components/Unity'

export function SendMessage (gameObjectName, methodName, paramterValue) {
console.warn (`SendMessage is deprecated since version 6.4.0, use UnityEvent instead.`)
export function SendMessage(gameObjectName, methodName, paramterValue) {
console.warn(`SendMessage is deprecated since version 6.4.0, use UnityEvent instead.`)
if (typeof UnityInstance !== 'undefined')
UnityInstance.SendMessage (
gameObjectName,
methodName,
UnityInstance.SendMessage(
gameObjectName,
methodName,
paramterValue || '')
else
console.warn (`Wait for Unity to be instantiated before sending a message to '${gameObjectName}'`)
console.warn(`Wait for Unity to be instantiated before sending a message to '${gameObjectName}'`)
}
18 changes: 9 additions & 9 deletions source/modules/UnityEvent.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { UnityInstance } from '../components/Unity'

export class UnityEvent {
constructor (gameObjectName, methodName) {
constructor(gameObjectName, methodName) {
this.gameObjectName = gameObjectName
this.methodName = methodName
}
emit (parameter) {
if (this.canEmit () === true)
UnityInstance.SendMessage (
this.gameObjectName,
this.methodName,
parameter || '')
emit(parameter) {
if (this.canEmit() === true)
UnityInstance.SendMessage(
this.gameObjectName,
this.methodName,
typeof parameter !== 'undefined' ? parameter : '')
else
console.warn (`Wait for Unity to be instantiated before sending an event '${this.methodName}'`)
console.warn(`Wait for Unity to be instantiated before sending an event '${this.methodName}'`)
}
canEmit () {
canEmit() {
return typeof UnityInstance !== 'undefined'
}
}
18 changes: 9 additions & 9 deletions source/services/UnityLoaderService.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
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 () {
unmount() {
if (this.unityLoaderScript !== null) {
this.documentHead.removeChild (this.unityLoaderScript)
this.documentHead.removeChild(this.unityLoaderScript)
}
}
}