using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LoadGame : MonoBehaviour {
public Slider processView;//進度條
void Start () {
LoadGameMethod();
}
public void LoadGameMethod()
{
StartCoroutine(StartLoading_4(2));//開啓加載遊戲場景協程
}
private IEnumerator StartLoading_4(int scene)
{
int displayProgress = 0;//當前進度值
int toProgress = 0;//當前進條想要達到的值
AsyncOperation op = SceneManager.LoadSceneAsync(scene); //異步加載
op.allowSceneActivation = false;//場景加載完後禁止切換
while (op.progress < 0.9f)
{
toProgress = (int)op.progress * 100;
while (displayProgress < toProgress)
{
++displayProgress;
SetLoadingPercentage(displayProgress);
yield return new WaitForEndOfFrame();//等待當前幀
}
}
toProgress = 100;
//從90%追到100%
while (displayProgress < toProgress)
{
++displayProgress;
SetLoadingPercentage(displayProgress);
yield return new WaitForEndOfFrame();
}
op.allowSceneActivation = true;
}
//設置進度條
private void SetLoadingPercentage(float v)
{
processView.value = v / 100;
}
}