using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LoadP : MonoBehaviour {
//float timer = 0f;
UISlider slider;//进度条
int nowProgress, toProgress;//当前进度、异步加载进度
AsyncOperation ao;//异步加载对象
void Start () {
slider = transform .GetChild(1).GetChild (0).GetComponent <UISlider >();//获取UI界面进度条
StartCoroutine(LoadScene ());//开启异步加载协程
}
void Update () {
//timer += Time.deltaTime;
//slider.value = timer / 10;
// async.progress 表示正在读取的场景的进度值 0---0.9
// 如果当前的进度小于0.9,说明它还没有加载完成,就说明进度条还需要移动
// 如果,场景的数据加载完毕,async.progress 的值就会等于0.9
if (ao.progress <0.9f)
{
toProgress = (int)(ao.progress *100);
}
else
{
toProgress = 100;
}
if (nowProgress <toProgress )
{
nowProgress++;//当前进度累加 注:在UI界面只能看到当前进度
}
slider.value = nowProgress / 100f;
if (nowProgress ==100)//当当前进度达到100,跳转场景
{
ao.allowSceneActivation = true;
}
}
IEnumerator LoadScene()
{
ao = SceneManager.LoadSceneAsync(2);//异步加载完成,跳转到场景2
ao.allowSceneActivation = false;//阻止加载完自动跳转场景
yield return ao;
}
}
Unity:游戏场景异步加载
最新推荐文章于 2024-07-04 23:22:27 发布