前言
使用unity API协程实现场景加载进度条。
1 代码
// 使用单例模式
public class LoadManager : MonoSingleton<LoadManager>
{
[SerializeField] private Canvas mLoadCanvas;
[SerializeField] private Image mLoadSlider;
public void LoadScene(string sceneName)
{
mLoadCanvas.gameObject.SetActive(true);
StartCoroutine(LoadLevel(sceneName));
}
IEnumerator LoadLevel(string sceneName)
{
AsyncOperation operation = SceneManager.LoadSceneAsync(sceneName);
// 加载完成后直接激活进入场景
operation.allowSceneActivation = true;
while (!operation.isDone)
{
mLoadSlider.fillAmount = operation.progress;
yield return null;
}
mLoadCanvas.gameObject.SetActive(false);
}
}