unity 异步跳转进度

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

public class Loading : MonoBehaviour {

    AsyncOperation async;
    int progress = 0;
    public Image fill;


    void Start () {
        
        StartCoroutine(loadScene());
    }

    public Text txt_test;
    void Update()
    {

        //progress = (int)(async.progress * 100);
        //txt_test.text = "当前进度:" + progress;


    }

    //void OnGUI()
    //{
    //    if (GUILayout.Button("跳转厂房", GUILayout.Width(100)))
    //    {
    //        //SceneManager.LoadSceneAsync("Loading");
    //        Global.loadSceneName = "ELE";
    //        //isOn = true;
    //        StartCoroutine(loadScene());
    //    }
    //}

     IEnumerator LoadScene()
    {
        float toProgress = 0.9f;//为了解决progress值只到0.9的问题,90%以后显示该值
        yield return new WaitForEndOfFrame();
        AsyncOperation asyncOper = SceneManager.LoadSceneAsync("DH");//跳转的场景
        asyncOper.allowSceneActivation = false;//是否允许场景激活,设为true则激活已加载的场景
        
        while (!asyncOper.isDone)
        {
            txt_test.text = Mathf.FloorToInt(asyncOper.progress * 100) + "%";
            fill.fillAmount = asyncOper.progress;
            if (asyncOper.progress >= 0.9f)
            {
                toProgress+=0.001f;
                //防止显示超过99%
                if (toProgress >= 0.99)
                    toProgress = 0.99f;

                txt_test.text = Mathf.FloorToInt(toProgress * 100) + "%";
                fill.fillAmount = toProgress;

                //显示到99%时跳转场景,这里跳转有延时,视场景而定是否需要,或改变比值
                //if (toProgress >= 0.99)
                asyncOper.allowSceneActivation = true;
                
            }
            yield return new WaitForEndOfFrame();
        }

       // if (asyncOper.isDone){
           //场景加载完毕
       // }

    }

}

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