using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LoadingScene : MonoBehaviour {
public Slider processBar;
private AsyncOperation async;
private int nowProcess;
void Start()
{
StartCoroutine(loadScene());
}
/// <summary>
/// 加載完場景后就會跳轉(zhuǎn)
/// </summary>
/// <returns></returns>
IEnumerator loadScene()
{
async = SceneManager.LoadSceneAsync("sceneName");
async.allowSceneActivation = false;
yield return async;
}
void Update()
{
if(async == null)
{
return;
}
int toProcess;
// async.progress 你正在讀取的場景的進度值 0---0.9
// 如果當前的進度小于0.9敢艰,說明它還沒有加載完成蛛蒙,就說明進度條還需要移動
// 如果,場景的數(shù)據(jù)加載完畢权悟,async.progress 的值就會等于0.9
if(async.progress < 0.9f)
{
toProcess = (int)async.progress * 100;
}
else
{
toProcess = 100;
}
// 如果滑動條的當前進度缰揪,小于陨享,當前加載場景的方法返回的進度
if(nowProcess < toProcess)
{
nowProcess++;
}
processBar.value = nowProcess / 100f;
// 設(shè)置為true的時候,如果場景數(shù)據(jù)加載完畢钝腺,就可以自動跳轉(zhuǎn)場景
if(nowProcess == 100)
{
async.allowSceneActivation = true;
}
}
}
來自http://blog.csdn.net/qq_28221881/article/details/53674445#comments