react-unity-webgl
Advanced tools
Comparing version 5.7.1 to 6.0.0
{ | ||
"name": "react-unity-webgl", | ||
"version": "5.7.1", | ||
"version": "6.0.0", | ||
"description": "A Unity WebGL component for your React application", | ||
"main": "lib/react-unity-webgl.js", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
@@ -26,14 +26,13 @@ "compile": "babel --presets react source --out-dir lib", | ||
"dependencies": { | ||
"react": "15.4.2", | ||
"react-dom": "15.4.2" | ||
"react": "16.0.0", | ||
"react-dom": "16.0.0" | ||
}, | ||
"devDependencies": { | ||
"babel": "4.6.0", | ||
"babel-cli": "6.5.1", | ||
"webpack": "2.2.1", | ||
"babel-core": "6.22.1", | ||
"babel-loader": "6.2.10", | ||
"babel-preset-es2015": "6.22.0", | ||
"babel-preset-react": "6.22.0" | ||
"babel": "6.23.0", | ||
"babel-cli": "6.26.0", | ||
"babel-core": "6.26.0", | ||
"babel-loader": "7.1.2", | ||
"babel-preset-env": "1.6.1", | ||
"babel-preset-react": "6.24.1" | ||
} | ||
} |
154
README.md
@@ -1,33 +0,36 @@ | ||
# react-unity-webgl | ||
Easy to use Unity 5.6 or newer WebGL player component for your React application. Embed your Unity application in your react application for writing interactive interfaces with two way Unity and react communication. | ||
# React Unity WebGL | ||
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. | ||
<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"/> | ||
# installation | ||
Install using npm. Make sure you download the version 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. | ||
# Installation | ||
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. | ||
```sh | ||
# example | ||
$ npm install --save react-unity-webgl | ||
``` | ||
# usage | ||
To get stated import the Unity component from 'react-unity-webgl'. Once imported you can use the Unity component to load in your Unity content. Place the Unity tag along with a src to the json file Unity exported. | ||
> ### Notice | ||
> Don't forget to add a script tag to load the `UnityLoader.js` file, exported by Unity in your base html file. | ||
# Usage | ||
To get started import the default Unity class from react-unity-webgl. | ||
```js | ||
import React, { Component } from 'react'; | ||
import { Unity } from 'react-unity-webgl'; | ||
import React from 'react'; | ||
import Unity from 'react-unity-webgl'; | ||
export class App extends Component { | ||
render() { | ||
return (<div className="app"> | ||
<Unity src="Build/myGame.json" /> | ||
</div>) | ||
export class App extends React.Component { | ||
render () { | ||
return <Unity | ||
src='Build/myGame.json' | ||
loader='Build/UnityLoader.js' /> | ||
} | ||
} | ||
``` | ||
## Optional attributes | ||
@@ -37,29 +40,88 @@ | ||
// Overruling the module | ||
<Unity src="Build/myGame.json" module={this.myCustomModule} /> | ||
<Unity ... module={ this.myCustomModule } /> | ||
``` | ||
# communication | ||
Unity allows you to send Javascript messages to the Unity content. In order to do so using React you have to import the Message function from 'react-unity-webgl'. The first parameter is the target game object name, the next is the method name, and the last is a optional parameter value. | ||
# Calling Unity scripts functions from JavaScript in React | ||
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. | ||
```js | ||
import React, { Component } from 'react'; | ||
import { Message } from 'react-unity-webgl' | ||
SendMessage (objectName, methodName, value); | ||
``` | ||
export class Menu extends Component { | ||
onClick () { | ||
Message ("myGameObjectName", "myMethodName", "paramterValue"); | ||
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: | ||
```js | ||
import React from 'react'; | ||
import { SendMessage } from 'react-unity-webgl'; | ||
export class App extends React.Component { | ||
spawnEnemy (count) { | ||
SendMessage ('SpawnBehaviour', 'SpawnEnemies', count); | ||
} | ||
render() { | ||
return (<div className="menu"> | ||
<div onClick={this.onClick.bind(this)}> | ||
Click me | ||
</div> | ||
</div>) | ||
render () { | ||
return <div onClick={ this.spawnEnemy.bind(this, 5) }> | ||
Click to Spawn 5 Enemies</div> | ||
} | ||
} | ||
``` | ||
While in Unity, for example: | ||
```cs | ||
using UnityEngine; | ||
# styling | ||
The player will be injected in the a component with the className "unity-container". To style to player use the following sass styling. To style the loader you can style the component with the className "unity-loader". See the example below. | ||
public class SpawnController: MonoBehaviour { | ||
public void SpawnEnemies (int count) { | ||
Debug.Log (string.Format ("Spawning {0} enemies", count)); | ||
} | ||
} | ||
``` | ||
# Calling JavaScript functions within React from Unity scripts | ||
We also allow you to call JavaScript functions within React from the Unity Content. To get started import the function RegisterExternalListener from react-unity-webgl. | ||
```js | ||
RegisterExternalListener (methodName, callback); | ||
``` | ||
Where methodName is the name of a method in the script, this method will be binded to the current browser window so Unity can refer to it; callback canwill be a function, which takes one parameter with the value passed by your content. Note that it is recommended to register the callbacks before loading the Unity content. For example: | ||
```js | ||
import React from 'react'; | ||
import { RegisterExternalListener } from 'react-unity-webgl'; | ||
export class App extends React.Component { | ||
constructor () { | ||
RegisterExternalListener ('OpenMenu', this.openMenu.bind (this)); | ||
} | ||
openMenu (menuId) { | ||
console.log (`opening menu with id ${menuId$}`); | ||
} | ||
} | ||
``` | ||
While in Unity, for example: | ||
```cs | ||
using UnityEngine; | ||
public class MenuController: MonoBehaviour { | ||
[DllImport("__Internal")] | ||
private static extern void OpenMenu (string menuId); | ||
public void OpenReactMenuById (string menuId) { | ||
OpenMenu (menuId); | ||
} | ||
} | ||
``` | ||
Simple numeric types can be passed to JavaScript in function parameters without requiring any conversion. Other data types will be passed as a pointer in the emscripten heap (which is really just a big array in JavaScript). For strings, you can use the Pointer_stringify helper function to convert to a JavaScript string. To return a string value you need to call _malloc_ to allocate some memory and the writeStringToMemory helper function to write a JavaScript string to it. If the string is a return value, then the il2cpp runtime will take care of freeing the memory for you. For arrays of primitive types, emscripten provides different ArrayBufferViews into it’s heap for different sizes of integer, unsigned integer or floating point representations of memory: HEAP8, HEAPU8, HEAP16, HEAPU16, HEAP32, HEAPU32, HEAPF32, HEAPF64. To access a texture in WebGL, emscripten provides the GL.textures array which maps native texture IDs from Unity to WebGL texture objects. WebGL functions can be called on emscripten’s WebGL context, GLctx. | ||
Legacy ways of calling JavaScript code from Unity. You can use the Application.ExternalCall () and Application.ExternalEval () functions to invoke JavaScript code on the embedding web page. Note that expressions are evaluated in the local scope of the build. If you would like to execute JavaScript code in the global scope, see the Code Visibility section below. | ||
# Styling | ||
The following hierarchy will be applied to the React Unity WebGL component. Feel free to apply any styles to the component. | ||
```scss | ||
@@ -69,8 +131,8 @@ .unity { | ||
canvas { | ||
/* don't forget to set my width and height! */ | ||
} | ||
} | ||
.unity-loader { | ||
.bar { | ||
.fill { | ||
.loading-bar { | ||
.loading-fill { | ||
/* the width will be set by the component */ | ||
@@ -83,15 +145,7 @@ } | ||
# html example | ||
```html | ||
<!DOCTYPE html> | ||
<html lang="nl"> | ||
<head> | ||
<title>My Unity Game</title> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
</body> | ||
<script src="Build/UnityLoader.js"></script> | ||
<script src="compiled/bundle.js"></script> | ||
</html> | ||
``` | ||
# Contributing | ||
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! |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
20688
6
15
274
149
1
+ Addedprop-types@15.8.1(transitive)
+ Addedreact@16.0.0(transitive)
+ Addedreact-dom@16.0.0(transitive)
+ Addedreact-is@16.13.1(transitive)
- Removedreact@15.4.2(transitive)
- Removedreact-dom@15.4.2(transitive)
Updatedreact@16.0.0
Updatedreact-dom@16.0.0