Asynchronous scene loading details

Asynchronous scene loading details

introduce

Asynchronous scene loading is a way of loading scenes in Unity that allows other operations to be performed while the loading is in progress and provides feedback on the progress of the loading. Through asynchronous loading, you can avoid the stuttering phenomenon when loading large scenes, and improve the fluency of the game and the user experience.

method

In Unity, there is SceneManager.LoadSceneAsynca method for asynchronous loading. This method has two parameters:

  • sceneName: The scene name or index to load.
  • loadSceneMode: Specifies the mode of loading the scene, such as loading alone or appending to the current scene.

After loading the scene asynchronously, you can AsyncOperationget the loading progress through the class and perform corresponding operations.

for example

Here are a few common code examples showing how to load the current scene using asynchronous loading:

Example 1: Load the scene asynchronously and display the loading progress bar

using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneLoader : MonoBehaviour
{
    
    
    public Slider progressBar; // 进度条UI

    void Start()
    {
    
    
        StartCoroutine(LoadSceneAsync());
    }

    IEnumerator LoadSceneAsync()
    {
    
    
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex);

        while (!asyncOperation.isDone)
        {
    
    
            float progress = Mathf.Clamp01(asyncOperation.progress / 0.9f); // 获取加载进度(范围从0到1)
            progressBar.value = progress; // 更新进度条UI

            yield return null;
        }
    }
}

Example 2: Load a scene asynchronously and perform other operations after loading is complete

using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneLoader : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        StartCoroutine(LoadSceneAsync());
    }

    IEnumerator LoadSceneAsync()
    {
    
    
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex);

        while (!asyncOperation.isDone)
        {
    
    
            yield return null;
        }

        // 加载完成后执行其他操作
        Debug.Log("Scene loaded successfully!");
    }
}

Example 3: Load a scene asynchronously and jump to a new scene after loading is complete

using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneLoader : MonoBehaviour
{
    
    
    public string nextSceneName; // 要跳转的下一个场景名称

    void Start()
    {
    
    
        StartCoroutine(LoadSceneAsync());
    }

    IEnumerator LoadSceneAsync()
    {
    
    
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(nextSceneName);

        while (!asyncOperation.isDone)
        {
    
    
            yield return null;
        }

        // 加载完成后跳转到新场景
        SceneManager.LoadScene(nextSceneName);
    }
}

These examples show different uses, and you can modify and extend them according to your needs. Using asynchronous scene loading can improve game performance and perform other operations during the loading process, enhancing the user experience.

Hope to help you! If you have further questions, please feel free to ask.

Guess you like

Origin blog.csdn.net/qq_20179331/article/details/132230301