以前做過的場景切換加載條,加載還是比較均勻的不會出現突然加載完成的情況。
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;
}
}