天天看點

遊戲開發中進度條的常用實作方式

實作方式

  常見的進度條實作方式分為兩種,

  一:當需要加載資源非常少的時候,我們可以使用同步加載的方式,當同步加載完場景之後,播放一個進度條的動畫,當然這種"假"的進度條現實的并非是實際場景的加載進度,隻是一個加載的動畫效果,但為什麼還要使用這種不是進度條的方式呢?如果播放實時加載進度,資源很小的話瞬間就完成了,也就看不到什麼進度條效果了,這對使用者體驗是十分差的。

  二:第二種進度條實作方式是異步加載場景,進度條實時顯示加載進度,這種方式比較常用,适用于大型場景的加載。

  上代碼,首先是第二種加載方式。

1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using UnityEngine;
 5 using UnityEngine.UI;
 6 using UnityEngine.SceneManagement;
 7 public class LoadGame : MonoBehaviour {
 8 
 9     public Slider processView;
10 
11     // Use this for initialization
12     void Start () {
13         LoadGameMethod();
14         
15     }
16     
17     // Update is called once per frame
18     void Update () {
19         
20 
21     }
22     public void LoadGameMethod()
23     {
24         StartCoroutine(StartLoading_4(2));
25     }
26 
27     private IEnumerator StartLoading_4(int scene)
28     {
29         int displayProgress = 0;    //目前顯示進度
30         int toProgress = 0;       //想要加載的進度
31         AsyncOperation op = SceneManager.LoadSceneAsync(scene); //異步加載
32         op.allowSceneActivation = false;  //加載完成不顯示
33         while (op.progress < 0.9f)      //當加載進度小于數值時循環增加 
34         {
35             toProgress = (int)op.progress * 100;
36             while (displayProgress < toProgress)
37             {
38                 ++displayProgress;
39                 SetLoadingPercentage(displayProgress);
40                 yield return new WaitForEndOfFrame();
41             }
42         }
43 
44         toProgress = 100;
45         while (displayProgress < toProgress)
46         {
47             ++displayProgress;
48             SetLoadingPercentage(displayProgress);
49             yield return new WaitForEndOfFrame();    //等待所有UI渲染及錄影機完成繼續執行
50         }
51         op.allowSceneActivation = true;    //顯示新場景
52     }
53 
54     private void SetLoadingPercentage(float v)
55     {
56         processView.value = v / 100;    //進度條顯示
57     }
58 
59    
60 }      

轉載于:https://www.cnblogs.com/Freedom-1024/p/9961989.html

ui