Unity3D开发之场景加载条制作

    以前做过的场景切换加载条,加载还是比较均匀的不会出现突然加载完成的情况。

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

public class LoadScene : MonoBehaviour {

    [SerializeField]
    private Slider _slider;
    private AsyncOperation _async;
    private float toProgress = 0;
    private float displayProgress = 0;
    public static string _loadSceneName = "NEXTSCENE";

    // Use this for initialization
    void Start()
    {
        StartCoroutine(AsyncloadScene());
    }
    
    // Update is called once per frame
    void Update()
    {
        _slider.value = displayProgress;
    
    }
    IEnumerator AsyncloadScene()
    {
        _async = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(_loadSceneName);
        //不允许切换场景
        _async.allowSceneActivation = false;
        while (_async.progress < 0.9f|| displayProgress<0.9f)//即使场景加载很快完成 我们也要让进度条一点一点增加
        {
            toProgress = _async.progress;
            while (displayProgress < toProgress)
            {
                displayProgress += 0.01f;
                yield return new WaitForEndOfFrame();
            }
        }
        toProgress = 1;
        while (displayProgress < toProgress)
        {
            displayProgress += 0.01f;
            yield return new WaitForEndOfFrame();
        }
        //可以切换场景
        _async.allowSceneActivation = true;
    }
}
    

猜你喜欢

转载自blog.csdn.net/qq_33994566/article/details/80136766
今日推荐