ProssBar加载动画完美事例(仅供自己学习,大家不嫌弃可以拿去用)

如图,做加载ProgressBar

UI组件如下:


代码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;


class LoadingPanel:MonoBehaviour
{


    private UIProgressBar PB;
    private UILabel txtContent;


    void Awake()
    {
        PB = Util.GetObjComponent<UIProgressBar>(this.transform,"ProgressBar");
        txtContent = PB.GetComponentInChildren<UILabel>();


        if (PB == null)
            Logger.Log_Error("LoadingPanel ProgreessBar is null!!!");
        if (txtContent == null)
            Logger.Log_Error("LoadingPanel txtContent is null!!!");


        PB.value = 0;
    }


    void Start()
    {
        LoadGame();
    }


    public void LoadGame()
    {
        //协同
        StartCoroutine(StartLoading("Lobby"));
    }


    private IEnumerator StartLoading(String sceneName)
    {
        //当前展示的进度
        int displayProgress = 0;
        //实际变化的进度
        int toProgress = 0;
        
        AsyncOperation op = Application.LoadLevelAsync(sceneName);
        //设置场景手动切换
        op.allowSceneActivation = false;
        //手动切换progress只能加载90%,对应的处理
        while (op.progress < 0.9f) 
        {
            //获得场景加载的进度
            toProgress = (int)op.progress * 100;
            //当展示的进度小于后台加载的进度时,对展示的数值进行改变
            while (displayProgress < toProgress)
            {
                ++displayProgress;
                //百分比数值的跟进
                txtContent.text = displayProgress + "%";
                //progressBar的跟进
                PB.value = displayProgress*0.01f;
                yield return new WaitForEndOfFrame();
            }
        }
        //对剩余10%做处理
        toProgress = 100;
        while (displayProgress < toProgress)
        {
            ++displayProgress;
            txtContent.text = displayProgress + "%";
            PB.value = displayProgress * 0.01f;
            yield return new WaitForEndOfFrame();
        }
        //开启场景
        op.allowSceneActivation = true;   


    }
}

将脚本挂到Panel下,运行即可!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值