11import "./app.css" ;
22import { Fragment , useEffect , useState } from "react" ;
3- import Unity , { UnityContext } from "react-unity-webgl" ;
3+ import { Unity , useUnityContext } from "react-unity-webgl" ;
44
55interface Vector2 {
66 x : number ;
77 y : number ;
88}
99
10- // This is the context that Unity will use to communicate with the React app.
11- const unityContext = new UnityContext ( {
12- productName : "React Unity WebGL Tests" ,
13- companyName : "Jeffrey Lanters" ,
14- // The url's of the Unity WebGL runtime, these paths are public and should be
15- // accessible from the internet and relative to the index.html.
16- loaderUrl : "unitybuild/2020.1/myunityapp.loader.js" ,
17- dataUrl : "unitybuild/2020.1/myunityapp.data" ,
18- frameworkUrl : "unitybuild/2020.1/myunityapp.framework.js" ,
19- codeUrl : "unitybuild/2020.1/myunityapp.wasm" ,
20- streamingAssetsUrl : "unitybuild/2020.1/streamingassets" ,
21- // Additional configuration options.
22- webglContextAttributes : {
23- preserveDrawingBuffer : true ,
24- } ,
25- } ) ;
10+
2611
2712// This is the React component that will be rendering the Unity app.
2813function App ( ) {
14+
15+ // This is the context that Unity will use to communicate with the React app.
16+ const unityContext = useUnityContext ( {
17+ productName : "React Unity WebGL Tests" ,
18+ companyName : "Jeffrey Lanters" ,
19+ // The url's of the Unity WebGL runtime, these paths are public and should be
20+ // accessible from the internet and relative to the index.html.
21+ loaderUrl : "unitybuild/2020.1/myunityapp.loader.js" ,
22+ dataUrl : "unitybuild/2020.1/myunityapp.data" ,
23+ frameworkUrl : "unitybuild/2020.1/myunityapp.framework.js" ,
24+ codeUrl : "unitybuild/2020.1/myunityapp.wasm" ,
25+ streamingAssetsUrl : "unitybuild/2020.1/streamingassets" ,
26+ // Additional configuration options.
27+ webglContextAttributes : {
28+ preserveDrawingBuffer : true ,
29+ } ,
30+ } ) ;
31+ const { addEventListener, removeEventListener, sendMessage } = unityContext
32+
2933 // The app's state.
3034 const [ isUnityMounted , setIsUnityMounted ] = useState < boolean > ( true ) ;
3135 const [ rotationSpeed , setRotationSpeed ] = useState < number > ( 30 ) ;
@@ -37,22 +41,27 @@ function App() {
3741
3842 // When the component is mounted, we'll register some event listener.
3943 useEffect ( ( ) => {
40- unityContext . on ( "canvas" , handleOnUnityCanvas ) ;
41- unityContext . on ( "progress" , handleOnUnityProgress ) ;
42- unityContext . on ( "loaded" , handleOnUnityLoaded ) ;
43- unityContext . on ( "RotationDidUpdate" , handleOnUnityRotationDidUpdate ) ;
44- unityContext . on ( "ClickedPosition" , handleOnUnityClickedPosition ) ;
45- unityContext . on ( "Say" , handleOnUnitySayMessage ) ;
46- // When the component is unmounted, we'll unregister the event listener.
47- return function ( ) {
48- unityContext . removeAllEventListeners ( ) ;
44+ addEventListener ( "canvas" , handleOnUnityCanvas ) ;
45+ addEventListener ( "progress" , handleOnUnityProgress ) ;
46+ addEventListener ( "loaded" , handleOnUnityLoaded ) ;
47+ addEventListener ( "RotationDidUpdate" , handleOnUnityRotationDidUpdate ) ;
48+ addEventListener ( "ClickedPosition" , handleOnUnityClickedPosition ) ;
49+ addEventListener ( "Say" , handleOnUnitySayMessage ) ;
50+ return ( ) => {
51+ removeEventListener ( "canvas" , handleOnUnityCanvas ) ;
52+ removeEventListener ( "progress" , handleOnUnityProgress ) ;
53+ removeEventListener ( "loaded" , handleOnUnityLoaded ) ;
54+ removeEventListener ( "RotationDidUpdate" , handleOnUnityRotationDidUpdate ) ;
55+ removeEventListener ( "ClickedPosition" , handleOnUnityClickedPosition ) ;
56+ removeEventListener ( "Say" , handleOnUnitySayMessage ) ;
4957 } ;
50- } , [ ] ) ;
58+ } , [ addEventListener , removeEventListener ] ) ;
59+
5160
5261 // When the rotation speed has been updated, it will be sent to Unity.
5362 useEffect ( ( ) => {
54- unityContext . send ( "MeshCrate" , "SetRotationSpeed" , rotationSpeed ) ;
55- } , [ rotationSpeed ] ) ;
63+ sendMessage ( "MeshCrate" , "SetRotationSpeed" , rotationSpeed ) ;
64+ } , [ rotationSpeed , sendMessage ] ) ;
5665
5766 // Built-in event invoked when the Unity canvas is ready to be interacted with.
5867 function handleOnUnityCanvas ( canvas : HTMLCanvasElement ) {
@@ -138,7 +147,7 @@ function App() {
138147 </ div >
139148 ) }
140149 { /* The Unity app will be rendered here. */ }
141- < Unity className = "unity-canvas" unityContext = { unityContext } />
150+ < Unity className = "unity-canvas" unityProvider = { unityContext . unityProvider } />
142151 </ div >
143152 { /* Displaying some output values */ }
144153 < p >
0 commit comments