using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Loading : MonoBehaviour {
public Text loadText;
public Slider loadSlider;
private AsyncOperation _asyncOperation;
// Use this for initialization
void Start () {
StartCoroutine (StartLoad ("2"));
}
void Update()
{
}
IEnumerator StartLoad(string sceneName)
{
int currentProgress = 0;
//目标进度
int targetProgress = 0;
_asyncOperation = Application.LoadLevelAsync(sceneName);
//unity 加载90%
_asyncOperation.allowSceneActivation = false;
while (_asyncOperation.progress<0.9f)
{
targetProgress = (int) _asyncOperation.progress*100;
//平滑过渡
while (currentProgress<targetProgress)
{
++currentProgress;
yield return new WaitForEndOfFrame();
}
}
//自行加载剩余的10%
targetProgress = 100;
while (currentProgress < targetProgress)
{
++currentProgress;
loadText.text = string.Format("{0}{1}", currentProgress.ToString(), "%");
loadSlider.value = (float)currentProgress / 100;
yield return new WaitForEndOfFrame();
}
_asyncOperation.allowSceneActivation = true;
}
}