首页 > 编程语言 > 详细

unity 之 场景切换进度条显示

时间:2019-10-19 01:27:07      阅读:62      评论:0      收藏:0      [点我收藏+]

一、UI建立建立slider适当更改即可;

技术分享图片

 

技术分享图片

 

 

二、新增loadScene脚本,用来进行场景切换,将其绑定任意物体上面。博主以放置主相机为例。参数分别为进度条(用来设置value值),显示进度文本text

 技术分享图片

在设置中加入两个场景:

 技术分享图片

三、脚本;

 1 /// <summary>
 2 /// 场景切换
 3 /// 在unity 获取当前加载进度progress中,其中最多到0.9.只有等到加载到第二个场景才会到1
 4 /// 所有在加载进度条时如果progress的值近似0.9,则直接将进度参数设置为1,实现进度到100%
 5 /// 并且progress的值是在一帧加载一些资源,所以其值不会是连续的,因此设置两个参数来记录当前
 6 /// 进度和页面显示的进度,进行++。
 7 /// </summary>
 8 public class loadScene : MonoBehaviour
 9 {
10     AsyncOperation async;
11     public Slider slider;
12     public Text text;//百分制显示进度加载情况
13 
14     void Start()
15     {
16         //开启协程
17         StartCoroutine("loginMy");
18     }
19     
20     void Update()
21     {
22        
23     }
24     IEnumerator loginMy()
25     {
26         int displayProgress = 0;
27         int toProgress = 0;
28         AsyncOperation op = SceneManager.LoadSceneAsync(1);
29         op.allowSceneActivation = false;
30         while (op.progress < 0.9f)  //此处如果是 <= 0.9f 则会出现死循环所以必须小0.9
31         {
32             toProgress = (int)op.progress * 100;
33             while (displayProgress < toProgress)
34             {
35                 ++displayProgress;
36                 SetLoadingPercentage(displayProgress);
37                 yield return new WaitForEndOfFrame();//ui渲染完成之后
38             }
39         }
40         toProgress = 100;
41         while (displayProgress < toProgress)
42         {
43             ++displayProgress;
44             SetLoadingPercentage(displayProgress);
45             yield return new WaitForEndOfFrame();
46         }
47         op.allowSceneActivation = true;
48 
49     }
50 
51     private void SetLoadingPercentage(int displayProgress)
52     {
53         slider.value = displayProgress;
54         text.text = displayProgress.ToString() + "%";
55     }
56 }

四、运行:

   技术分享图片

 

unity 之 场景切换进度条显示

原文:https://www.cnblogs.com/unknown6248/p/11701377.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!