Unity异步加载场景与加载进度条

思路

使用 SceneManager.LoadSceneAsync 异步加载场景,阻止当加载完成自动切换,使用 AsyncOperation.progress 获取加载进度,显示在进度条上。当进度条为百分之百时切换场景。

关键代码

IEnumerator AsyncLoading()  
    {  
        operation = SceneManager.LoadSceneAsync(Globe.nextSceneName);  
        //阻止当加载完成自动切换  
        operation.allowSceneActivation = false;  
  
        yield return operation;  
    }  
void Update ()  
    {  
        targetValue = operation.progress;  
  
        if (operation.progress >= 0.9f)  
        {  
            //operation.progress的值最大为0.9  
            targetValue = 1.0f;  
        }  
  
        if (targetValue != loadingSlider.value)  
        {  
            //插值运算  
            loadingSlider.value = Mathf.Lerp(loadingSlider.value, targetValue, Time.deltaTime * loadingSpeed);  
            if (Mathf.Abs(loadingSlider.value - targetValue) < 0.01f)  
            {  
                loadingSlider.value = targetValue;  
            }  
        }  
      
        loadingText.text = ((int)(loadingSlider.value * 100)).ToString() + "%";  
  
        if ((int)(loadingSlider.value * 100) == 100)  
        {  
            //允许异步加载完毕后自动切换场景  
            operation.allowSceneActivation = true;  
        }  
    }  

效果

image

参考:
http://blog.csdn.net/qq_33747722/article/details/72582213

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容