unity中实现场景之间加载进度条
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LoadingScene : MonoBehaviour
{
public Image progressImg;
private AsyncOperation async;
public Text text;
private int curProgressVaule = 0;//计数器
// Use this for initialization
void Start ()
{
StartCoroutine(LoadScene());
}
IEnumerator LoadScene()
{
async = SceneManager.LoadSceneAsync("game");//异步跳转到game场景
async.allowSceneActivation = false;//当game场景加载到90%时,不让它直接跳转到game场景。
yield return async;
}
// Update is called once per frame
void Update () {
if (async == null)
{
return;
}
int progressVaule = 0;
if (async.progress < 0.9f)
{
progressVaule = (int) async.progress*100;
}
else
{
progressVaule = 100;
}
if (curProgressVaule < progressVaule)
{
curProgressVaule++;
}
text.text = curProgressVaule + "%";
progressImg.fillAmount = curProgressVaule/100f;
if (curProgressVaule == 100)
{
async.allowSceneActivation = true;
}
}
}