Skip to content

Commit 047b5d0

Browse files
Merge branch 'patch/unity-loader-error-handling'
2 parents 5a0c3c1 + b4b6a0f commit 047b5d0

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

source/components/unity.ts

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import UnityLoaderService from "../services/unityLoaderService";
77

88
export default class Unity extends PureComponent<IUnityProps, {}> {
99
/**
10-
* The Unity context passed by the props.
10+
* The UnityContext passed by the props.
1111
* @type {UnityContext}
1212
*/
1313
private unityContext: UnityContext = this.props.unityContext;
1414

1515
/**
16-
* The Unity Loader service instance.
16+
* The UnityLoader service instance.
1717
* @type {UnityLoaderService}
1818
*/
1919
private unityLoaderService: UnityLoaderService = new UnityLoaderService();
@@ -24,9 +24,9 @@ export default class Unity extends PureComponent<IUnityProps, {}> {
2424
private htmlCanvasElementReference?: HTMLCanvasElement;
2525

2626
/**
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'.
3030
* @param {number} progression
3131
*/
3232
private onProgress(progression: number): void {
@@ -37,35 +37,53 @@ export default class Unity extends PureComponent<IUnityProps, {}> {
3737
}
3838

3939
/**
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.
4142
*/
42-
public async componentDidMount(): Promise<void> {
43+
public componentDidMount(): void {
4344
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();
6046
}
6147

6248
/**
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.
6451
*/
6552
public componentWillUnmount(): void {
6653
this.unityContext.quitUnityInstance();
6754
}
6855

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+
6987
/**
7088
* Renders the unity wrapper and player.
7189
* @returns {React.ReactNode} element

source/models/unityContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default class UnityContext {
5151
}
5252

5353
/**
54-
* Quits the Unity Instance and removes it from memory.
54+
* Quits the Unity Instance and clears it from memory.
5555
*/
5656
public quitUnityInstance(): void {
5757
if (typeof this.unityInstance !== "undefined")

source/services/unityLoaderService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export default class UnityLoaderService {
3232
this.unityLoaderScript.async = true;
3333
this.unityLoaderScript.src = url;
3434
this.unityLoaderScript.onload = () => resolve();
35+
this.unityLoaderScript.onerror = () => reject(`Unable to load ${url}`);
3536
this.documentHead.appendChild(this.unityLoaderScript);
3637
});
3738
}

0 commit comments

Comments
 (0)