异步加载场景并显示进度条

A场景是当前场景,B场景是异步加载的过渡场景,C场景是要前往的目标场景

在A场景的任意一个脚本中添加一个方法,并在要用的按钮出传入数值即可

 void LoadNewScene(string nextSceneName, string goSceneName)
    {
        //保存需要加载的目标场景  
        AppDefine.nextSceneName = nextSceneName;
        //前往中转场景
        SceneManager.LoadScene(goSceneName);

    }

在B场景中新建脚本

public class WaitScene : MonoBehaviour
{
     Slider loadingSlider;

     Text loadingText;

    private float loadingSpeed = 1;

    private float targetValue;

    private AsyncOperation operation;


    private void Awake()
    {
        loadingSlider = transform.Find("Slider").GetComponent<Slider>();
        loadingText = transform.Find("Text1").GetComponent<Text>();
    }
    void Start()
    {
        loadingSlider.value = 0.0f;

        if (SceneManager.GetActiveScene().name == "Waiting")
        {
            //启动协程
            StartCoroutine(AsyncLoading());
        }
    }

    IEnumerator AsyncLoading()
    {
        operation = SceneManager.LoadSceneAsync(AppDefine.nextSceneName);
        //阻止当加载完成自动切换
        operation.allowSceneActivation = false;

        yield return operation;
    }

    // Update is called once per frame
    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)
        {
            //允许异步加载完毕后自动切换场景
            operation.allowSceneActivation = true;
        }
    }
}

场景名称建一个脚本,把它在其中当作静态变量,需要的时候改写即可

public class AppDefine 
{
    public static string nextSceneName;
    
}


这样就可以满足需求啦,如下图

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在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之间的值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值