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

实现方式

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

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

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

 

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

 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值