NGUI 现有的进度条存在的问题:
进度条跳跃式前进,加载到90%后卡住,突然进入下一个场景。接下来就是解决这个问题。
背景
通常游戏的主场景包含的资源较多,这会导致加载场景的时间较长。为了避免这个问题,可以首先加载Loading场景,然后再通过Loading场景来加载主场景。因为Loading场景包含的资源较少,所以加载速度快。在加载主场景的时候一般会在Loading界面中显示一个进度条来告知玩家当前加载的进度。在Unity中可以通过调用Application.LoadLevelAsync函数来异步加载游戏场景,通过查询AsyncOperation.progress的值来得到场景加载的进度。public void LoadGame() { StartCoroutine(StartLoading_1(1)); } private IEnumerator StartLoading_1(int scene) { AsyncOperation op = Application.LoadLevelAsync(scene); while(!op.isDone) { SetLoadingPercentage(op.progress * 100); yield return new WaitForEndOfFrame(); } }
// this function is not work private IEnumerator StartLoading_2() { AsyncOperation op = Application.LoadLevelAsync(1); op.allowSceneActivation = false; while(!op.isDone) { SetLoadingPercentage(op.progress * 100); yield return new WaitForEndOfFrame(); } op.allowSceneActivation = true; }
private IEnumerator StartLoading_3() { AsyncOperation op = Application.LoadLevelAsync(1); op.allowSceneActivation = false; while(op.progress < 0.9f) { SetLoadingPercentage(op.progress * 100); yield return new WaitForEndOfFrame(); } SetLoadingPercentage(100); yield return new WaitForEndOfFrame(); op.allowSceneActivation = true; }
private IEnumerator StartLoading_4() { int displayProgress = 0; int toProgress = 0; AsyncOperation op = Application.LoadLevelAsync(1); 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; }
Unity3d官方论坛.using allowSceneActivation
原文链接:http://www.58player.com/blog-2537-89690.html