背景
Application.LoadLevelAsync
函数来异步加载游戏场景,通过查询AsyncOperation.progress
的值来得到场景加载的进度。
遇到问题
LoadGame
函数开始加载游戏场景,使用异步加载的方式加载场景1(Loading场景为0,主场景为1),通过Unity提供的Coroutine机制,我们可以方便的在每一帧结束后调用SetLoadingPercentage
函数来更新界面中显示的进度条的数值。
<code class="csharp" style="word-wrap: break-word;"><span class="k" style="word-wrap: break-word;">public</span> <span class="k" style="word-wrap: break-word;">void</span> <span class="nf" style="word-wrap: break-word;">LoadGame</span><span class="p" style="word-wrap: break-word;">()</span> <span class="p" style="word-wrap: break-word;">{</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">StartCoroutine</span><span class="p" style="word-wrap: break-word;">(</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">StartLoading_1</span><span class="p" style="word-wrap: break-word;">(</span><span class="m" style="word-wrap: break-word;">1</span><span class="p" style="word-wrap: break-word;">));</span>
<span class="p" style="word-wrap: break-word;">}</span>
<span class="k" style="word-wrap: break-word;">private</span> <span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">IEnumerator</span> <span class="nf" style="word-wrap: break-word;">StartLoading_1</span><span class="p" style="word-wrap: break-word;">(</span><span class="kt" style="word-wrap: break-word;">int</span> <span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">scene</span><span class="p" style="word-wrap: break-word;">)</span> <span class="p" style="word-wrap: break-word;">{</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">AsyncOperation</span> <span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span> <span class="p" style="word-wrap: break-word;">=</span> <span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">Application</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">LoadLevelAsync</span><span class="p" style="word-wrap: break-word;">(</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">scene</span><span class="p" style="word-wrap: break-word;">);</span>
<span class="k" style="word-wrap: break-word;">while</span><span class="p" style="word-wrap: break-word;">(!</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">isDone</span><span class="p" style="word-wrap: break-word;">)</span> <span class="p" style="word-wrap: break-word;">{</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">SetLoadingPercentage</span><span class="p" style="word-wrap: break-word;">(</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">progress</span> <span class="p" style="word-wrap: break-word;">*</span> <span class="m" style="word-wrap: break-word;">100</span><span class="p" style="word-wrap: break-word;">);</span>
<span class="k" style="word-wrap: break-word;">yield</span> <span class="k" style="word-wrap: break-word;">return</span> <span class="k" style="word-wrap: break-word;">new</span> <span class="nf" style="word-wrap: break-word;">WaitForEndOfFrame</span><span class="p" style="word-wrap: break-word;">();</span>
<span class="p" style="word-wrap: break-word;">}</span>
<span class="p" style="word-wrap: break-word;">}</span>
</code>
最后进度条的效果显示如下:
Application.LoadLevelAsync
并不是真正的后台加载,它在每一帧加载一些游戏资源,并给出一个progress值,所以在加载的时候还是会造成游戏卡顿,AsyncOperation.progress
的值也不够精确。当主场景加载完毕后Unity就自动切换场景,所以上述代码中的while循环体内的代码是不会被调用的,导致进度条不会显示100%。
修补——100%完成
AsyncOperation.progress
的值乘上2,这样当加载到50%的时候界面上就显示100%了。缺点是当界面上显示100%的时候,用户还要等待一段时间才会进入游戏。其实Unity提供了手动切换场景的方法,把AsyncOperation.allowSceneActivation
设为false
就可以禁止Unity加载完毕后自动切换场景,修改后的StartLoading_2
代码如下:
<code class="csharp" style="word-wrap: break-word;"><span class="c1" style="word-wrap: break-word;">// this function is not work</span>
<span class="k" style="word-wrap: break-word;">private</span> <span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">IEnumerator</span> <span class="nf" style="word-wrap: break-word;">StartLoading_2</span><span class="p" style="word-wrap: break-word;">(int scene)</span> <span class="p" style="word-wrap: break-word;">{</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">AsyncOperation</span> <span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span> <span class="p" style="word-wrap: break-word;">=</span> <span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">Application</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">LoadLevelAsync</span><span class="p" style="word-wrap: break-word;">(scene</span><span class="p" style="word-wrap: break-word;">);</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">allowSceneActivation</span> <span class="p" style="word-wrap: break-word;">=</span> <span class="k" style="word-wrap: break-word;">false</span><span class="p" style="word-wrap: break-word;">;</span>
<span class="k" style="word-wrap: break-word;">while</span><span class="p" style="word-wrap: break-word;">(!</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">isDone</span><span class="p" style="word-wrap: break-word;">)</span> <span class="p" style="word-wrap: break-word;">{</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">SetLoadingPercentage</span><span class="p" style="word-wrap: break-word;">(</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">progress</span> <span class="p" style="word-wrap: break-word;">*</span> <span class="m" style="word-wrap: break-word;">100</span><span class="p" style="word-wrap: break-word;">);</span>
<span class="k" style="word-wrap: break-word;">yield</span> <span class="k" style="word-wrap: break-word;">return</span> <span class="k" style="word-wrap: break-word;">new</span> <span class="nf" style="word-wrap: break-word;">WaitForEndOfFrame</span><span class="p" style="word-wrap: break-word;">();</span>
<span class="p" style="word-wrap: break-word;">}</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">allowSceneActivation</span> <span class="p" style="word-wrap: break-word;">=</span> <span class="k" style="word-wrap: break-word;">true</span><span class="p" style="word-wrap: break-word;">;</span>
<span class="p" style="word-wrap: break-word;">}</span>
</code>
AsyncOperation.allowSceneActivation
设为false
,当加载完成后再设为true
。代码看上去没有错,但是执行的结果是进度条最后会一直停留在90%上,场景不会切换。通过打印log发现AsyncOperation.isDone
一直为false
,AsyncOperation.progress
的值增加到0.9后就保持不变了,也就是说场景永远不会被加载完毕。
allowSceneActivation
设置为false
后,Unity就只会加载场景到90%,剩下的10%要等到allowSceneActivation
设置为true
后才加载,这不得不说是一个坑。所以代码改为如下。当AsyncOperation.progress
到达0.9后,就直接把进度条的数值更新为100%,然后设置AsyncOperation.allowSceneActivation
为ture
,让Unity继续加载未完成的场景。
<code class="csharp" style="word-wrap: break-word;"><span class="k" style="word-wrap: break-word;">private</span> <span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">IEnumerator</span> <span class="nf" style="word-wrap: break-word;">StartLoading_3</span><span class="p" style="word-wrap: break-word;">(int scene)</span> <span class="p" style="word-wrap: break-word;">{</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">AsyncOperation</span> <span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span> <span class="p" style="word-wrap: break-word;">=</span> <span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">Application</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">LoadLevelAsync</span><span class="p" style="word-wrap: break-word;">(scene</span><span class="p" style="word-wrap: break-word;">);</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">allowSceneActivation</span> <span class="p" style="word-wrap: break-word;">=</span> <span class="k" style="word-wrap: break-word;">false</span><span class="p" style="word-wrap: break-word;">;</span>
<span class="k" style="word-wrap: break-word;">while</span><span class="p" style="word-wrap: break-word;">(</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">progress</span> <span class="p" style="word-wrap: break-word;"><</span> <span class="m" style="word-wrap: break-word;">0.9f</span><span class="p" style="word-wrap: break-word;">)</span> <span class="p" style="word-wrap: break-word;">{</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">SetLoadingPercentage</span><span class="p" style="word-wrap: break-word;">(</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">progress</span> <span class="p" style="word-wrap: break-word;">*</span> <span class="m" style="word-wrap: break-word;">100</span><span class="p" style="word-wrap: break-word;">);</span>
<span class="k" style="word-wrap: break-word;">yield</span> <span class="k" style="word-wrap: break-word;">return</span> <span class="k" style="word-wrap: break-word;">new</span> <span class="nf" style="word-wrap: break-word;">WaitForEndOfFrame</span><span class="p" style="word-wrap: break-word;">();</span>
<span class="p" style="word-wrap: break-word;">}</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">SetLoadingPercentage</span><span class="p" style="word-wrap: break-word;">(</span><span class="m" style="word-wrap: break-word;">100</span><span class="p" style="word-wrap: break-word;">);</span>
<span class="k" style="word-wrap: break-word;">yield</span> <span class="k" style="word-wrap: break-word;">return</span> <span class="k" style="word-wrap: break-word;">new</span> <span class="nf" style="word-wrap: break-word;">WaitForEndOfFrame</span><span class="p" style="word-wrap: break-word;">();</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">allowSceneActivation</span> <span class="p" style="word-wrap: break-word;">=</span> <span class="k" style="word-wrap: break-word;">true</span><span class="p" style="word-wrap: break-word;">;</span>
<span class="p" style="word-wrap: break-word;">}</span>
</code>
最后的效果如下:
打磨——增加动画
AsyncOperation.progress
的值后,不立即更新进度条的数值,而是每一帧在原有的数值上加1,这样就会产生数字不停滚动的动画效果了,迅雷中显示下载进度就用了这个方法。
<code class="csharp" style="word-wrap: break-word;"><span class="k" style="word-wrap: break-word;">private</span> <span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">IEnumerator</span> <span class="nf" style="word-wrap: break-word;">StartLoading_4</span><span class="p" style="word-wrap: break-word;">(int scene)</span> <span class="p" style="word-wrap: break-word;">{</span>
<span class="kt" style="word-wrap: break-word;">int</span> <span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">displayProgress</span> <span class="p" style="word-wrap: break-word;">=</span> <span class="m" style="word-wrap: break-word;">0</span><span class="p" style="word-wrap: break-word;">;</span>
<span class="kt" style="word-wrap: break-word;">int</span> <span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">toProgress</span> <span class="p" style="word-wrap: break-word;">=</span> <span class="m" style="word-wrap: break-word;">0</span><span class="p" style="word-wrap: break-word;">;</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">AsyncOperation</span> <span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span> <span class="p" style="word-wrap: break-word;">=</span> <span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">Application</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">LoadLevelAsync</span><span class="p" style="word-wrap: break-word;">(scene</span><span class="p" style="word-wrap: break-word;">);</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">allowSceneActivation</span> <span class="p" style="word-wrap: break-word;">=</span> <span class="k" style="word-wrap: break-word;">false</span><span class="p" style="word-wrap: break-word;">;</span>
<span class="k" style="word-wrap: break-word;">while</span><span class="p" style="word-wrap: break-word;">(</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">progress</span> <span class="p" style="word-wrap: break-word;"><</span> <span class="m" style="word-wrap: break-word;">0.9f</span><span class="p" style="word-wrap: break-word;">)</span> <span class="p" style="word-wrap: break-word;">{</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">toProgress</span> <span class="p" style="word-wrap: break-word;">=</span> <span class="p" style="word-wrap: break-word;">(</span><span class="kt" style="word-wrap: break-word;">int</span><span class="p" style="word-wrap: break-word;">)</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">progress</span> <span class="p" style="word-wrap: break-word;">*</span> <span class="m" style="word-wrap: break-word;">100</span><span class="p" style="word-wrap: break-word;">;</span>
<span class="k" style="word-wrap: break-word;">while</span><span class="p" style="word-wrap: break-word;">(</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">displayProgress</span> <span class="p" style="word-wrap: break-word;"><</span> <span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">toProgress</span><span class="p" style="word-wrap: break-word;">)</span> <span class="p" style="word-wrap: break-word;">{</span>
<span class="p" style="word-wrap: break-word;">++</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">displayProgress</span><span class="p" style="word-wrap: break-word;">;</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">SetLoadingPercentage</span><span class="p" style="word-wrap: break-word;">(</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">displayProgress</span><span class="p" style="word-wrap: break-word;">);</span>
<span class="k" style="word-wrap: break-word;">yield</span> <span class="k" style="word-wrap: break-word;">return</span> <span class="k" style="word-wrap: break-word;">new</span> <span class="nf" style="word-wrap: break-word;">WaitForEndOfFrame</span><span class="p" style="word-wrap: break-word;">();</span>
<span class="p" style="word-wrap: break-word;">}</span>
<span class="p" style="word-wrap: break-word;">}</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">toProgress</span> <span class="p" style="word-wrap: break-word;">=</span> <span class="m" style="word-wrap: break-word;">100</span><span class="p" style="word-wrap: break-word;">;</span>
<span class="k" style="word-wrap: break-word;">while</span><span class="p" style="word-wrap: break-word;">(</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">displayProgress</span> <span class="p" style="word-wrap: break-word;"><</span> <span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">toProgress</span><span class="p" style="word-wrap: break-word;">){</span>
<span class="p" style="word-wrap: break-word;">++</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">displayProgress</span><span class="p" style="word-wrap: break-word;">;</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">SetLoadingPercentage</span><span class="p" style="word-wrap: break-word;">(</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">displayProgress</span><span class="p" style="word-wrap: break-word;">);</span>
<span class="k" style="word-wrap: break-word;">yield</span> <span class="k" style="word-wrap: break-word;">return</span> <span class="k" style="word-wrap: break-word;">new</span> <span class="nf" style="word-wrap: break-word;">WaitForEndOfFrame</span><span class="p" style="word-wrap: break-word;">();</span>
<span class="p" style="word-wrap: break-word;">}</span>
<span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">op</span><span class="p" style="word-wrap: break-word;">.</span><span class="n" style="word-wrap: break-word; padding-top: 15px; border: none; background-image: none !important; background-color: transparent !important; background-position: initial initial !important; background-repeat: initial initial !important;">allowSceneActivation</span> <span class="p" style="word-wrap: break-word;">=</span> <span class="k" style="word-wrap: break-word;">true</span><span class="p" style="word-wrap: break-word;">;</span>
<span class="p" style="word-wrap: break-word;">}</span>
</code>
displayProgress
用来记录要显示在进度条上的数值,最后进度条的动画如下:
对比第一种的进度条
总结
如果在加载游戏主场景之前还需要解析数据表格,生成对象池,进行网络连接等操作,那么可以给这些操作赋予一个权值,利用这些权值就可以计算加载的进度了。如果你的场景加载速度非常快,那么可以使用一个假的进度条,让玩家看上几秒钟的loading动画,然后再加载场景。总之进度条虽然小,但要做好也是不容易的。