Skip to content

Commit

Permalink
Unity Loader uses Unity Config and destructures its own requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreylanters committed Mar 6, 2023
1 parent 40578c4 commit bd4cd01
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion module/source/components/unity-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const Unity: ForwardRefExoticComponent<
* URL from the Unity Provider's Unity Config.
*/
const unityLoaderStatus = useUnityLoader(
unityProps.unityProvider.unityConfig.loaderUrl
unityProps.unityProvider.unityConfig
);

// The Unity Instance is created based on the Unity Arguments. The loader
Expand Down
11 changes: 6 additions & 5 deletions module/source/hooks/use-unity-loader.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useEffect, useState } from "react";
import { isBrowserEnvironment } from "../constants/is-browser-environment";
import { UnityLoaderStatus } from "../enums/unity-loader-status";
import { UnityConfig } from "../exports";

/**
* Hook to embed a Unity Loader script.
* @param source The source of the unity loader.
* @returns a hook that returns the status of the loader.
*/
const useUnityLoader = (source: string): UnityLoaderStatus => {
const useUnityLoader = (unityConfig: UnityConfig): UnityLoaderStatus => {
const [status, setStatus] = useState<UnityLoaderStatus>(
UnityLoaderStatus.Loading
);
Expand All @@ -20,7 +21,7 @@ const useUnityLoader = (source: string): UnityLoaderStatus => {
return;
}
// If the script's source is null, we'll reset the status to idle.
if (source === null) {
if (unityConfig.loaderUrl === null) {
setStatus(UnityLoaderStatus.Idle);
return;
}
Expand All @@ -29,14 +30,14 @@ const useUnityLoader = (source: string): UnityLoaderStatus => {
* another instance of this hook.
*/
let script: HTMLScriptElement | null = window.document.querySelector(
`script[src="${source}"]`
`script[src="${unityConfig.loaderUrl}"]`
);
// If there wan't another instance of this script, we're going to create a
// new one with the provided source.
if (script === null) {
script = window.document.createElement("script");
script.type = "text/javascript";
script.src = source;
script.src = unityConfig.loaderUrl;
script.async = true;
script.setAttribute("data-status", "loading");
// Add script to window.document body.
Expand Down Expand Up @@ -80,7 +81,7 @@ const useUnityLoader = (source: string): UnityLoaderStatus => {
window.document.body.removeChild(script);
}
};
}, [source]);
}, [unityConfig.loaderUrl]);

return status;
};
Expand Down

0 comments on commit bd4cd01

Please sign in to comment.