代码:
public void LoadGameMethod()
{
StartCoroutine(StartLoading_4(2));
}
private IEnumerator StartLoading_4(int scene)
{
int displayProgress = 0;
int toProgress = 0;
AsyncOperation op = SceneManager.LoadSceneAsync(scene);
op.allowSceneActivation = false;
while (op.progress < 0.9f)
{
toProgress = (int)op.progress * 100;
while (displayProgress < toProgress)
{
++displayProgress;
SetLoadingPercentage(displayProgress);
yield return new WaitForEndOfFrame();
}
}
toProgress = 100;
while (displayProgress < toProgress)
{
++displayProgress;
SetLoadingPercentage(displayProgress);
yield return new WaitForEndOfFrame();
}
op.allowSceneActivation = true;
}
private void SetLoadingPercentage(float v)
{
processView.value = v / 100;
}**
解释:
SceneManager.LoadSceneAsync(scene); 异步加载函数返回的是一个协程对象,定义AsyncOperation 对象用来接收,AsyncOperation 类中的allowSceneActivation的bool属性变量,为false的时候,该类中的鸡杂游戏的进度pocess属性值保持在0.9,不再变化,为true的时候会继续记载剩余的0.1,另外剩余的0.1加载速度是非常快的,几毫秒就会完成,所以先将allowSceneActivation值设为false,让现实的进度条从0开始记载到0.89,然后再直接从0.89加载完成,最后把allowSceneActivation设为true,这样玩家看到的效果就是当进度条完成后直接进入下一个场景,不会看到还未加载完成的下一个场景。
WaitForEndofFrame()函数等待camera和GUI渲染结束,在加载下一帧**