Unity功能 進度條加載跳轉場景

 

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LoadGame : MonoBehaviour {

    public Slider processView;//進度條

	void Start () {
        LoadGameMethod();        
	}	
	
    public void LoadGameMethod()
    {
        StartCoroutine(StartLoading_4(2));//開啓加載遊戲場景協程
    }

    private IEnumerator StartLoading_4(int scene)
    {
        int displayProgress = 0;//當前進度值
        int toProgress = 0;//當前進條想要達到的值
        AsyncOperation op = SceneManager.LoadSceneAsync(scene); //異步加載
        op.allowSceneActivation = false;//場景加載完後禁止切換
        while (op.progress < 0.9f)
        {
            toProgress = (int)op.progress * 100;
            while (displayProgress < toProgress)
            {
                ++displayProgress;
                SetLoadingPercentage(displayProgress);
                yield return new WaitForEndOfFrame();//等待當前幀
            }
        }

        toProgress = 100;
        //從90%追到100%
        while (displayProgress < toProgress)
        {
            ++displayProgress;
            SetLoadingPercentage(displayProgress);
            yield return new WaitForEndOfFrame();
        }
        op.allowSceneActivation = true;
    }
    //設置進度條
    private void SetLoadingPercentage(float v)
    {
        processView.value = v / 100;
    }

   
}

 

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