【Unity笔记】使用协程(Coroutine)异步加载场景

2022-11-13,,,,

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using System; public class LoadingPage : MonoBehaviour { public UISlider progressBar; // 目标进度
float target = ;
// 读取场景的进度,取值范围0~1
float progress = ;
// 异步对象
AsyncOperation op = null; void Start () {
Debug.Log("开始LoadScene"); op = SceneManager.LoadSceneAsync("GamePlayScene");
op.allowSceneActivation = false;
progressBar.value = ; // 开启协程,开始调用加载方法
StartCoroutine(processLoading());
} float dtimer = ;
void Update()
{
progressBar.value = Mathf.Lerp(progressBar.value, target, dtimer * 0.02f);
dtimer += Time.deltaTime;
if (progressBar.value > 0.99f)
{
progressBar.value = ;
op.allowSceneActivation = true;
}
} // 加载进度
IEnumerator processLoading()
{
while (true)
{
target = op.progress; // 进度条取值范围0~1
if (target >= 0.9f)
{
target = ;
yield break;
}
yield return ;
}
} }

【Unity笔记】使用协程(Coroutine)异步加载场景的相关教程结束。

《【Unity笔记】使用协程(Coroutine)异步加载场景.doc》

下载本文的Word格式文档,以方便收藏与打印。