@@ -7,13 +7,13 @@ import UnityLoaderService from "../services/unityLoaderService";
7
7
8
8
export default class Unity extends PureComponent < IUnityProps , { } > {
9
9
/**
10
- * The Unity context passed by the props.
10
+ * The UnityContext passed by the props.
11
11
* @type {UnityContext }
12
12
*/
13
13
private unityContext : UnityContext = this . props . unityContext ;
14
14
15
15
/**
16
- * The Unity Loader service instance.
16
+ * The UnityLoader service instance.
17
17
* @type {UnityLoaderService }
18
18
*/
19
19
private unityLoaderService : UnityLoaderService = new UnityLoaderService ( ) ;
@@ -24,9 +24,9 @@ export default class Unity extends PureComponent<IUnityProps, {}> {
24
24
private htmlCanvasElementReference ?: HTMLCanvasElement ;
25
25
26
26
/**
27
- * An event that is triggered by the Unity player. This tracks
28
- * the loading progression of the player. It will send '1' when
29
- * the loading is completed .
27
+ * Event invoked by the UnityInstance when the initialization is progressing.
28
+ * Will be used to track the loading progression and invokes the event listeners
29
+ * for both 'progress' and 'loaded' when the progression hits a value of '1' .
30
30
* @param {number } progression
31
31
*/
32
32
private onProgress ( progression : number ) : void {
@@ -37,35 +37,53 @@ export default class Unity extends PureComponent<IUnityProps, {}> {
37
37
}
38
38
39
39
/**
40
- * Initialzied the Unity player when the component is mounted.
40
+ * Event invoked when the component is mounted. This sets the component
41
+ * reference and starts the mounting of the UnityInstance.
41
42
*/
42
- public async componentDidMount ( ) : Promise < void > {
43
+ public componentDidMount ( ) : void {
43
44
this . unityContext . setComponentReference ( this ) ;
44
- await this . unityLoaderService . load ( this . unityContext . unityConfig . loaderUrl ) ;
45
- const _unityInstanceParamters = {
46
- dataUrl : this . unityContext . unityConfig . dataUrl ,
47
- frameworkUrl : this . unityContext . unityConfig . frameworkUrl ,
48
- codeUrl : this . unityContext . unityConfig . codeUrl ,
49
- streamingAssetsUrl : this . unityContext . unityConfig . streamingAssetsUrl ,
50
- companyName : this . unityContext . unityConfig . companyName ,
51
- productName : this . unityContext . unityConfig . productName ,
52
- productVersion : this . unityContext . unityConfig . productVersion ,
53
- } ;
54
- const _unityInstance = await window . createUnityInstance (
55
- this . htmlCanvasElementReference ! ,
56
- _unityInstanceParamters ,
57
- this . onProgress . bind ( this )
58
- ) ;
59
- this . unityContext . setUnityInstance ( _unityInstance ) ;
45
+ this . mountUnityInstance ( ) ;
60
46
}
61
47
62
48
/**
63
- * When the component will be unmounted, the UnityInstance will quit.
49
+ * Event invoked when the component will unmount. This force quits the
50
+ * UnityInstance which will clear it from the memory.
64
51
*/
65
52
public componentWillUnmount ( ) : void {
66
53
this . unityContext . quitUnityInstance ( ) ;
67
54
}
68
55
56
+ /**
57
+ * Initialized the Unity Loader and mounts the UnityInstance to the component.
58
+ */
59
+ private async mountUnityInstance ( ) : Promise < void > {
60
+ try {
61
+ await this . unityLoaderService . load (
62
+ this . unityContext . unityConfig . loaderUrl
63
+ ) ;
64
+ const _unityInstanceParamters = {
65
+ dataUrl : this . unityContext . unityConfig . dataUrl ,
66
+ frameworkUrl : this . unityContext . unityConfig . frameworkUrl ,
67
+ codeUrl : this . unityContext . unityConfig . codeUrl ,
68
+ streamingAssetsUrl : this . unityContext . unityConfig . streamingAssetsUrl ,
69
+ companyName : this . unityContext . unityConfig . companyName ,
70
+ productName : this . unityContext . unityConfig . productName ,
71
+ productVersion : this . unityContext . unityConfig . productVersion ,
72
+ } ;
73
+ const _unityInstance = await window . createUnityInstance (
74
+ this . htmlCanvasElementReference ! ,
75
+ _unityInstanceParamters ,
76
+ this . onProgress . bind ( this )
77
+ ) ;
78
+ this . unityContext . setUnityInstance ( _unityInstance ) ;
79
+ } catch ( error ) {
80
+ console . warn (
81
+ "Something went wrong while mouting the UnityInstance" ,
82
+ error
83
+ ) ;
84
+ }
85
+ }
86
+
69
87
/**
70
88
* Renders the unity wrapper and player.
71
89
* @returns {React.ReactNode } element
0 commit comments