11import { useEffect , useState } from "react" ;
22import { isBrowserEnvironment } from "../constants/is-browser-environment" ;
33import { 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