UnityWebGL______Do not load the scene and display the loading progress bar at the same time in the coroutine

On the webGL platform, the method of asynchronously loading the scene and reading the progress bar that I wrote in the coroutine will directly freeze and cannot be used, but it works on the pc platform.

The solution is that the asynchronous loading scene can be written in the coroutine, and the progress bar reading bar and update code can be written in Update or FixedUpdate, so there is no problem.

I don't know the reason, who knows and saw this article, please give pointers, thank you.

Below is my original code:

/// <summary>
    /// 异步加载场景
    /// </summary>
    /// <param name="_name"></param>
    /// <returns></returns>
    IEnumerator AsyncLoading(string _name)
    {
        int displayProgress = 0;
        int toProgress = 0;
        //异步加载关卡
        operation = SceneManager.LoadSceneAsync(_name);
        //阻止当加载完成自动切换
        operation.allowSceneActivation = false;
        while(operation.progress<0.9f)
        {
            toProgress = (int)operation.progress * 100;
            while(displayProgress<toProgress)
            {
                ++displayProgress;
                SetLoadingPercentage(displayProgress);
                yield return new WaitForFixedUpdate();
            }
        }
        toProgress = 100;
        while(displayProgress<toProgress)
        {
            ++displayProgress;
            SetLoadingPercentage(displayProgress);
            yield return new WaitForFixedUpdate();
        }
        yield return new WaitForSeconds(0.5f);
        CompleteLoadScene();
    }

 

 

 

Here is my modified code: 

  void FixedUpdate()
    {
        if(loadScene)
        {
            while (operation.progress < 0.9f)
            {
                toProgress = (int)(operation.progress * 100);
                while (displayProgress < toProgress)
                {
                    ++displayProgress;
                    SetLoadingPercentage(displayProgress);
                    return;
                }
            }
            toProgress = 100;
            while (displayProgress < toProgress)
            {
                ++displayProgress;
                SetLoadingPercentage(displayProgress);
                return;
            }
            CompleteLoadScene();
        }
    }

    /// <summary>
    /// 异步加载场景
    /// </summary>
    /// <returns></returns>
    IEnumerator AsyncLoading()
    {
        //异步加载关卡
        operation = SceneManager.LoadSceneAsync(targetSceneName);
        //阻止当加载完成自动切换
        operation.allowSceneActivation = false;
        loadScene = true;
        yield return operation;
    }

 

Guess you like

Origin blog.csdn.net/qq_37760273/article/details/106782659