@@ -27,6 +27,13 @@ export class Unity extends PureComponent<IUnityProps, {}> {
27
27
*/
28
28
private htmlCanvasElementReference ?: HTMLCanvasElement ;
29
29
30
+ /**
31
+ * A flag representing the component's mount state
32
+ * @private
33
+ * @type {boolean }
34
+ */
35
+ private mounted : boolean = false ;
36
+
30
37
/**
31
38
* Event invoked by the UnityInstance when the initialization is progressing.
32
39
* Will be used to track the loading progression and invokes the event listeners
@@ -52,6 +59,7 @@ export class Unity extends PureComponent<IUnityProps, {}> {
52
59
* @public
53
60
*/
54
61
public componentDidMount ( ) : void {
62
+ this . mounted = true ;
55
63
this . mountUnityInstance ( ) ;
56
64
}
57
65
@@ -62,6 +70,7 @@ export class Unity extends PureComponent<IUnityProps, {}> {
62
70
*/
63
71
public componentWillUnmount ( ) : void {
64
72
this . unityContext . quitUnityInstance ( ) ;
73
+ this . mounted = false ;
65
74
}
66
75
67
76
/**
@@ -81,6 +90,9 @@ export class Unity extends PureComponent<IUnityProps, {}> {
81
90
await this . unityLoaderService . addFromUrl (
82
91
this . unityContext . unityConfig . loaderUrl
83
92
) ;
93
+ if ( ! this . mounted ) {
94
+ return ;
95
+ }
84
96
const _unityInstanceParameters : IUnityInstanceParameters = {
85
97
...this . unityContext . unityConfig ,
86
98
printErr : ( message : string ) =>
@@ -99,6 +111,9 @@ export class Unity extends PureComponent<IUnityProps, {}> {
99
111
this . onProgress . bind ( this )
100
112
) ;
101
113
this . unityContext . setUnityInstance ( _unityInstance ) ;
114
+ if ( ! this . mounted ) {
115
+ return this . unityContext . quitUnityInstance ( ) ;
116
+ }
102
117
} catch ( message ) {
103
118
this . unityContext . dispatchEventListener ( "error" , message ) ;
104
119
console . error ( "A problem occurred while mounting" , message ) ;
0 commit comments