1
1
import { useEffect , useState } from "react" ;
2
2
import { isBrowserEnvironment } from "../constants/is-browser-environment" ;
3
3
import { UnityLoaderStatus } from "../enums/unity-loader-status" ;
4
+ import { UnityConfig } from "../exports" ;
4
5
5
6
/**
6
7
* Hook to embed a Unity Loader script.
7
8
* @param source The source of the unity loader.
8
9
* @returns a hook that returns the status of the loader.
9
10
*/
10
- const useUnityLoader = ( source : string ) : UnityLoaderStatus => {
11
+ const useUnityLoader = ( unityConfig : UnityConfig ) : UnityLoaderStatus => {
11
12
const [ status , setStatus ] = useState < UnityLoaderStatus > (
12
13
UnityLoaderStatus . Loading
13
14
) ;
@@ -20,7 +21,7 @@ const useUnityLoader = (source: string): UnityLoaderStatus => {
20
21
return ;
21
22
}
22
23
// If the script's source is null, we'll reset the status to idle.
23
- if ( source === null ) {
24
+ if ( unityConfig . loaderUrl === null ) {
24
25
setStatus ( UnityLoaderStatus . Idle ) ;
25
26
return ;
26
27
}
@@ -29,14 +30,14 @@ const useUnityLoader = (source: string): UnityLoaderStatus => {
29
30
* another instance of this hook.
30
31
*/
31
32
let script : HTMLScriptElement | null = window . document . querySelector (
32
- `script[src="${ source } "]`
33
+ `script[src="${ unityConfig . loaderUrl } "]`
33
34
) ;
34
35
// If there wan't another instance of this script, we're going to create a
35
36
// new one with the provided source.
36
37
if ( script === null ) {
37
38
script = window . document . createElement ( "script" ) ;
38
39
script . type = "text/javascript" ;
39
- script . src = source ;
40
+ script . src = unityConfig . loaderUrl ;
40
41
script . async = true ;
41
42
script . setAttribute ( "data-status" , "loading" ) ;
42
43
// Add script to window.document body.
@@ -80,7 +81,7 @@ const useUnityLoader = (source: string): UnityLoaderStatus => {
80
81
window . document . body . removeChild ( script ) ;
81
82
}
82
83
} ;
83
- } , [ source ] ) ;
84
+ } , [ unityConfig . loaderUrl ] ) ;
84
85
85
86
return status ;
86
87
} ;
0 commit comments