Unity3d制作Loading场景进度条

通常游戏的主场景包含的资源较多,会导致加载场景的时间较长。为了避免这个问题,可以首先加载Loading场景,然后再通过Loading场景来加载主场景。因为Loading场景包含的资源较少,所以加载速度快。在加载主场景的时候一般会在Loading界面中显示一个进度条来告知玩家当前加载的进度。

在Unity中可以通过调用Application.LoadLevelAsync函数来异步加载游戏场景,通过查询AsyncOperation.progress的值来得到场景加载的进度。

Loading场景进度条在游戏中的应用是很广泛的。进度条是用NGUI做的。

实现代码如下:

using UnityEngine;
using System.Collections;

public class scene : MonoBehaviour
{
    public UISlider proLoadingBar;  //NGUI进度条
    public UILabel labProgress;     //NGUI label用于显示加载百分之多少

    //异步对象  
    void Start()
    {
        LoadGame();
    }

    public void LoadGame()
    {
        StartCoroutine(StartLoading("001"));
    }

    private IEnumerator StartLoading(string sceneName)
    {
        int displayProgress = 0;
        int toProgress = 0;
        AsyncOperation op = Application.LoadLevelAsync(sceneName);   //异步对象
        op.allowSceneActivation = false;
        while (op.progress < 0.9f)
        {
            toProgress = (int)op.progress * 100;
            while (displayProgress < toProgress)
            {
                ++displayProgress;
                SetLoadingPercentage(displayProgress);
                yield return new WaitForEndOfFrame();
            }
        }

        toProgress = 100;
        while (displayProgress < toProgress)
        {
            ++displayProgress;
            SetLoadingPercentage(displayProgress);
            yield return new WaitForEndOfFrame();
        }
        op.allowSceneActivation = false;
    }

    private void SetLoadingPercentage(int DisplayProgress)     //设置显示进度
    {
        proLoadingBar.value = DisplayProgress * 0.01f;
        labProgress.text = DisplayProgress.ToString() + "%";
    }
}
效果如下:(.gif图可能会有点卡,但是实际效果是完全很顺畅不会卡的。)

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值