Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Incineration.Singletons
- {
- using UnityEngine;
- using Incineration.Utilities;
- public static class PersistentObjects
- {
- public static void Initialize(GameObject prefab)
- {
- GameObject root = new GameObject();
- root.SetActive(false);
- var instance = Object.Instantiate(prefab, root.transform);
- foreach (var component in instance.GetComponentsInChildren<IPersistentObject>(includeInactive:true))
- {
- component.Initialize();
- }
- Object.DontDestroyOnLoad(root);
- instance.transform.DetachChildren();
- Object.Destroy(root);
- }
- }
- internal static class PersistentObjectInititializer
- {
- private static GameObject GetPrefab()
- {
- return Resources.Load<GameObject>("Managers");
- }
- [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
- private static void OnSubsystemRegistration()
- {
- GameObject mangersPrefab = GetPrefab();
- if (mangersPrefab == null)
- {
- InciDebug.LogError("Managers Prefab not found in Resources folder skipping initialization");
- }
- else
- {
- PersistentObjects.Initialize(GetPrefab());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment