Unity場景加載進度條

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

public class LoadingBarScript : MonoBehaviour
{
    public Text loadingText;
    public Slider sliderBar;
    private AsyncOperation async;
    private int curProgressVaule = 0;
    
    void Start()
    {
        StartCoroutine(LoadScene());
    }    

    IEnumerator LoadScene()
    {
        async = SceneManager.LoadSceneAsync("SampleScene");
        async.allowSceneActivation = false;
        yield return async;
    }
    
    void Update()
    {
        if (async == null)
        {
            return;
        }

        int progressVaule = 0;

        if (async.progress < 0.9f)
        {
            progressVaule = (int)async.progress * 100;
        }
        else
        {
            progressVaule = 100;
        }

        if (curProgressVaule < progressVaule)
        {
            curProgressVaule++;
        }
        sliderBar.value = curProgressVaule / 100.0f;
        loadingText.text = curProgressVaule + "%";
       

        if (curProgressVaule == 100)
        {
            async.allowSceneActivation = true;
        }
    }
}

 

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