You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -155,24 +157,24 @@ public class EnemyController : MonoBehaviour {
155
157
156
158
## Communication from Unity to React
157
159
158
-
> Available since version 6.0.0
160
+
> Available since version 6.0.0, refactored in 8.6.0
159
161
160
162
Sending messages from Unity to React is done using Event Listeners via the Unity Context instance. Invoking these Event Listeners from your Unity Project is quite different.
161
163
162
164
On the React side of your project an Event Listeners can be registered to the Unity Context instance. Register the Event Listener using the "on" method as following, where "eventName" is the name of your listener, and the "eventListener" method is the Method which will be Invoked which may or may not pass along any Arguments based on your implementation.
163
165
164
-
> Keep in mind communication from Unity to React is global, so Event Listeners with the same name will overwrite one another.
166
+
> Keep in mind communication from Unity to React is global, so Event Listeners with the same name will will be invoked on all Unity Instances.
165
167
166
168
> 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 Pointerstringify helper function to convert to a JavaScript string. You can read more about parameters and [JavaScript to Unityscript types](#javascript-to-unityscript-types) here.
167
169
168
170
```ts
169
171
function on(eventName:string, eventListener:Function):void;
170
172
```
171
173
172
-
In order to emit Event Listeners, a JSLib file has to be created within your Unity Project "Plugins/WebGL" directory. The React Unity WebGL module exposes a global Object which allows for the emitting of the Event Listeners. When writing your JSLib file, simply invoke the eventName as a member of the "ReactUnityWebGL" object within any method.
174
+
In order to dispatch Event Listeners, a JSLib file has to be created within your Unity Project "Plugins/WebGL" directory. The React Unity WebGL module exposes a global method which allows for the dispatchment of the Event Listeners. When writing your JSLib file, simply invoke the eventName using the global methpd "dispatchReactUnityEvent" with an optional parameter.
173
175
174
-
```js
175
-
ReactUnityWebGL[eventName: string];
176
+
```ts
177
+
function dispatchReactUnityEvent(eventName:string, ...parameters:any);
176
178
```
177
179
178
180
#### Example implementation
@@ -216,14 +218,14 @@ function App() {
216
218
217
219
To emit the Event Listener we've just created, we'll have to create a new JSLib file within our Unity Project first. This JSLib file will be places within the "Assets/Plugins/WebGL" directory. The JSLib itself has nothing to do with this module, it is natively supported by Unity and is used for all communication between your CSharp and JavaScript in any given context.
218
220
219
-
We'll start of by creating a new method inside of our JSLib. The name of this method can be anything, but in this example we'll give it it the same name as our Event Name to keep things clean. In the body of the method, we'll emit our Event Listener by invoking a method on the "ReactUnityWebGL" object exposed by the module. All of your Event Listeners are available as a property using the Event Name on the object. We'll pass along the userName and the score. The userName has to go through the built-in "Pointer_stringify" method in order to get the value, otherwise a int pointer will be passed instead. You can read more about parameters and [JavaScript to Unityscript types](#javascript-to-unityscript-types) here.
221
+
We'll start of by creating a new method inside of our JSLib. The name of this method can be anything, but in this example we'll give it it the same name as our Event Name to keep things clean. In the body of the method, we'll emit our Event Listener by invoking the global method "dispatchReactUnityEvent" exposed by this module. All of your Event Listeners are available using the Event Name as the first parameter. We'll pass along the userName and the score. The userName has to go through the built-in "Pointer_stringify" method in order to get the value, otherwise a int pointer will be passed instead. You can read more about parameters and [JavaScript to Unityscript types](#javascript-to-unityscript-types) here.
The Unity context object allows you to enable and disable the fullscreen mode of your application. Cursor locking (using Cursor.lockState) and full-screen mode are both supported in WebGL, implemented using the respective HTML5 APIs (Element.requestPointerLock and Element.requestFullscreen). These are supported in Firefox and Chrome. Safari cannot currently use full-screen and cursor locking. An implementation could look something like:
352
354
353
-
```js
355
+
```ts
354
356
function setFullscreen(enabled:boolean):void;
355
357
```
356
358
@@ -493,6 +495,47 @@ function App() {
493
495
}
494
496
```
495
497
498
+
## Requesting Canvas Pointer Locking
499
+
500
+
> Available since version 8.6.0
501
+
502
+
Asynchronously ask for the pointer to be locked on current canvas. To track the success or failure of the request, it is necessary to listen for the pointerlockchange and pointerlockerror events at the Document level.
503
+
504
+
```tsx
505
+
function requestPointerLock():void;
506
+
```
507
+
508
+
#### Example implementation
509
+
510
+
A basic implementation could look something like this. In the following example we'll request a pointer lock on the click of a button.
By default, Unity WebGL builds capture the keyboard as soon as it's loaded. This means that all keyboard input on your React Application is captured by the Unity Application instead. Doing so will result in a focus and blur on all keyboard events when clicking on, or around the Unity Application. Implementing the tabIndex of the element mitigates this issue and allows for other elements to be selected.
@@ -967,6 +1010,56 @@ function App() {
967
1010
}
968
1011
```
969
1012
1013
+
## Taking Screenshots of the Canvas
1014
+
1015
+
> Available since version 8.6.0
1016
+
1017
+
Takes a screenshot of the canvas and returns a data URL containing image data. The image data is in .png format unless otherwise specified. Enabling preserve drawing buffer within the WebGL context attributes is required in order to take a screenshot.
A basic implementation could look something like this. In the following example a button is added to the Render. When it's being clicked, a high quality JPEG screenshot will be taken and opened within a new tab.
1029
+
1030
+
```jsx
1031
+
// File: App.jsx
1032
+
1033
+
import React from "react";
1034
+
import Unity, { UnityContext } from "react-unity-webgl";
1035
+
1036
+
const unityContext = new UnityContext({
1037
+
loaderUrl: "build/myunityapp.loader.js",
1038
+
dataUrl: "build/myunityapp.data",
1039
+
frameworkUrl: "build/myunityapp.framework.js",
1040
+
codeUrl: "build/myunityapp.wasm",
1041
+
webglContextAttributes: {
1042
+
preserveDrawingBuffer: true,
1043
+
},
1044
+
});
1045
+
1046
+
function App() {
1047
+
function handleOnClickTakeScreenshot() {
1048
+
const data = unityContext.takeScreenshot("image/jpeg", 1.0);
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 Pointerstringify helper function to convert to a JavaScript string.
@@ -984,19 +1077,19 @@ A basic implementation could look something like this. In this example a series
0 commit comments