一般游戏的主场景包含的资源较多,这会致使加载场景的时间较长。为了不这个问题,能够首先加载Loading场景,而后再经过Loading场景来加载主场景。由于Loading场景包含的资源较少,因此加载速度快。在加载主场景的时候通常会在Loading界面中显示一个进度条来告知玩家当前加载的进度。在Unity中能够经过调用Application.LoadLevelAsync
函数来异步加载游戏场景,经过查询AsyncOperation.progress
的值来获得场景加载的进度。html
第一步当加载完Loading场景后,调用以下的LoadGame
函数开始加载游戏场景,使用异步加载的方式加载场景1(Loading场景为0,主场景为1),经过Unity提供的Coroutine机制,咱们能够方便的在每一帧结束后调用SetLoadingPercentage
函数来更新界面中显示的进度条的数值。网络
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(); } }
最后进度条的效果显示以下:异步
进度条并无连续的显示加载的进度,而是停顿一下切换一个数字,再停顿一下切换一个数子,最后在没有显示100%就状况下就切换到主场景了。究其缘由在于Application.LoadLevelAsync
并非真正的后台加载,它在每一帧加载一些游戏资源,并给出一个progress值,因此在加载的时候仍是会形成游戏卡顿,AsyncOperation.progress
的值也不够精确。当主场景加载完毕后Unity就自动切换场景,因此上述代码中的while循环体内的代码是不会被调用的,致使进度条不会显示100%。函数
为了让进度条能显示100%,取巧一点的办法是将AsyncOperation.progress
的值乘上2,这样当加载到50%的时候界面上就显示100%了。缺点是当界面上显示100%的时候,用户还要等待一段时间才会进入游戏。其实Unity提供了手动切换场景的方法,把AsyncOperation.allowSceneActivation
设为false
就能够禁止Unity加载完毕后自动切换场景,修改后的StartLoading_2
代码以下:动画
// this function is not work private IEnumerator StartLoading_2(int scene) { AsyncOperation op = Application.LoadLevelAsync(scene); op.allowSceneActivation = false; while(!op.isDone) { SetLoadingPercentage(op.progress * 100); yield return new WaitForEndOfFrame(); } op.allowSceneActivation = true; }
咱们首先将AsyncOperation.allowSceneActivation
设为false
,当加载完成后再设为true
。代码看上去没有错,可是执行的结果是进度条最后会一直停留在90%上,场景不会切换。经过打印log发现AsyncOperation.isDone
一直为false
,AsyncOperation.progress
的值增长到0.9后就保持不变了,也就是说场景永远不会被加载完毕。this
在这个帖子中找到了答案,原来把allowSceneActivation
设置为false
后,Unity就只会加载场景到90%,剩下的10%要等到allowSceneActivation
设置为true
后才加载,这不得不说是一个坑。因此代码改成以下。当AsyncOperation.progress
到达0.9后,就直接把进度条的数值更新为100%,而后设置AsyncOperation.allowSceneActivation
为ture
,让Unity继续加载未完成的场景。spa
private IEnumerator StartLoading_3(int scene) { AsyncOperation op = Application.LoadLevelAsync(scene); 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; }
最后的效果以下:code
上述的进度条虽然解决了100%显示的问题,但因为进度条的数值更新不是连续的,因此看上去不够天然和美观。为了看上去像是在连续加载,能够每一次更新进度条的时候插入过渡数值。这里我采用的策略是当得到AsyncOperation.progress
的值后,不当即更新进度条的数值,而是每一帧在原有的数值上加1,这样就会产生数字不停滚动的动画效果了,迅雷中显示下载进度就用了这个方法。htm
private IEnumerator StartLoading_4(int scene) { int displayProgress = 0; int toProgress = 0; AsyncOperation op = Application.LoadLevelAsync(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; }
displayProgress
用来记录要显示在进度条上的数值,最后进度条的动画以下:对象
对比第一种的进度条
若是在加载游戏主场景以前还须要解析数据表格,生成对象池,进行网络链接等操做,那么能够给这些操做赋予一个权值,利用这些权值就能够计算加载的进度了。若是你的场景加载速度很是快,那么可使用一个假的进度条,让玩家看上几秒钟的loading动画,而后再加载场景。总之进度条虽然小,但要作好也是不容易的。