Created
June 19, 2022 11:45
-
-
Save xahon/bceeae625b9c69b38bd9b6dd57460d36 to your computer and use it in GitHub Desktop.
Preloads all GameObjects from _preloader scene and makes them DDOL automatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
public class ScenePreloader : MonoBehaviour | |
{ | |
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] | |
static void OnBeforeSceneLoad() | |
{ | |
LoadPreloaderScene(); | |
} | |
void Start() | |
{ | |
GameObject[] rootObjects = SceneManager.GetSceneByName("_preloader").GetRootGameObjects(); | |
foreach (GameObject rootObject in rootObjects) | |
DontDestroyOnLoad(rootObject); | |
Destroy(gameObject); | |
SceneManager.UnloadSceneAsync("_preloader"); | |
} | |
static void LoadPreloaderScene() | |
{ | |
for (int i = 0; i < SceneManager.sceneCount; i++) | |
{ | |
Scene scene = SceneManager.GetSceneAt(i); | |
if (scene.name == "_preloader") | |
return; | |
} | |
SceneManager.LoadScene("_preloader", LoadSceneMode.Additive); | |
GameObject go = new GameObject("_PRELOADER"); | |
go.AddComponent<ScenePreloader>(); | |
SceneManager.MoveGameObjectToScene(go, SceneManager.GetSceneByName("_preloader")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment