【问题标题】:Load all scenes during splash screen在启动画面期间加载所有场景
【发布时间】:2017-08-17 18:37:30
【问题描述】:

我的移动 2D Unity 游戏中有多个场景,我想将我的所有场景加载到启动画面中,这样场景通过就会顺畅。我怎样才能做到这一点 ?

如果这样做,是否需要更改“Application.LoadScene()”方法,我可以使用什么方法?

【问题讨论】:

    标签: c# unity3d splash-screen unity2d


    【解决方案1】:

    我是否需要更改“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...有什么想法吗?
    • @anunixercoder 问题很可能是SceneManager.SetActiveScene。确保您正在传递的场景 SceneManager.SetActiveScene 已加载。如果不是,您会收到此错误。最后,如果您确定,请等待一帧后再调用SceneManager.SetActiveScene
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    相关资源
    最近更新 更多