场景加载进度条的完美方案
来源:互联网 发布:鹰眼数据 编辑:程序博客网 时间:2024/06/14 04:49
转自:http://www.cnblogs.com/czaoth/p/5785630.html
我以为做个进度条很简单,分分钟解决,结果折腾了一天才搞定,Unity有很多坑,要做完美需要逐一解决.
问题1:最简单的方法不能实现100%的进度
用最简单的方法来实现,不能实现100%的进度,原因是Unity加载完新场景立马就激活新场景了,无法显示最后的进度.解决办法就是使用allowSceneActivation来控制进入场景的时机.
问题2:使用allowSceneActivation后进度卡在90%
这个问题官网论坛也有人讨论,解决办法就是自己手动修补最后的10%,
问题3:进度条一顿一顿地增加.不平滑
解决办法手动插值平滑
问题4:www和LoadLevelAsync两部分进度的整合问题
大部分场景是打成Bundle包的,先要WWW加载,再LoadLevelAsync,两部分的进度要整合在一起,
少量场景是不打Bundle包的,比如登录场景,
- if (nextSceneID == (int)GlobeEnum.SceneDefine.LOGIN)
- yield return StartCoroutine(LoadNormalScene(sceneTable.ResName));
- else
- yield return StartCoroutine(LoadBundleScene(sceneTable.ResName));
问题5:用yield return null代替Update()来处理每帧的进度界面更新.
用yield return null来处理更新,比如在Update函数里面处理,代码更简洁,但是要注意一个问题就是while死循环的问题
每个while的地方必须要对应一个yield return null,
经过以上处理,进度条看起来完美了很多,终于能满足我这个完美主义者的要求了. (o_o)
代码如下:
- IEnumerator LoadNormalScene(string sceneName, float startPercent = 0)
- {
- GameRoot.Instance.CurrentSceneId = (int)LoadingWindow.nextSceneID;
- loadingText.text = "加载场景中...";
- int startProgress = (int)(startPercent * 100);
- int displayProgress = startProgress;
- int toProgress = startProgress;
- AsyncOperation op = Application.LoadLevelAsync(sceneName);
- op.allowSceneActivation = false;
- while (op.progress < 0.9f)
- {
- toProgress = startProgress + (int)(op.progress * (1.0f - startPercent) * 100);
- while (displayProgress < toProgress)
- {
- ++displayProgress;
- SetProgress(displayProgress);
- yield return null;
- }
- yield return null;
- }
- toProgress = 100;
- while (displayProgress < toProgress)
- {
- ++displayProgress;
- SetProgress(displayProgress);
- yield return null;
- }
- op.allowSceneActivation = true;
- }
- IEnumerator LoadBundleScene(string sceneName)
- {
- string path = BundleManager.GetBundleLoadPath(BundleManager.PathSceneData, sceneName + ".data");
- WWW www = new WWW(path);
- loadingText.text = "加载资源包中...";
- int displayProgress = 0;
- int toProgress = 0;
- while (!www.isDone)
- {
- toProgress = (int)(www.progress * m_BundlePercent * 100);
- while (displayProgress < toProgress)
- {
- ++displayProgress;
- SetProgress(displayProgress);
- yield return null;
- }
- yield return null;
- }
- toProgress = (int)(m_BundlePercent * 100);
- while (displayProgress < toProgress)
- {
- ++displayProgress;
- SetProgress(displayProgress);
- yield return null;
- }
- yield return www;
- if (null != www.assetBundle)
- {
- m_LastSceneBundle = www.assetBundle;
- yield return StartCoroutine(LoadNormalScene(sceneName, m_BundlePercent));
- }
- }
- void SetProgress(int progress)
- {
- loadingBar.value = progress * 0.01f;
- loadingProgress.text = progress.ToString() + " %";
- }
LoadNoramlScene表示加载没有打包的场景
LoadBundleScene表示加载打包的场景
m_BundlePercent表示加载bundle包占总进度的百分比,默认0.7f
- 场景加载进度条的完美方案
- Unity3D手游开发日记(3) - 场景加载进度条的完美方案
- 场景加载进度条优化方案
- 【Unity】场景异步加载的进度条制作
- 【Unity】场景异步加载的进度条制作
- 【Unity】场景异步加载进度条的制作
- 很完美的jquery网页加载进度条
- Unity3d场景加载进度条
- Unity3d_UGUI加载场景进度条
- 场景进度条加载
- 场景加载进度条
- 场景切换时进度条加载
- UNITY之进度条加载场景
- 加载场景进度条平滑问题
- Unity场景加载与进度条
- Unity异步加载场景与加载进度条
- <Unity3D专区>Unity实现加载场景进度条
- (转)Unity实现加载场景进度条
- [leetcode 169. Majority Element]easy |week4
- 64位LIBSVM在Matlab下的安装使用
- 各种坑......
- 1.2引入jQuery的方法
- <操作系统:精髓与设计原理> 操作系统概述
- 场景加载进度条的完美方案
- java对象在set属性时为什么不更新数据库
- Animation动画介绍和实现
- 团体程序设计天梯赛-练习集 L3-002. 堆栈 线段树 单点更新 解题报告
- C++中随机函数rand()和srand()的用法
- Unity Assets目录下的文件夹用途
- effective java 碰到的问题
- 逻辑回归为什么使用对数损失函数
- Hash(篇1)实现一个函数来查找具有相同唯一字符集的所有单词