UnityWebGL______不要在协程里同时加载场景和显示加载进度条

在webGL平台,我在协程里面写的异步加载场景和进度条读条的方法会直接卡死不能用,但是它在pc平台是管用的。

解决办法是,异步加载场景可以写在协程里,进度条读条和更新的代码写在Update或者FixedUpdate里,这样就没问题。

原因我不知道,谁知道并且看到了这篇文章,烦请指点一下,谢谢。

下面是我原来的代码:

/// 
    /// 异步加载场景
    /// 
    /// 
    /// 
    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

 

 

 

这是我修改后的代码: 

  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();
        }
    }

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

 

你可能感兴趣的:(Unity3D)