Load all scenes during splash screen

痞子三分冷 提交于 2019-11-30 22:56:07

do I need to change "Application.LoadScene()" method, and what method can I use ?

You need to use SceneManager.LoadSceneAsync if you don't want this to block Unity while loading so many scenes. By using SceneManager.LoadSceneAsync, you will be able to show the loading status.

I want to load all my scenes in splash screen

Create a scene and make sure that this scene loads before any other scene. From there you can loop from 0 to the max index of your scene. You can use SceneManager.GetSceneByBuildIndex to retrieve the Scene from index then SceneManager.SetActiveScene to activate the scene you just retrieved.

List<AsyncOperation> allScenes = new List<AsyncOperation>();
const int sceneMax = 5;
bool doneLoadingScenes = false;

void Startf()
{
    StartCoroutine(loadAllScene());
}

IEnumerator loadAllScene()
{
    //Loop through all scene index
    for (int i = 0; i < sceneMax; i++)
    {
        AsyncOperation scene = SceneManager.LoadSceneAsync(i, LoadSceneMode.Additive);
        scene.allowSceneActivation = false;

        //Add to List so that we don't lose the reference
        allScenes.Add(scene);

        //Wait until we are done loading the scene
        while (scene.progress < 0.9f)
        {
            Debug.Log("Loading scene #:" + i + " [][] Progress: " + scene.progress);
            yield return null;
        }

        //Laod the next one in the loop
    }

    doneLoadingScenes = true;
    OnFinishedLoadingAllScene();
}

void enableScene(int index)
{
    //Activate the Scene
    allScenes[index].allowSceneActivation = true;
    SceneManager.SetActiveScene(SceneManager.GetSceneByBuildIndex(index));
}

void OnFinishedLoadingAllScene()
{
    Debug.Log("Done Loading All Scenes");
}

You can the enableScene(int index) to enable the scene. Note that only one scene can be loaded at a time and you must activate them in the order you loaded them and finally, do not lose the reference of AsyncOperation. This is why I stored them in a List.

If you run into problems, try to remove allScenes[index].allowSceneActivation = true; and scene.allowSceneActivation = false;. I've seen these causing problems sometimes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!