在闪屏界面加载所有场景

5

我在我的移动2D Unity游戏中有多个场景,我想在闪屏界面加载所有场景,这样场景之间的过渡会更加流畅。我该怎么做?

如果我这样做了,我需要改变“Application.LoadScene()”方法吗?我可以使用哪种方法?

1个回答

8
我需要改变"Application.LoadScene()"方法吗?有什么其他方法可以使用?
如果你不想在加载这么多场景时阻塞Unity,你需要使用SceneManager.LoadSceneAsync。通过使用SceneManager.LoadSceneAsync,你将能够显示加载状态。
我想在闪屏界面中加载所有场景
创建一个场景并确保该场景在任何其他场景之前加载。从那里,你可以从0循环到你场景的最大索引。你可以使用SceneManager.GetSceneByBuildIndex来从索引检索Scene,然后使用SceneManager.SetActiveScene来激活你刚刚检索到的场景。
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");
}

你可以使用enableScene(int index)来启用场景。请注意,一次只能加载一个场景,并且必须按照加载顺序激活它们,最后不要丢失AsyncOperation的引用。这就是为什么我将它们存储在List中的原因。
如果遇到问题,请尝试删除allScenes[index].allowSceneActivation = true;scene.allowSceneActivation = false;。有时候我看到这些会引起问题。

当我尝试激活一个场景时,出现错误ArgumentException: SceneManager.SetActiveScene failed; scene 'EN_FlashCard_Landscape_Portable' is not loaded and therefore cannot be set active...有什么想法吗? - questionasker
@anunixercoder 问题很可能出在 SceneManager.SetActiveScene 上。请确保你传递给 SceneManager.SetActiveScene 的场景已经被加载。如果没有加载,你会得到这个错误。最后,如果你确定已经加载了场景,请在调用 SceneManager.SetActiveScene 前等待一帧。 - Programmer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接