↓不带进度条的异步加载
/// <summary>
/// 异步加载
/// </summary>
/// <returns></returns>
public static IEnumerator LoadAsyncScene(string sceneName, Action action = null)
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);
while (!asyncLoad.isDone)
{
yield return null;
}
if (action != null)
{
action();
}
}
↓协程更新进度条, WebGL用这个会报错 (PS:现在回头看看, 当时写的代码实在太蠢了…)
/// <summary>
/// 异步加载带进度条
/// </summary>
/// <returns></returns>
public static IEnumerator LoadAsyncScene(string sceneName, Action action = null)
{
//进度
int nowProgress = 0;
int toProgress = 0;
//进度条
Slider slider = GameObject.Find("Canvas").transform.Find("Bg/Slider").GetComponent<Slider>();
//进度文字
Text loadText = GameObject.Find("Canvas").transform.Find("Bg/Text").GetComponent<Text>();
//图片: loading
Transform image = GameObject.Find("Canvas").transform.Find("Bg/Image");
//初始化
slider.value = 0;
loadText.text = "0%";
slider.gameObject.SetActive(true);
loadText.gameObject.SetActive(true);
image.gameObject.SetActive(true);
//加载场景
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);
//先不激活场景, 这时候Unity加载到90%就自己停
asyncLoad.allowSceneActivation = false;
//90%之前按加载进步跑
while (asyncLoad.progress < 0.9f)
{
toProgress = (int)asyncLoad.progress * 100;
while (nowProgress < toProgress)
{
++nowProgress;
loadText.text = nowProgress + "%";
slider.value = nowProgress / 100f;
yield return new WaitForEndOfFrame();
}
}
//跑到100%
toProgress = 100;
while (nowProgress < toProgress)
{
++nowProgress;
loadText.text = nowProgress + "%";
slider.value = nowProgress / 100f;
yield return new WaitForEndOfFrame();
}
//激活场景, Unity加载到100%
asyncLoad.allowSceneActivation = true;
//UI隐藏掉
slider.gameObject.SetActive(false);
loadText.gameObject.SetActive(false);
image.gameObject.SetActive(false);
//等一帧再加载方法, 不然可能方法执行不了而报空
yield return null;
if (action != null)
{
action();
}
}
↓在Update里更新进度条, WebGL能用
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LoadUpdate : MonoBehaviour
{
public static LoadUpdate instance;
//需要挂上的
[Header("加载页面")]
public GameObject panelLoad;
[Header("加载的进度条")]
public Slider loadSlider;
[Header("加载进度的显示文字")]
public Text loadText;
private int loadVaule;
private int currentVaule;
private AsyncOperation asyncOperation;
private void Awake()
{
if (instance == null )
{
instance = this;
}
else if (instance !=this)
{
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
}
/// <summary>
/// 调用加载
/// </summary>
public void LoadScene(string sceneName, Action action = null)
{
StartCoroutine(LoadSceneAsync(sceneName, action));
}
/// <summary>
/// 异步加载场景,加载完成后执行Action
/// </summary>
private IEnumerator LoadSceneAsync(string sceneName, Action action = null)
{
if (panelLoad != null)
{
panelLoad.SetActive(true);
}
asyncOperation = SceneManager.LoadSceneAsync(sceneName);
asyncOperation.allowSceneActivation = false;
yield return asyncOperation;
if (panelLoad != null)
{
panelLoad.SetActive(false);
}
if (action != null)
{
action.Invoke();
}
}
/// <summary>
/// 因为更新进度条的方法放进协程里打包WebGl会卡住,所以放进Updata里
/// </summary>
private void Update()
{
if (asyncOperation == null)
{
return;
}
//在90%之前正常加载
if (asyncOperation.progress < 0.9f)
{
loadVaule = (int)asyncOperation.progress * 100;
}
//到90%的时候就设为100%
else
{
loadVaule = 100;
}
//更新进度
if (currentVaule < loadVaule)
{
currentVaule++;
}
//加载完了
if (currentVaule >= 100)
{
asyncOperation.allowSceneActivation = true;
}
//更新Slider和Text
if (loadText != null)
{
loadText.text = currentVaule + "%";
}
if (loadSlider != null)
{
loadSlider.value = currentVaule / 100f;
}
}
}