大部分代码参考了
http://www.58player.com/blog-2537-89690.html
不过,它的逻辑有地方不太严谨,对比一下即可。
/// <summary>
/// 异步加载场景
/// </summary>
public void LoadScene()
{
if(!string.IsNullOrEmpty(loadScenePath))
{
StartCoroutine(loadScenceAsyn());
}
}
/// <summary>
/// 加载速度
/// </summary>
public int LoadSpeed = 1;
private IEnumerator loadScenceAsyn()
{
asynOp = Application.LoadLevelAsync(loadScenePath);
if (show == false || asynOp == null)
{
yield break;
}
int displayProgress = 0;
int toProgress = 0;
asynOp.allowSceneActivation = false;
while (asynOp.progress < 0.9f)
{
toProgress = (int)(asynOp.progress * 100);
while (displayProgress < toProgress)
{
displayProgress += LoadSpeed;
SetProgressBarValue(displayProgress);
yield return new WaitForEndOfFrame();
}
yield return new WaitForEndOfFrame();
}
toProgress = 100;
while (displayProgress < toProgress)
{
displayProgress += LoadSpeed;
SetProgressBarValue(displayProgress);
yield return new WaitForEndOfFrame();
}
asynOp.allowSceneActivation = true;
while (!asynOp.isDone)
{
yield return new WaitForEndOfFrame();
}
StartDelyHideLoadingUI();
}