Unity asynchronous jump scene 丨 excessive empty scene 丨 progress bar excessive scene

foreword

提示:大体介绍今日功能介绍
This article adds a transitional scene for the sake of the scene being too large to jump and freeze

make

Structure introduction

  • There are three scenarios in this article
  • A transition scene is added to the original scene and the scene after the jump, and the scene is divided into a version with a progress bar and a version without a progress bar.

excessive scene production

  • code direct production
  • First, we define a class with a fixed scene name as a spare, which will be used directly later
public class AppDefine
{
    
    
    public static string nextSceneName;
}
  • asynchronous part

progress bar version

  • This article chooses to directly choose to add a visual progress bar version
  • This scene is called AsyncLoadScene
 public Slider loadingSlider;  //加载场景进度条,B场景中使用UGUI实现

    public Text loadingText;  //显示加载进度 %

    private float loadingSpeed = 1;  //加载速度,这里是进度条的读取速度

    private float targetValue;  //进度条目标的值/异步加载进度的值
    private float operation = 3.0f;
    private AsyncOperation asyncLoad;  //定义异步加载的引用

    // Use this for initialization  
    void Start()
    {
    
    
        //loadingSlider.value = 0.0f;  //初始化进度条

        if (SceneManager.GetActiveScene().name == "AsyncLoadScene")  //如果当前活动场景是B
        {
    
    
            //启动协程  
            StartCoroutine(AsyncLoading());  //开启协程进行异步加载
        }
        
    }

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

    } // Update is called once per frame 

    void Update()
    {
    
    
        targetValue = asyncLoad.progress;
        if (asyncLoad.progress >= 0.9f)
        {
    
    
            //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)
            //如果当前进度条value和目标值接近 设置进度条value为目标值 
            {
    
     loadingSlider.value = targetValue; }
        }
        loadingText.text = ((int)(loadingSlider.value * 100)).ToString() + "%"; if ((int)(loadingSlider.value * 100) == 100)
        //当进度条读取到百分之百时允许场景切换 
        {
    
     //允许异步加载完毕后自动切换场景 
            asyncLoad.allowSceneActivation = true;
        }
    }

watch version

  • This version can make the asynchronous scene into a relatively good-looking scene, or show the logo of our scene and other scenes
  • The scene is not recommended to be too large
 private float loadingSpeed = 1;  //加载速度,这里是进度条的读取速度

    private float targetValue;  //进度条目标的值/异步加载进度的值
    private float operation = 3.0f;
    private AsyncOperation asyncLoad;  //定义异步加载的引用

    // Use this for initialization  
    void Start()
    {
    
    
        //loadingSlider.value = 0.0f;  //初始化进度条

        if (SceneManager.GetActiveScene().name == "AsyncLoadScene")  //如果当前活动场景是B
        {
    
    
            //启动协程  
            StartCoroutine(AsyncLoading());  //开启协程进行异步加载
        }

    }

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

    } // Update is called once per frame 

    void Update()
    {
    
    
        targetValue = asyncLoad.progress;
        if (asyncLoad.progress >= 0.9f)
        {
    
    
            //progress的值最大为0.9 
            targetValue = 1.0f;
        }

        asyncLoad.allowSceneActivation = true;

    }

Instructions

  • very easy to use
  • We directly first control the scene modification that the next scene in the separate class AppDefine we created needs to jump
  • Then follow the jump we normally use
  • Because our asynchronous scene is not very large, it will jump very fast
  public void LoadNewScene()
    {
    
    
        //保存需要加载的目标场景  
        AppDefine.nextSceneName = "原本需要跳转的相对较大的场景";

        SceneManager.LoadScene("AsyncLoadScene");
    }

result

Finally, we can use some medium to control us to start jumping, jump to our asynchronous transition scene, wait for time in the scene, wait for our Unity to finish loading the third scene, and then jump to the past.

Guess you like

Origin blog.csdn.net/weixin_42746271/article/details/129088702