Unity async 真的异步工作吗?

时间:2021-01-29 13:14:05

标签: multithreading unity3d coroutine

嗨,我将在场景 A 和场景 B 之间制作一个加载屏幕。 我现在已经使用 Async Operation 异步引入 Thin B。 但我不能那样做。

我记录了一个日志,所有屏幕都停止,直到异步操作的进度达到我随机设置的 0.9。

如果进度超过 0.9,那么一切都会开始工作。

这是为什么?是不是因为Thin B的容量很大?

如果我不能使用异步操作,我应该如何制作加载屏幕? 请帮我!我在下面附上了我的代码。

  IEnumerator Start()
    {
        yield return null;
        StartCoroutine(LoadingProgress(loadSceneName));
    }

    IEnumerator LoadingProgress(string loadSceneName)
    {
        AsyncOperation async = SceneManager.LoadSceneAsync(loadSceneName);
        
        async.allowSceneActivation = false;
       
        float timer = 0.0f;

        while (!async.isDone)
        {
            timer += Time.deltaTime;

            if (async.progress >= 0.9f)
            {
                loadingBar.fillAmount = Mathf.Lerp(loadingBar.fillAmount, 1f, timer);
                if (loadingBar.fillAmount == 1.0f)
                {
                    async.allowSceneActivation = true;
                    yield break;
                }
            }
            else
            {
                loadingBar.fillAmount = Mathf.Lerp(loadingBar.fillAmount, async.progress, timer);

                if (loadingBar.fillAmount >= async.progress)
                {
                    timer = 0f;
                }
            }

            yield return null;
        }
    }

0 个答案:

没有答案