Skip to content

Commit bd4cd01

Browse files
Unity Loader uses Unity Config and destructures its own requirements
1 parent 40578c4 commit bd4cd01

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

module/source/components/unity-component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const Unity: ForwardRefExoticComponent<
4545
* URL from the Unity Provider's Unity Config.
4646
*/
4747
const unityLoaderStatus = useUnityLoader(
48-
unityProps.unityProvider.unityConfig.loaderUrl
48+
unityProps.unityProvider.unityConfig
4949
);
5050

5151
// The Unity Instance is created based on the Unity Arguments. The loader

module/source/hooks/use-unity-loader.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { useEffect, useState } from "react";
22
import { isBrowserEnvironment } from "../constants/is-browser-environment";
33
import { UnityLoaderStatus } from "../enums/unity-loader-status";
4+
import { UnityConfig } from "../exports";
45

56
/**
67
* Hook to embed a Unity Loader script.
78
* @param source The source of the unity loader.
89
* @returns a hook that returns the status of the loader.
910
*/
10-
const useUnityLoader = (source: string): UnityLoaderStatus => {
11+
const useUnityLoader = (unityConfig: UnityConfig): UnityLoaderStatus => {
1112
const [status, setStatus] = useState<UnityLoaderStatus>(
1213
UnityLoaderStatus.Loading
1314
);
@@ -20,7 +21,7 @@ const useUnityLoader = (source: string): UnityLoaderStatus => {
2021
return;
2122
}
2223
// If the script's source is null, we'll reset the status to idle.
23-
if (source === null) {
24+
if (unityConfig.loaderUrl === null) {
2425
setStatus(UnityLoaderStatus.Idle);
2526
return;
2627
}
@@ -29,14 +30,14 @@ const useUnityLoader = (source: string): UnityLoaderStatus => {
2930
* another instance of this hook.
3031
*/
3132
let script: HTMLScriptElement | null = window.document.querySelector(
32-
`script[src="${source}"]`
33+
`script[src="${unityConfig.loaderUrl}"]`
3334
);
3435
// If there wan't another instance of this script, we're going to create a
3536
// new one with the provided source.
3637
if (script === null) {
3738
script = window.document.createElement("script");
3839
script.type = "text/javascript";
39-
script.src = source;
40+
script.src = unityConfig.loaderUrl;
4041
script.async = true;
4142
script.setAttribute("data-status", "loading");
4243
// Add script to window.document body.
@@ -80,7 +81,7 @@ const useUnityLoader = (source: string): UnityLoaderStatus => {
8081
window.document.body.removeChild(script);
8182
}
8283
};
83-
}, [source]);
84+
}, [unityConfig.loaderUrl]);
8485

8586
return status;
8687
};

0 commit comments

Comments
 (0)