首页 > 解决方案 > 无法通过 LoadSceneAsync 加载 EasyAr 场景

问题描述

我试图通过脚本 b 异步加载 EasyAR 场景。但它不起作用。

SceneManager.LoadSceneAsync("Scene_name")

如果我尝试使用加载场景,它会起作用

SceneManager.LoadScene("Scene_name")

问题是:有没有办法异步加载它?

使用 c#。
在安卓设备上测试。
Unity 2018.3.12f
Easy AR 版本。3.0


谢谢!

新代码更新:

  private void WallSceneLoader()
    {
        _loaderGameObject.SetActive(true);
        var asyncScene = SceneManager.LoadSceneAsync(Constants.Scenes.AR_SCENE);
        asyncScene.allowSceneActivation = false;

        while (asyncScene.progress < 0.9f)
        {
            var progress = Mathf.Clamp01(asyncScene.progress / 0.9f);
            _loaderBar.value = progress;
        }

        Debug.Log("asyncScene.isDone = true");
        asyncScene.allowSceneActivation = true;}

_loader.gameObject它只是带有进度条的滑块。

标签: c#unity3dasynchronoussceneeasyar

解决方案


通过在“正常”方法中使用此while循环,您无论如何都会阻塞线程,因此您在这里失去了异步的全部优势。


你宁愿做的是在协程中使用它,就像它实际上也在示例中显示的那样SceneManager.LoadSceneAsync

private IEnumerator WallSceneLoader()
{
    _loaderGameObject.SetActive(true);
    var asyncScene = SceneManager.LoadSceneAsync(Constants.Scenes.AR_SCENE);
    asyncScene.allowSceneActivation = false;

    while (asyncScene.progress < 0.9f)
    {
        var progress = Mathf.Clamp01(asyncScene.progress / 0.9f);
        _loaderBar.value = progress;

        // tells Unity to pause the routine here
        // render the frame and continue from here in the next frame
        yield return null;
    }

    Debug.Log("asyncScene.isDone = true");
    asyncScene.allowSceneActivation = true;
}

然后StartCoroutine在你想调用它的地方使用

StartCoroutine(WalSceneLoader());

推荐阅读