Skip to content

Commit bec2c65

Browse files
Fixed an issue where the canvas reference could not be send
1 parent caab1bf commit bec2c65

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

source/components/unity.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,26 @@ export function Unity(props: IUnityProps): ReactElement {
5050
// Effect hook will be called when the reference to the canvas changes.
5151
useEffect(
5252
function () {
53-
const htmlCanvaselement = htmlCanvasElement.current;
5453
// Whether or not the canvas has been defined, it will be set as the
5554
// current html canvas element on the Unity Context.
56-
unityContext.htmlCanvasElement = htmlCanvaselement;
57-
// But only when it is defined, an event will be dispatched.
58-
if (htmlCanvaselement !== null) {
59-
unityContext.dispatchEvent("canvas", htmlCanvaselement);
60-
}
55+
unityContext.htmlCanvasElement = htmlCanvasElement.current;
6156
},
6257
[htmlCanvasElement]
6358
);
6459

6560
// Effect hook will be called when the Unity Instance progession changes.
6661
useEffect(
6762
function () {
68-
// If the Unity Instance loading progression hits 1, then the Unity
69-
// Instance is ready to be used and the loaded event is dispatched.
63+
// If the Unity Instance loading progression hits 1, then this means that
64+
//the Unity Instance is initialized successfully and is ready to be used.
7065
if (progression === 1) {
66+
// When the application is loaded, we'll send over a reference to the
67+
// canvas element. When the HTML Canvas Element ref is defined, an event
68+
// will be dispatched.
69+
if (htmlCanvasElement !== null) {
70+
unityContext.dispatchEvent("canvas", htmlCanvasElement);
71+
}
72+
// The loaded event is dispatched.
7173
unityContext.dispatchEvent("loaded");
7274
}
7375
// Dispatches an event every time the Unity Instance progression changes.

0 commit comments

Comments
 (0)