【Unity】Unity实现异步加载场景并显示进度条

 两个场景  一个当前场景 一个为要加载的下一个场景

在第一个场景中做了一个加载UI用于显示

代码如下

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Loading : MonoBehaviour
{
    private Slider loadingSlider;
    private Text loadingText;
    private GameObject Loading_UI;
    private float loadingSpeed = 1;
    private float targetValue;
    private AsyncOperation operation;

    private void Awake()
    {
        DontDestroyOnLoad(this);
        Loading_UI = transform.Find("Loading").gameObject;
        loadingSlider = Loading_UI.transform.Find("Slider").GetComponent<Slider>();
        loadingText = Loading_UI.transform.Find("Text").GetComponent<Text>();
    }

    void Start()
    {
        loadingSlider.value = 0.0f;
        StartCoroutine(AsyncLoading());
    }

    IEnumerator AsyncLoading()
    {
        operation = SceneManager.LoadSceneAsync("Scense");
        Loading_UI.SetActive(true);
        operation.allowSceneActivation = false;
        yield return operation;
    }

    void Update()
    {
        targetValue = operation.progress;

        if (operation.progress >= 0.9f)
        {
            //operation.progress的值最大为0.9
            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)
        {
            Loading_UI.SetActive(false);
            //允许异步加载完毕后自动切换场景
            operation.allowSceneActivation = true;
        }
    }
}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Unity中,异步场景显示进度可以通过以下步骤实现: 1. 创建一个UI界面,用于显示进度。 2. 使用SceneManager.LoadSceneAsync()方法异步场景。 3. 在场景的同时,使用AsyncOperation.progress属性获取进度,并将其显示在UI界面上。 4. 在场景完成后,隐藏UI界面。 下面是一个示例代码: ```csharp using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Collections; public class SceneLoader : MonoBehaviour { public Slider progressBar; public Text progressText; void Start() { StartCoroutine(LoadSceneAsync()); } IEnumerator LoadSceneAsync() { AsyncOperation operation = SceneManager.LoadSceneAsync("MyScene"); while (!operation.isDone) { float progress = Mathf.Clamp01(operation.progress / 0.9f); progressBar.value = progress; progressText.text = "Loading..." + (int)(progress * 100) + "%"; yield return null; } progressBar.gameObject.SetActive(false); progressText.gameObject.SetActive(false); } } ``` 在这个示例中,我们使用了Slider和Text UI元素来显示进度。在Start()方法中启动了一个协程来异步场景。在协程中,我们使用AsyncOperation.progress属性获取进度,并将其转换为0到1之间的值。最后,我们使用SetActive()方法隐藏进度条和文本UI元素。 需要注意的是,AsyncOperation.progress属性的值在0到0.9之间,因为在此阶段Unity会在后台所有场景数据。只有在最后10%的时间内,Unity才会开始实际场景,此时AsyncOperation.progress的值将达到1。因此,在计算进度时,我们需要将AsyncOperation.progress除以0.9来获得0到1之间的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值