Unity3d制作Loading场景进度条

本文介绍了一个使用Unity进行游戏开发时实现异步加载场景的例子。通过展示具体的C#代码,详细解释了如何利用StartCoroutine和LoadLevelAsync来平滑地加载游戏场景,并更新UI加载条进度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1.   
  2.     public void LoadGame()  
  3.     {  
  4.         StartCoroutine(StartLoading("001"));  
  5.     }  
  6.   
  7.     private IEnumerator StartLoading(string sceneName)  
  8.     {  
  9.         int displayProgress = 0;  
  10.         int toProgress = 0;  
  11.         AsyncOperation op = Application.LoadLevelAsync(sceneName);   //异步对象  
  12.         op.allowSceneActivation = false;  
  13.         while (op.progress < 0.9f)  
  14.         {  
  15.             toProgress = (int)op.progress * 100;  
  16.             while (displayProgress < toProgress)  
  17.             {  
  18.                 ++displayProgress;  
  19.                 SetLoadingPercentage(displayProgress);  
  20.                 yield return new WaitForEndOfFrame();  
  21.             }  
  22.         }  
  23.   
  24.         toProgress = 100;  
  25.         while (displayProgress < toProgress)  
  26.         {  
  27.             ++displayProgress;  
  28.             SetLoadingPercentage(displayProgress);  
  29.             yield return new WaitForEndOfFrame();  
  30.         }  
  31.         op.allowSceneActivation = false;  
  32.     }  
  33.   
  34.     private void SetLoadingPercentage(int DisplayProgress)     //设置显示进度  
  35.     {  
  36.         proLoadingBar.value = DisplayProgress * 0.01f;  
  37.         labProgress.text = DisplayProgress.ToString() + "%";  
  38.     }  
  39. }  
效果如下:(.gif图可能会有点卡,但是实际效果是完全很顺畅不会卡的。)


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值