using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Loading : MonoBehaviour
{
public Slider processBar;
private AsyncOperation async;
private int nowProcess;
public Text _text;
void Start()
{
StartCoroutine(loadScene());
}
/// <summary>
/// 加載完場景後就會跳轉
/// </summary>
/// <returns></returns>
IEnumerator loadScene()
{
async = SceneManager.LoadSceneAsync("123");
async.allowSceneActivation = false;
yield return new WaitForEndOfFrame();
}
void Update()
{
if (async == null)
{
return;
}
int toProcess;
// async.progress 你正在讀取的場景的進度值 0---0.9
// 如果當前的進度小於0.9,說明它還沒有加載完成,就說明進度條還需要移動
// 如果,場景的數據加載完畢,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;
_text.text = nowProcess.ToString() + "%";
// 設置爲true的時候,如果場景數據加載完畢,就可以自動跳轉場景
if (nowProcess == 100)
{
async.allowSceneActivation = true;
}
}
}
個人學習收藏,不做他用