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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章