1
- import '../global' ;
2
-
3
1
import { createElement , HTMLAttributes , useEffect , useState , VFC } from 'react' ;
4
2
5
3
import { UnityContext } from '..' ;
6
4
7
- import { UnityLoaderService } from '../loader' ;
5
+ import { UnityLoaderService } from '../lib/ loader' ;
8
6
9
7
export type UnityRendererProps = Omit <
10
8
HTMLAttributes < HTMLCanvasElement > ,
@@ -65,39 +63,11 @@ export const UnityRenderer: VFC<UnityRendererProps> = ({
65
63
}
66
64
67
65
/**
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.
100
68
*
69
+ * @param {() => void } onComplete Callback function which will be executed
70
+ * after the unmounting has completed.
101
71
*/
102
72
function unmount ( onComplete ?: ( ) => void ) {
103
73
ctx . shutdown ( ( ) => {
@@ -109,10 +79,49 @@ export const UnityRenderer: VFC<UnityRendererProps> = ({
109
79
if ( onUnityReadyStateChange ) onUnityReadyStateChange ( false ) ;
110
80
setLastReadyState ( false ) ;
111
81
82
+ // callbck
112
83
if ( onComplete ) onComplete ( ) ;
113
84
} ) ;
114
85
}
115
86
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
+
116
125
// on loader + renderer ready
117
126
useEffect ( ( ) => {
118
127
if ( ! loader || ! renderer ) return ;
0 commit comments