unity www加载进度条和场景异步加载进度条

www加载进度条实现:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class TestLoadingProcess : MonoBehaviour {

    public Text showProcess;
    public Scrollbar bar;
    void Start()
    {
        StartCoroutine(getWWW());
    }

    IEnumerator getWWW()
    {
        WWW www = new WWW(@"file:///E:/AssetBundleProject/01.mp4");
        string progress;
        while (!www.isDone)
        {
            progress = (((int)(www.progress * 100)) % 100) + "%";
            bar.value = www.progress;
            showProcess.text = progress;
            //Debug.Log(progress);
            yield return 1;
        }
        if (www.error != null)
        {
            Debug.Log("loading error:" + www.url);
        }
        else
        {
            progress = "100%";
            SceneManager.LoadScene("GSS_4");
            //Debug.Log(progress);
            //enter complete code
        }
    }
}

异步加载场景进度条01:

private IEnumerator StartLoading_4()
    {
        int displayProgress = 0;
        int toProgress = 0;
        AsyncOperation op = Application.LoadLevelAsync(1);
        op.allowSceneActivation = false;
        while (op.progress < 0.9f)
        {
            toProgress = (int)op.progress * 100;
            while (displayProgress < toProgress)
            {
                ++displayProgress;
                loadingText.text = displayProgress.ToString() + "%";
                loadingSlider.value = displayProgress * 0.01f;
               //SetLoadingPercentage(displayProgress);
               yield return new WaitForEndOfFrame();
            }
        }

        toProgress = 100;
        while (displayProgress < toProgress)
        {
            ++displayProgress;
            loadingText.text = displayProgress.ToString() + "%";
            //Debug.Log(float.Parse((displayProgress/100).ToString()));
            //Debug.Log(displayProgress * 0.01);
            loadingSlider.value = displayProgress * 0.01f;
            //SetLoadingPercentage(displayProgress);
            yield return new WaitForEndOfFrame();
        }
        op.allowSceneActivation = true;
    }

异步加载场景进度条02:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class TestLoadingScene : MonoBehaviour {

    public Slider loadingSlider;
    public Text loadingText;
    private float loadingSpeed = 0.1f;
    private float targetValue;
    private AsyncOperation operation;  
    void Start()
    {       
           //启动协程  
            StartCoroutine(AsyncLoading());       
    }

    IEnumerator AsyncLoading()
    {
        operation = SceneManager.LoadSceneAsync("GSS_4");
        targetValue = operation.progress;
        //阻止当加载完成自动切换  
        //operation.allowSceneActivation = false;
        yield return operation;
        if (operation.progress >= 0.9f)
        {
            //operation.progress的值最大为0.9  
            targetValue = 1.0f;
        }

        if (targetValue != loadingSlider.value)
        {
            //插值运算  
            loadingSlider.value = Mathf.Lerp(loadingSlider.value, targetValue, Time.deltaTime * loadingSpeed);
            if (Mathf.Abs(loadingSlider.value - targetValue) < 0.01f)
            {
                loadingSlider.value = targetValue;
            }
        }

        loadingText.text = ((int)(loadingSlider.value * 100)).ToString() + "%";

        if ((int)(loadingSlider.value * 100) == 100)
        {
            //允许异步加载完毕后自动切换场景  
            operation.allowSceneActivation = true;
        }
    }    
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rain_love_snow

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值