1- import '../global' ;
2-
31import { createElement , HTMLAttributes , useEffect , useState , VFC } from 'react' ;
42
53import { UnityContext } from '..' ;
64
7- import { UnityLoaderService } from '../loader' ;
5+ import { UnityLoaderService } from '../lib/ loader' ;
86
97export type UnityRendererProps = Omit <
108 HTMLAttributes < HTMLCanvasElement > ,
@@ -65,39 +63,11 @@ export const UnityRenderer: VFC<UnityRendererProps> = ({
6563 }
6664
6765 /**
68- * Uses the native Unity loader method to attach the Unity instance to
69- * the renderer `canvas`.
70- *
71- * @returns {Promise<void> } A Promise resolving on successful mount of the
72- * Unity instance.
73- */
74- async function mount ( ) : Promise < void > {
75- // get the current loader configuration from the UnityContext
76- const c = ctx . getConfig ( ) ;
77-
78- // attach Unity's native JavaScript loader
79- await loader ! . execute ( c . loaderUrl ) ;
80-
81- const instance = await window . createUnityInstance (
82- renderer ! ,
83- {
84- dataUrl : c . dataUrl ,
85- frameworkUrl : c . frameworkUrl ,
86- codeUrl : c . codeUrl ,
87- streamingAssetsUrl : c . streamingAssetsUrl ,
88- companyName : c . companyName ,
89- productName : c . productName ,
90- productVersion : c . productVersion ,
91- } ,
92- ( p ) => onUnityProgress ( p )
93- ) ;
94-
95- // set the instance for further JavaScript <--> Unity communication
96- ctx . setInstance ( instance ) ;
97- }
98-
99- /**
66+ * Unmounts the game by shutting its instance down, removing the loader
67+ * script from the DOM and sending the appropriate events via the props.
10068 *
69+ * @param {() => void } onComplete Callback function which will be executed
70+ * after the unmounting has completed.
10171 */
10272 function unmount ( onComplete ?: ( ) => void ) {
10373 ctx . shutdown ( ( ) => {
@@ -109,10 +79,49 @@ export const UnityRenderer: VFC<UnityRendererProps> = ({
10979 if ( onUnityReadyStateChange ) onUnityReadyStateChange ( false ) ;
11080 setLastReadyState ( false ) ;
11181
82+ // callbck
11283 if ( onComplete ) onComplete ( ) ;
11384 } ) ;
11485 }
11586
87+ /**
88+ * Uses the native Unity loader method to attach the Unity instance to
89+ * the renderer `canvas`.
90+ *
91+ * @returns {Promise<void> } A Promise resolving on successful mount of the
92+ * Unity instance.
93+ */
94+ async function mount ( ) : Promise < void > {
95+ try {
96+ // get the current loader configuration from the UnityContext
97+ const c = ctx . getConfig ( ) ;
98+
99+ // attach Unity's native JavaScript loader
100+ await loader ! . execute ( c . loaderUrl ) ;
101+
102+ const instance = await window . createUnityInstance (
103+ renderer ! ,
104+ {
105+ dataUrl : c . dataUrl ,
106+ frameworkUrl : c . frameworkUrl ,
107+ codeUrl : c . codeUrl ,
108+ streamingAssetsUrl : c . streamingAssetsUrl ,
109+ companyName : c . companyName ,
110+ productName : c . productName ,
111+ productVersion : c . productVersion ,
112+ } ,
113+ ( p ) => onUnityProgress ( p )
114+ ) ;
115+
116+ // set the instance for further JavaScript <--> Unity communication
117+ ctx . setInstance ( instance ) ;
118+ } catch ( e ) {
119+ unmount ( ( ) => {
120+ if ( onUnityError ) onUnityError ( e ) ;
121+ } ) ;
122+ }
123+ }
124+
116125 // on loader + renderer ready
117126 useEffect ( ( ) => {
118127 if ( ! loader || ! renderer ) return ;
0 commit comments