Skip to content

Instantly share code, notes, and snippets.

@xahon
Created June 19, 2022 11:45
Show Gist options
  • Save xahon/bceeae625b9c69b38bd9b6dd57460d36 to your computer and use it in GitHub Desktop.
Save xahon/bceeae625b9c69b38bd9b6dd57460d36 to your computer and use it in GitHub Desktop.
Preloads all GameObjects from _preloader scene and makes them DDOL automatically
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