天天看点

游戏开发中进度条的常用实现方式

实现方式

  常见的进度条实现方式分为两种,

  一:当需要加载资源非常少的时候,我们可以使用同步加载的方式,当同步加载完场景之后,播放一个进度条的动画,当然这种"假"的进度条现实的并非是实际场景的加载进度,只是一个加载的动画效果,但为什么还要使用这种不是进度条的方式呢?如果播放实时加载进度,资源很小的话瞬间就完成了,也就看不到什么进度条效果了,这对用户体验是十分差的。

  二:第二种进度条实现方式是异步加载场景,进度条实时显示加载进度,这种方式比较常用,适用于大型场景的加载。

  上代码,首先是第二种加载方式。

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