异步加载场景并且显示进度条

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

namespace GameScene
{
    public class LoadingSync : MonoBehaviour
    {
        private static string NextSceneName = "";
       
        public static void EnterScene(string name)
        {
            NextSceneName = name;
            SceneManager.LoadScene("LoadingSync");
        }

        //异步对象
        public Slider LoadingSlider;
        public Text LoadingText;
        private int displayProgress = 0;
        // Use this for initialization
        void Awake()
        {

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

        IEnumerator LoadScene()
        {
            Debug.Log("EnterScene:" + NextSceneName);
            LoadingSlider.value = 0;
            LoadingText.text = "0%";
            yield return new WaitForEndOfFrame();
            AsyncOperation op = SceneManager.LoadSceneAsync(NextSceneName);
            op.allowSceneActivation = false;
            while (op.progress < 0.9f) {
                displayProgress = (int)(op.progress * 100);
                while (LoadingSlider.value < displayProgress) {
                    LoadingSlider.value++;
                    LoadingText.text = ((int)(LoadingSlider.value)).ToString()+"%";
                    yield return null;
                }
                yield return null;
            }
            displayProgress = 100;
            while (LoadingSlider.value < displayProgress) {
                LoadingSlider.value++;
                LoadingText.text = ((int)(LoadingSlider.value)).ToString() + "%";
                yield return null;
            }
            LoadingText.text = "100%";
            op.allowSceneActivation = true;
        }
    }
}

  等到进度条显示完啦场景在加载出来

 

posted @ 2018-03-13 17:32  智能化的代码  阅读(233)  评论(0编辑  收藏  举报