在Unity3D中异步加载场景不起作用

3

我已经做了一些关于Unity如何异步加载场景的研究。到目前为止,我发现有两种非常相似的方法,它们基于同样的原理。

   StartCoroutiune(loadScene());

    private AsyncOperation async;

     // ...

    IEnumerator loadScene(){
          async = SceneManager.LoadAsyncScene("Scene", LoadSceneMode.Single);
            async.allowSceneActivation = false;
            while(async.progress < 0.9f){
                  progressText.text = async.progress+"";
            }
           while(!async.isDone){
                  yield return null;
            }

    }

    public void showScene(){
     async.allowSceneActivation = true;
    }

然而这对我并没有起作用。我仍然需要等待很长时间才能加载,并且场景会立即显示,即使我还没有调用显示它的代码。我还尝试过执行

SceneManager.LoadAsyncScene("Scene", LoadSceneMode.Additive);

这是我负责的课程,旨在完成这项工作。如果我的错误太简单,请见谅,我是Unity的新手。谢谢。

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager_StartGame : MonoBehaviour
{
    private GameManager_MasterMenu gameManagerRef;
    private AsyncOperation loadGame;

    private void OnEnable()
    {
        SetInitialReferences();
        StartCoroutine("loadMainScene");//loads scene before the game starts
        gameManagerRef.ContinueGameEvent += StartGame; //subscribing the StartGame method to an Event
    }

    private void OnDisable()
    {
       gameManagerRef.ContinueGameEvent -= StartGame;//getting the references to the game Manager
    }

    void SetInitialReferences()
    {
        gameManagerRef = GetComponent<GameManager_MasterMenu>();
    }

    IEnumerator loadMainScene()
    {
        Debug.LogWarning("ASYNC LOAD STARTED - " +
        "DO NOT EXIT PLAY MODE UNTIL SCENE LOADS... UNITY WILL CRASH");
        loadGame = SceneManager.LoadSceneAsync(1,LoadSceneMode.Single);
        loadGame.allowSceneActivation = false;//setting the allowscene to false so that it won't show it immediately
        yield return loadGame;
    }


    void StartGame()
    {
        if (GameReferences.currentSave == null)
        {
            GameReferences.currentSave = GameReferences.dBConnector.GetLastSave();
        }
        loadGame.allowSceneActivation = true; //is activated from the outside
    }
}
3个回答

2
由于进程在 while 循环中进行,当加载时您永远不会触发 yield。 尝试将其放入异步 while 循环中,如下所示:
IEnumerator loadScene(){
      async = SceneManager.LoadAsyncScene("Scene", LoadSceneMode.Single);
      async.allowSceneActivation = false;

      while(!async.isDone){
              progressText.text = async.progress+"";
              yield return null;
      }

}

0
using UnityEngine.SceneManagement;
   
SceneManager.LoadScene(1, LoadSceneMode.Single);

//SceneManager.LoadSceneAsync - (Loads the Scene asynchronously in the background.)
//1 - (BuildIndex or "my scene" StringName.)
//LoadSceneMode.Additive - (Adds the Scene to the current loaded Scenes.)
//LoadSceneMode.Single - (Closes all current loaded Scenes and loads a Scene.)

3
你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

0
补充一下siusiulala的回答(还不能评论),我要提醒大家在一个独立于编辑器播放模式的构建玩家中测试异步加载。Unity的编辑器播放模式无法很好地处理异步加载,经常看起来就像同步加载一样,所以即使修复了实现问题,你仍然应该在构建的玩家中进行测试。

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