為loadmanager添加腳本,代碼如下 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
/// <summary>
///
/// </summary>
public class LoadManager : MonoBehaviour
{
public GameObject loadScreen;
public Slider slider;
public Text text;
public void LoadNextLevel()
{
StartCoroutine(Loadlevel());
}
IEnumerator Loadlevel()
{
loadScreen.SetActive(true);
AsyncOperation operation = SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex + 1);
operation.allowSceneActivation = false;//控制不自動跳轉到加載好的場景,progress值停在0.9
while (!operation.isDone)
{
slider.value = operation.progress;//将加載進度傳輸給Slider的滑動條
text.text = operation.progress * 100 + "%";//文本顯示百分比
if (operation.progress >= 0.9f)//判斷是否場景已經在背景準備好
{
slider.value = 1;//強行調整到100%的進度條
text.text = "Press AnyKey to continue";
if (Input.anyKeyDown)
{
operation.allowSceneActivation = true;//完成最後10%的工作,顯示加載好的場景
}
}
yield return null;
}
}
}