Unity C# What does the funtion Application.CanStreamedLevelBeLoaded(String string) do?

Viewed 207

in the ARFoundation Unity samples I found the following code snippet:

public void BackButtonPressed()
        {
            if (Application.CanStreamedLevelBeLoaded("Menu"))
            {
                SceneManager.LoadScene("Menu", LoadSceneMode.Single);
                LoaderUtility.Deinitialize();
            }
        }

Can somebody please explain to me when and why to use Application.CanStreamedLevelBeLoaded(String string)? In the Unity docs I found this as explanation: Can the streamed level be loaded?

But that tells me nothing.

And when and why should one also use LoadSceneMode.Single and LoaderUtility.Deinitialize()? I found this for LoaderUtility.Deinitialize(): Deinitializes the currently active XR Loader, if one exists. This destroys all subsystems.

I appreciate any help. Thanks!

1 Answers

Application.CanStreamedLevelBeLoaded does exactly what you think it should do when reading the function name, it checks if the streamed level can be loaded. Use it BEFORE attempting to actually load a streamed level. Streamed in this case means downloading the level for the WebGL client. The progress of the stream means how far the download of your level has progressed.

Use this function to check the download state, just like you see in the doc example, to prevent an exception when trying to load an unfinished level.

Behind the scenes it uses a float called downloadProgress of the UnityWebRequest to determine if the file is downloaded.

https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest-downloadProgress.html

Deeper down you will most probably find a FileStream, hence the name of the function.

Related