How to switch scene with dynamic resource loading and initializing,How to switch scene beautifully with dynamic resource loading

I want to init some data and load some resources when the scene begins, but it will block the game if i put them all in start() or awake(), so I choose to make a loading progress bar and do my initialization asynchronously. But I think it is not a beautiful way to build the loading progress inside the scene, cause in that way i have to do that in every scene i want to use. I want to use a special scene for loading. Is there any way i can do this? Is there any beautiful way to realize it? thanks

To achieve this, you can use Unity’s SceneManager and AsyncOperation functionalities. Here’s one to go about it:

Create a new scene for loading:

In your Unity project, create a new scene and name it “LoadingScene”. In this scene, you can design a UI with a progress bar to show the loading progress.

Create a script for managing the loading process:

In the LoadingScene, create a new script, for example, “LoadingManager.cs”. This script will handle the asynchronous loading of the target scene and updating the progress bar.

Example:

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

public class LoadingManager : MonoBehaviour {
    public Slider progressBar;

    private void Start() {
        LoadSceneAsync("YourTargetSceneName");
    }

    public void LoadSceneAsync(string sceneName) {
        StartCoroutine(LoadSceneAsyncCoroutine(sceneName));
    }

    private IEnumerator LoadSceneAsyncCoroutine(string sceneName) {
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneName);
        asyncOperation.allowSceneActivation = false;

        while (!asyncOperation.isDone) {
            progressBar.value = Mathf.Clamp01(asyncOperation.progress / 0.9f);

            if (asyncOperation.progress >= 0.9f && progressBar.value >= 1f) {
                asyncOperation.allowSceneActivation = true;
            }

            yield return null;
        }
    }
}

Remember to replace “YourTargetSceneName” with the name of the scene you want to load. Attach this script to an empty GameObject in the LoadingScene, and link the Slider UI component to the progressBar field in the Inspector.

Create a script to switch to the loading scene:

In the scene where you want to initiate the loading process, create a new script, for example, “SceneSwitcher.cs”. This script will be responsible for switching to the LoadingScene.

Example:

using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneSwitcher : MonoBehaviour {
    public void SwitchToLoadingScene() {
        SceneManager.LoadScene("LoadingScene");
    }
}

Attach this script to a relevant GameObject, such as a button, in the scene where you want to initiate the loading process. Call the SwitchToLoadingScene() method when you want to switch to the loading scene, for example, when a button is clicked.

This approach will separate the loading process from your main game scenes, making it more modular and easier to manage. You can reuse the LoadingScene and its associated scripts for all your scene transitions. Just remember to update the target scene name in the LoadSceneAsync() method of the LoadingManager.cs script as needed.