• 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 }

    四、运行:

       

  • 相关阅读:
    2015/11/2用Python写游戏,pygame入门(2):游戏中的事件和显示
    2015/11/1用Python写游戏,pygame入门(1):pygame的安装
    2015/10/13 算法习题:最大子列和问题
    2015/10/9 Python核编初级部分学习总结
    2015/10/9 Python基础(21):可调用和可执行对象
    2015/9/29 Python基础(20):类的授权
    2015/9/28 Python基础(19):类的定制和私有性
    2015/9/22 Python基础(18):组合、派生和继承
    2015/9/21 Python基础(17):绑定和方法调用
    MVC 依赖注入
  • 原文地址:https://www.cnblogs.com/unknown6248/p/11701377.html
Copyright © 2020-2023  润新知