場景加載進度條的完美方案

轉自: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包的,比如登錄場景,

 

  1. if (nextSceneID == (int)GlobeEnum.SceneDefine.LOGIN)  
  2.     yield return StartCoroutine(LoadNormalScene(sceneTable.ResName));  
  3. else  
  4.     yield return StartCoroutine(LoadBundleScene(sceneTable.ResName));  

 

 

問題5:用yield return null代替Update()來處理每幀的進度界面更新.

用yield return null來處理更新,比如在Update函數裏面處理,代碼更簡潔,但是要注意一個問題就是while死循環的問題

每個while的地方必須要對應一個yield return null,

 

經過以上處理,進度條看起來完美了很多,終於能滿足我這個完美主義者的要求了. (o_o)

 

代碼如下:

 

  1. IEnumerator LoadNormalScene(string sceneName, float startPercent = 0)  
  2. {  
  3.     GameRoot.Instance.CurrentSceneId = (int)LoadingWindow.nextSceneID;  
  4.   
  5.     loadingText.text = "加載場景中...";  
  6.   
  7.     int startProgress = (int)(startPercent * 100);  
  8.     int displayProgress = startProgress;  
  9.     int toProgress = startProgress;  
  10.     AsyncOperation op = Application.LoadLevelAsync(sceneName);  
  11.     op.allowSceneActivation = false;  
  12.     while (op.progress < 0.9f)  
  13.     {  
  14.         toProgress = startProgress + (int)(op.progress * (1.0f - startPercent) * 100);  
  15.         while (displayProgress < toProgress)  
  16.         {  
  17.             ++displayProgress;  
  18.             SetProgress(displayProgress);  
  19.             yield return null;  
  20.         }  
  21.         yield return null;  
  22.     }  
  23.   
  24.     toProgress = 100;  
  25.     while (displayProgress < toProgress)  
  26.     {  
  27.         ++displayProgress;  
  28.         SetProgress(displayProgress);  
  29.         yield return null;  
  30.     }  
  31.     op.allowSceneActivation = true;  
  32. }  
  33.   
  34. IEnumerator LoadBundleScene(string sceneName)  
  35. {  
  36.     string path = BundleManager.GetBundleLoadPath(BundleManager.PathSceneData, sceneName + ".data");  
  37.     WWW www = new WWW(path);  
  38.   
  39.     loadingText.text = "加載資源包中...";  
  40.     int displayProgress = 0;  
  41.     int toProgress = 0;  
  42.     while (!www.isDone)  
  43.     {  
  44.         toProgress = (int)(www.progress * m_BundlePercent * 100);  
  45.         while (displayProgress < toProgress)  
  46.         {  
  47.             ++displayProgress;  
  48.             SetProgress(displayProgress);  
  49.             yield return null;  
  50.         }  
  51.         yield return null;  
  52.     }  
  53.   
  54.     toProgress = (int)(m_BundlePercent * 100);  
  55.     while (displayProgress < toProgress)  
  56.     {  
  57.         ++displayProgress;  
  58.         SetProgress(displayProgress);  
  59.         yield return null;  
  60.     }  
  61.   
  62.     yield return www;  
  63.     if (null != www.assetBundle)  
  64.     {  
  65.         m_LastSceneBundle = www.assetBundle;  
  66.         yield return StartCoroutine(LoadNormalScene(sceneName, m_BundlePercent));  
  67.     }  
  68. }  
  69.   
  70. void SetProgress(int progress)  
  71. {  
  72.     loadingBar.value = progress * 0.01f;  
  73.     loadingProgress.text = progress.ToString() + " %";  
  74. }  


LoadNoramlScene表示加載沒有打包的場景

 

LoadBundleScene表示加載打包的場景

m_BundlePercent表示加載bundle包占總進度的百分比,默認0.7f


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章