diff --git a/.gitignore b/.gitignore index 146501ec..9fe21553 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ /distribution # System -.DS_Store \ No newline at end of file +.DS_Store +*.swp diff --git a/source/components/unity.ts b/source/components/unity.ts index 65f54557..fa433c70 100644 --- a/source/components/unity.ts +++ b/source/components/unity.ts @@ -27,6 +27,13 @@ export class Unity extends PureComponent { */ private htmlCanvasElementReference?: HTMLCanvasElement; + /** + * A flag representing the component's mount state + * @private + * @type {boolean} + */ + private mounted: boolean = false; + /** * Event invoked by the UnityInstance when the initialization is progressing. * Will be used to track the loading progression and invokes the event listeners @@ -52,6 +59,7 @@ export class Unity extends PureComponent { * @public */ public componentDidMount(): void { + this.mounted = true; this.mountUnityInstance(); } @@ -62,6 +70,7 @@ export class Unity extends PureComponent { */ public componentWillUnmount(): void { this.unityContext.quitUnityInstance(); + this.mounted = false; } /** @@ -81,6 +90,9 @@ export class Unity extends PureComponent { await this.unityLoaderService.addFromUrl( this.unityContext.unityConfig.loaderUrl ); + if (!this.mounted) { + return; + } const _unityInstanceParameters: IUnityInstanceParameters = { ...this.unityContext.unityConfig, printErr: (message: string) => @@ -99,6 +111,9 @@ export class Unity extends PureComponent { this.onProgress.bind(this) ); this.unityContext.setUnityInstance(_unityInstance); + if (!this.mounted) { + return this.unityContext.quitUnityInstance(); + } } catch (message) { this.unityContext.dispatchEventListener("error", message); console.error("A problem occurred while mounting", message);