- 创建两个场景:现在的场景“NowScene”,要加载的场景“LoadScene”;
- “NowScene”如图所示,“LoadScene”任意;
- 创建脚本“AsyncLoadScene”,复制如下代码,挂在到Canvas上;
- 推拽"Slider"和"Text"到面板上;
- 注意将要加载的场景添加到《Scenes In Build》,否则加载时回报空引用。
![]()
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class AsyncLoadScene : MonoBehaviour
{
// 进度条
public Slider loadingSlider;
// 文字显示加载进度
public Text loadingText;
// 进度条的行进速度
private float SliderLoadSpeed = 1;
// 场景加载类的对象
private AsyncOperation operation;
// 加载进度(由于加载进度不能为 1,所以需要此变量在加载进度大于某一个值时让加载进度变为1)
private float targetValue;
void Start ()
{
// 初始化进度条
loadingSlider.value = 0.0f;
if (SceneManager.GetActiveScene ().name == "NowSence") {
// 启动协程
StartCoroutine (AsyncLoading ());
}
}
void Update ()
{
// operation.progress 加载进度
targetValue = operation.progress;
if (operation.progress >= 0.9f) {
// operation.progress的值最大为0.9
targetValue = 1.0f;
}
if (targetValue != loadingSlider.value) {
// 插值运算(进度条向当前加载进度趋近)
loadingSlider.value = Mathf.Lerp (loadingSlider.value, targetValue, Time.deltaTime * SliderLoadSpeed);
// 避免插值运算一直进行
if (Mathf.Abs (loadingSlider.value - targetValue) < 0.01f) {
loadingSlider.value = targetValue;
}
}
// 显示加载百分比
loadingText.text = ((int)(loadingSlider.value * 100)).ToString () + "%";
// 当进度条完成 100% 时,加载场景
if ((int)(loadingSlider.value * 100) == 100) {
//允许异步加载完毕后自动切换场景
operation.allowSceneActivation = true;
}
}
// 加载场景的协程
IEnumerator AsyncLoading ()
{
// 异步加载场景
operation = SceneManager.LoadSceneAsync ("LoadSence");
// 阻止当加载完成自动切换
operation.allowSceneActivation = false;
yield return operation;
}
}