Guest User

Untitled

a guest
Jan 12th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | Gaming | 0 0
  1. namespace Incineration.Singletons
  2. {
  3.     using UnityEngine;
  4.     using Incineration.Utilities;
  5.     public static class PersistentObjects
  6.     {
  7.         public static void Initialize(GameObject prefab)
  8.         {
  9.             GameObject root = new GameObject();
  10.             root.SetActive(false);
  11.             var instance = Object.Instantiate(prefab, root.transform);
  12.             foreach (var component in instance.GetComponentsInChildren<IPersistentObject>(includeInactive:true))
  13.             {
  14.                 component.Initialize();
  15.             }
  16.             Object.DontDestroyOnLoad(root);
  17.             instance.transform.DetachChildren();
  18.             Object.Destroy(root);
  19.         }
  20.     }
  21.  
  22.     internal static class PersistentObjectInititializer
  23.     {
  24.         private static GameObject GetPrefab()
  25.         {
  26.            
  27.             return Resources.Load<GameObject>("Managers");
  28.            
  29.         }
  30.  
  31.         [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  32.         private static void OnSubsystemRegistration()
  33.         {
  34.             GameObject mangersPrefab = GetPrefab();
  35.             if (mangersPrefab == null)
  36.             {
  37.                 InciDebug.LogError("Managers Prefab not found in Resources folder skipping initialization");
  38.             }
  39.             else
  40.             {
  41.                 PersistentObjects.Initialize(GetPrefab());
  42.             }
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment