Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
/distribution

# System
.DS_Store
.DS_Store
*.swp
15 changes: 15 additions & 0 deletions source/components/unity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export class Unity extends PureComponent<IUnityProps, {}> {
*/
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
Expand All @@ -52,6 +59,7 @@ export class Unity extends PureComponent<IUnityProps, {}> {
* @public
*/
public componentDidMount(): void {
this.mounted = true;
this.mountUnityInstance();
}

Expand All @@ -62,6 +70,7 @@ export class Unity extends PureComponent<IUnityProps, {}> {
*/
public componentWillUnmount(): void {
this.unityContext.quitUnityInstance();
this.mounted = false;
}

/**
Expand All @@ -81,6 +90,9 @@ export class Unity extends PureComponent<IUnityProps, {}> {
await this.unityLoaderService.addFromUrl(
this.unityContext.unityConfig.loaderUrl
);
if (!this.mounted) {
return;
}
const _unityInstanceParameters: IUnityInstanceParameters = {
...this.unityContext.unityConfig,
printErr: (message: string) =>
Expand All @@ -99,6 +111,9 @@ export class Unity extends PureComponent<IUnityProps, {}> {
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);
Expand Down