Unity场景加载进度条

你。 提交于 2020-02-27 02:46:44

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

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!