Unity 异步场景加载中的进度条是真的还是假的

以前也是用过异步场景加载,一直认为进度条加载的进度是假的,研究过之后发现进度条代表的加载进度,有的是真的有的是假的0.0。

方法一:进度条的加载进度是实际要跳转场景的加载进度(operation.progress)----进度条加载的进度是真实加载进度

真实加载进度的效果
在这里插入图片描述

在这里插入图片描述

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

public class LoadManger : MonoBehaviour 
{
	public GameObject LoadScreen;
	public Slider slider;
	public Text text;
	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)
			{
				slider.value = 1;

				text.text = "Press Any Key to Conture";

				if (Input.anyKeyDown)
				{
					operation.allowSceneActivation = true;
				}
			}
			yield return null;
		}
	}
}

方法二:进度条的加载进度是虚拟的,加载进度与要跳转场景的加载进度没有任何关系----进度条加载的进度是假的

假进度条的最终效果在这里插入图片描述

从SceneA向SceneB跳转

SceneA场景的设置及脚本挂载
SceneA场景的设置及脚本挂载

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

public class OnProgress : MonoBehaviour {

	void Start () {
		
	}

    public void LoadNewScene()
    {
        Debug.Log("clicked");
        Globe.nextSceneName = "SceneB";//目标场景名称
        SceneManager.LoadScene("Loading");//加载进度条场景
    }
}

进度条场景级脚本挂载
在这里插入图片描述

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

/// <summary>
/// 进度条加载
/// </summary>
public class Globe
{
    public static string nextSceneName;
}

public class AsyncLoadScene : MonoBehaviour
{
    public Text loadingText;
    public Image progressBar;

    private int curProgressValue = 0;

    private AsyncOperation operation;

    void Start()
    {
        if (SceneManager.GetActiveScene().name == "Loading")//判断当前激活场景的名字
        {
            //启动协程
            StartCoroutine("AsyncLoading");
        }
    }

    IEnumerator AsyncLoading()
    {
        operation = SceneManager.LoadSceneAsync(Globe.nextSceneName);
        //阻止当加载完成自动切换
        //异步场景加载到90%时,会自己停下来判断allowSceneActivation是否为true,如果为true,则继续加载剩余的10%,完全加载完成后跳转场景
        //如果为false,则停止加载,直到allowSceneActivation为true时才继续加载
        operation.allowSceneActivation = false;

        yield return operation;
    }

    void Update()
    {

        int progressValue = 100;

        if (curProgressValue < progressValue)
        {
            curProgressValue++;
        }

        loadingText.text = curProgressValue + "%";//实时更新进度百分比的文本显示  

        progressBar.fillAmount = curProgressValue / 100f;//实时更新滑动进度图片的fillAmount值  

        if (curProgressValue == 100)
        {
            operation.allowSceneActivation = true;//启用自动加载场景  
        }
    }
}

总结

如果进度条进度表示真实加载进度,会有两个极端,一个是加载过快(要跳转的场景很快就加载完),没有看到进度条的加载进度,一瞬间就加载完;另一种情况是加载过慢(要跳转的场景很大,加载很卡),进度条卡在某一个进度等很久,然后直接加载完。
如果进度条进度是假的,进度条的加载进度会很均匀,有条不紊的增加,这个进度条加载完的时间跟速度都是可人为控制的,可通过代码修改,我自己再用的时候,会先从SceneA直接跳转SceneB(同步加载),记录下直接跳转时的时间,然后再用异步加载时,把进度条加载完的时间跟同步加载的时间设置的相近,这样就不太会出现卡的现象。


版权声明:本文为qq_43511290原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。