zoukankan      html  css  js  c++  java
  • unity 之 场景切换进度条显示

    一、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 }

    四、运行:

       

  • 相关阅读:
    bzoj 1697: [Usaco2007 Feb]Cow Sorting牛排序【置换群】
    【20】AngularJS 参考手册
    【19】AngularJS 应用
    【18】AngularJS 包含
    【17】AngularJS Bootstrap
    【16】AngularJS API
    【15】AngularJS 输入验证
    【14】AngularJS 表单
    【13】AngularJS 模块
    【12】AngularJS 事件
  • 原文地址:https://www.cnblogs.com/unknown6248/p/11701377.html
Copyright © 2011-2022 走看看