unity如何实现进度条效果(异步加载)


一、 介绍

unity如何实现进度条加载效果,按任意键结束

在这里插入图片描述


二、 制作一个按钮,添加脚本

使用异步加载场景操作,按任意键结束
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.SceneManagement;

public class load : MonoBehaviour
{
    
    
    
    // 指定加载过程中需要显示的UI元素
    public GameObject loadScreen;
    public Slider slider;
    public Text text;

    // Start is called before the first frame update
    public void LoadNextLevel()
    {
    
    
        // 调用协程函数开始异步加载场景
        StartCoroutine(Loadlevel());
    }

    IEnumerator Loadlevel()
    {
    
    
        // 激活加载界面
        loadScreen.SetActive(true);
        // 异步加载下一个场景,并等待加载结束
        AsyncOperation operation = SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex + 1);
        // 不允许立即激活场景
        operation.allowSceneActivation = false;
        
        while (!operation.isDone)
        {
    
    
            // 在整个加载过程中更新进度条显示
            slider.value = operation.progress;
            text.text = operation.progress * 100 + "%";
            if (operation.progress >= 0.9f)
            {
    
    
                // 如果已经加载到了90%以上,则直接跳转下一个场景
                slider.value = 1;
                text.text = "Press Any Key to Continue";
                if (Input.anyKeyDown)
                {
    
    
                    operation.allowSceneActivation = true;
                }
            }
            yield return null;
        }
    }
}


三、 下载


https://wwez.lanzoul.com/iOakN0tsvgje


猜你喜欢

转载自blog.csdn.net/qq_20179331/article/details/130313340