|
1 |
| -# React Unity WebGL |
| 1 | +# React Unity WebGL · []() []() []() []() []() |
| 2 | + |
2 | 3 | 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.
|
3 | 4 |
|
4 | 5 | <img src="https://raw.githubusercontent.com/jeffreylanters/react-unity-webgl/master/resources/readme/logo.png" width="300px"><br />
|
@@ -85,28 +86,28 @@ this.myCustomModule = { ... }
|
85 | 86 |
|
86 | 87 | <br/><br/><br/>
|
87 | 88 | # Calling Unity scripts functions from JavaScript in React
|
88 |
| -Sometimes you need to send some data or notification to the Unity script from the browser’s JavaScript. The recommended way of doing it is to call methods on GameObjects in your content. To get started import the function SendMessage from react-unity-webgl. |
| 89 | +Sometimes you need to send some data or notification to the Unity script from the browser’s JavaScript. The recommended way of doing it is to call methods on GameObjects in your content. To get started import the class UnityEvent from react-unity-webgl. |
89 | 90 |
|
90 | 91 | ```js
|
91 |
| -SendMessage (objectName: String, methodName: String, value: Object): void; |
| 92 | +UnityEvent (objectName: String, methodName: String); |
92 | 93 | ```
|
93 | 94 |
|
94 |
| -Where objectName is the name of an object in your scene; methodName is the name of a method in the script, currently attached to that object; value can be a string, a number, or can be empty. For example: |
| 95 | +Where objectName is the name of an object in your scene; methodName is the name of a method in the script, currently attached to that object. When you've created a new UnityEvent, you can call the 'emit' function to fire it into Unity. You can pass an optional parameter value. |
95 | 96 | ```js
|
96 | 97 | import React from 'react'
|
97 |
| -import { SendMessage } from 'react-unity-webgl' |
| 98 | +import { UnityEvent } from 'react-unity-webgl' |
98 | 99 |
|
99 | 100 | export class App extends React.Component {
|
100 |
| - spawnEnemy (count) { |
101 |
| - SendMessage ('SpawnBehaviour', 'SpawnEnemies', count) |
| 101 | + constructor () { |
| 102 | + this.spawnEnemies = new UnityEvent ('SpawnBehaviour', 'SpawnEnemies') |
102 | 103 | }
|
103 | 104 | render () {
|
104 |
| - return <div onClick={ this.spawnEnemy.bind(this, 5) }> |
| 105 | + return <div onClick={ this.spawnEnemies.emit (5) }> |
105 | 106 | Click to Spawn 5 Enemies</div>
|
106 | 107 | }
|
107 | 108 | }
|
108 | 109 | ```
|
109 |
| -While in Unity, for example: |
| 110 | +While in Unity the following script is attached the a game object named 'SpawnBehaviour'. |
110 | 111 | ```cs
|
111 | 112 | using UnityEngine;
|
112 | 113 |
|
@@ -187,4 +188,4 @@ Simple numeric types can be passed to JavaScript in function parameters without
|
187 | 188 |
|
188 | 189 | <br/><br/><br/>
|
189 | 190 | # Contributing
|
190 |
| -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! |
| 191 | +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! |
0 commit comments