加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在启动画面期间加载所有场景

发布时间:2020-12-15 23:40:22 所属栏目:百科 来源:网络整理
导读:我在移动2D统一游戏中有多个场景,我想在启动画面中加载我的所有场景,以便场景传递顺畅.我怎样才能做到这一点 ? 如果我这样做,我是否需要更改“Application.LoadScene()”方法,我可以使用哪种方法? 解决方法 do I need to change “Application.LoadScene()
我在移动2D统一游戏中有多个场景,我想在启动画面中加载我的所有场景,以便场景传递顺畅.我怎样才能做到这一点 ?

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

解决方法

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

如果您不希望在加载这么多场景时阻止Unity,则需要使用SceneManager.LoadSceneAsync.通过使用SceneManager.LoadSceneAsync,您将能够显示加载状态.

I want to load all my scenes in splash screen

创建场景并确保在任何其他场景之前加载此场景.从那里你可以从0循环到场景的最大索引.您可以使用SceneManager.GetSceneByBuildIndex从索引中检索场景,然后使用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;.我有时会看到这些问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读