public class LevelLoader : MonoBehaviour {
public GameObject loadingPanle;
public Slider slider;
public Text progressText;
public void LoadLevel(int levelIndex)
{
StartCoroutine(LoadAsynchronously(levelIndex));
}
IEnumerator LoadAsynchronously(int sceneIndex)
{
AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex);
loadingPanle.SetActive(true);
while (!operation.isDone)
{
float progress = Mathf.Clamp01(operation.progress / .9f);
slider.value = progress;
progressText.text = progress * 100 + "%";
yield return null;
}
}
}

本文介绍了一个Unity脚本,该脚本通过异步操作来加载游戏场景,并且在加载过程中显示进度条和当前进度百分比。具体实现包括定义一个公共类LevelLoader,继承自MonoBehaviour,其中包含用于显示加载面板的预制件、进度条滑块和进度文本等元素。LoadLevel方法接受一个整数参数作为要加载的场景索引,并启动协程LoadAsynchronously进行异步加载。
1516

被折叠的 条评论
为什么被折叠?



