Unity异步加载场景(有进度条,百分比)

Olinda ·
更新时间:2024-09-20
· 980 次阅读

using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement; public class AsyncLoadScene : MonoBehaviour { public Slider loadingSlider; public Text loadingText; private float loadingSpeed = 1; private float targetValue; private AsyncOperation operation; // Use this for initialization void Start() { loadingSlider.value = 0.0f; if (SceneManager.GetActiveScene().name == "HallLoading") { //启动协程 StartCoroutine(AsyncLoading()); } } IEnumerator AsyncLoading() { operation = SceneManager.LoadSceneAsync(Globe.nextSceneName); //阻止当加载完成自动切换 operation.allowSceneActivation = false; yield return operation; } private void Update() { targetValue = operation.progress; if (operation.progress >= 0.9f) { targetValue = 1.0f; } if (targetValue != loadingSlider.value) { //插值运算 loadingSlider.value = Mathf.Lerp(loadingSlider.value, targetValue, Time.deltaTime * loadingSpeed); if (Mathf.Abs(loadingSlider.value - targetValue) < 0.01f) { loadingSlider.value = targetValue; } } loadingText.text = ((int)(loadingSlider.value * 100)).ToString() + "%"; if ((int)(loadingSlider.value * 100) == 100) { //允许异步加载完毕后自动切换场景 operation.allowSceneActivation = true; } } }
作者:ShenHaoDeHao



进度条 百分比 unity

需要 登录 后方可回复, 如果你还没有账号请 注册新账号