Advertisement
apieceoffruit

MagicBootstrap

Feb 21st, 2025
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.71 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System;
  4. using System.Linq;
  5. using Object = UnityEngine.Object;
  6.  
  7. public class MagicBootstrap : MonoBehaviour,App
  8. {
  9.     static HideFlags VISIBILITY = HideFlags.HideInHierarchy;
  10.    
  11.     static MagicBootstrap _app;
  12.     void Awake()
  13.     {
  14.         _app = this;
  15.         Bootstrapping.Configure(this);
  16.     }
  17.    
  18.     public void ShowInHierarchy() => this.gameObject.hideFlags = HideFlags.None;
  19.     public void HideInHierarchy() => this.gameObject.hideFlags = HideFlags.HideInHierarchy;
  20.     public void ToggleInHierarchy() => this.gameObject.hideFlags = this.gameObject.hideFlags == HideFlags.None ? HideFlags.HideInHierarchy : HideFlags.None;
  21.    
  22.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  23.     public static void Main()
  24.     {
  25.         var app = new GameObject(Application.productName)
  26.         {
  27.             hideFlags = VISIBILITY
  28.         };
  29.         DontDestroyOnLoad(app);
  30.         Bootstrapping.Start(app.AddComponent<MagicBootstrap>());
  31.     }
  32.  
  33.     public static bool TryGetService<T>(out T result)
  34.     {
  35.         var found = GetService<T>();
  36.         result = found;
  37.         return found != null;
  38.     }
  39.     public static T GetService<T>() => _app ? _app.GetComponentInChildren<T>() : default;
  40.  
  41.     void OnDestroy() => Bootstrapping.Teardown(this);
  42.    
  43. #if UNITY_EDITOR
  44.     [UnityEditor.MenuItem("App/Select")]
  45.     public static void Menu_SelectManager()
  46.     {
  47.         _app.ShowInHierarchy();
  48.         UnityEditor.Selection.activeObject = _app.gameObject;
  49.     }
  50.  
  51.     [UnityEditor.MenuItem("App/Hide")]
  52.     public static void Menu_Hide() => _app.HideInHierarchy();
  53.  
  54.     [UnityEditor.MenuItem("App/Select", true)]
  55.     public static bool Menu_SelectApp() => Application.isPlaying;
  56.     [UnityEditor.MenuItem("App/Hide", true)]
  57.     public static bool Menu_HideApp() => Application.isPlaying;
  58. #endif
  59.  
  60.     public T Get<T>() => this.GetComponentInChildren<T>();
  61.  
  62.     public bool TryGet<T>(out T result)
  63.     {
  64.         var found = Get<T>();
  65.         result = found;
  66.         return found != null;
  67.     }
  68. }
  69.  
  70. public interface App
  71. {
  72.     T Get<T>();
  73.     bool TryGet<T>(out T result);
  74. }
  75.  
  76. public static class Bootstrapping
  77. {
  78.     static List<ISetup> _setups = new();
  79.     public static void Configure(App app)
  80.     {
  81.         _setups = typeof(ISetup).InstantiateAll().Cast<ISetup>().ToList();
  82.         _setups.ForEach(setup => setup.Configure(app));
  83.     }
  84.  
  85.     public static void Start(App app) => _setups.ForEach(setup => setup.Start(app));
  86.  
  87.     public static void Teardown(App app)
  88.     {
  89.         _setups.ForEach(setup => setup.Teardown(app));
  90.         _setups.Clear();
  91.     }
  92. }
  93.  
  94. public interface ISetup
  95. {
  96.     void Configure(App app);
  97.     void Start(App app);    
  98.     void Teardown(App app);
  99. }
  100.  
  101. public abstract class GameSetup : ISetup
  102. {
  103.     public abstract void Configure(App app);
  104.     public virtual void Start(App app){}
  105.     public virtual void Teardown(App app){}
  106. }
  107. public static class BootstrappingExtensions
  108. {
  109.     public static void ForAll<T>(this IList<T> list, Action<T> action)
  110.         {
  111.             foreach (var item in list)
  112.                 action(item);
  113.         }
  114.        
  115.         public static T[] WithComponents<T>(this IList<GameObject> gameObjects) =>
  116.             gameObjects.Select(x => x.GetComponent<T>()).Where(x => x != null).ToArray();
  117.        
  118.     public static IList<object> InstantiateAll(this Type type)
  119.     {
  120.         var types = AppDomain.CurrentDomain.GetAssemblies()
  121.             .SelectMany(s => s.GetTypes())
  122.             .Where(p => p.IsClass && !p.IsAbstract && type.IsAssignableFrom(p));
  123.         return types.Select(Activator.CreateInstance).ToList();
  124.     }
  125.         public static GameObject[] Load(this App app, string path) =>
  126.             InstantiateFromResource<GameObject>(path, (app as MagicBootstrap).transform);
  127.    
  128.         public static T[] InstantiateFromResource<T>(string path, Transform parent = null)
  129.             where T : Object
  130.         {
  131.             var prefabs = Resources.LoadAll<T>(path);
  132.             var instances = new T[prefabs.Length];
  133.             for (int i = 0; i < prefabs.Length; i++)
  134.             {
  135.                 var instance = Object.Instantiate(prefabs[i], parent, true);
  136.                 instance.name = prefabs[i].name;
  137.                 instances[i] = instance;
  138.             }
  139.    
  140.             return instances;
  141.         }
  142. }
  143. public interface IService
  144. {
  145.     void Configure(App app);
  146.     void Begin(App app);
  147.     void Teardown(App app);
  148. }
  149. public abstract class Service : MonoBehaviour, IService
  150. {
  151.     public abstract void Configure(App app);
  152.  
  153.     public virtual void Begin(App app)
  154.     {
  155.     }
  156.  
  157.     public virtual void Teardown(App app)
  158.     {
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement