using UnityEngine.SceneManagement; //使用场景管理空间
AsyncOperation prog = SceneManager.LoadSceneAsync("场景"); //新的异步加载场景API,返回异步参数 AsyncOperation.progress // 获取进度(我测试了一下最大是0.9,可能要真正跳到场景才是1) AsyncOperation.allowSceneActivation // //如果加载完成,是否立刻进入场景
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Load : MonoBehaviour {
public Slider progressUI;
public Text progressValue;
private AsyncOperation prog;
void Start() {
prog = SceneManager.LoadSceneAsync("场景名"); //异步加载场景
StartCoroutine(LoadingScene());
}
//设置滑动条
private void setProgressValue(int value) {
progressUI.value = value;
progressValue.text = value + "%";
}
private IEnumerator LoadingScene()
{
prog.allowSceneActivation = false; //如果加载完成,也不进入场景
int toProgress = 0;
int showProgress = 0;
//测试了一下,进度最大就是0.9
while (prog.progress < 0.9f)
{
toProgress = (int)(prog.progress * 100);
while ( showProgress<toProgress)
{
showProgress++;
setProgressValue(showProgress);
}
yield return new WaitForEndOfFrame(); //等待一帧
}
//计算0.9---1 其实0.9就是加载好了,我估计真正进入到场景是1
toProgress = 100;
while (showProgress < toProgress )
{
showProgress++;
setProgressValue(showProgress);
yield return new WaitForEndOfFrame(); //等待一帧
}
prog.allowSceneActivation = true; //如果加载完成,可以进入场景
}
}