思路
使用 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;
}
}
效果
参考:
http://blog.csdn.net/qq_33747722/article/details/72582213