場景載入進度條的完美方案
阿新 • • 發佈:2019-02-20
轉自: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