Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- using System;
- using System.Linq;
- using Object = UnityEngine.Object;
- public class MagicBootstrap : MonoBehaviour,App
- {
- static HideFlags VISIBILITY = HideFlags.HideInHierarchy;
- static MagicBootstrap _app;
- void Awake()
- {
- _app = this;
- Bootstrapping.Configure(this);
- }
- public void ShowInHierarchy() => this.gameObject.hideFlags = HideFlags.None;
- public void HideInHierarchy() => this.gameObject.hideFlags = HideFlags.HideInHierarchy;
- public void ToggleInHierarchy() => this.gameObject.hideFlags = this.gameObject.hideFlags == HideFlags.None ? HideFlags.HideInHierarchy : HideFlags.None;
- [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
- public static void Main()
- {
- var app = new GameObject(Application.productName)
- {
- hideFlags = VISIBILITY
- };
- DontDestroyOnLoad(app);
- Bootstrapping.Start(app.AddComponent<MagicBootstrap>());
- }
- public static bool TryGetService<T>(out T result)
- {
- var found = GetService<T>();
- result = found;
- return found != null;
- }
- public static T GetService<T>() => _app ? _app.GetComponentInChildren<T>() : default;
- void OnDestroy() => Bootstrapping.Teardown(this);
- #if UNITY_EDITOR
- [UnityEditor.MenuItem("App/Select")]
- public static void Menu_SelectManager()
- {
- _app.ShowInHierarchy();
- UnityEditor.Selection.activeObject = _app.gameObject;
- }
- [UnityEditor.MenuItem("App/Hide")]
- public static void Menu_Hide() => _app.HideInHierarchy();
- [UnityEditor.MenuItem("App/Select", true)]
- public static bool Menu_SelectApp() => Application.isPlaying;
- [UnityEditor.MenuItem("App/Hide", true)]
- public static bool Menu_HideApp() => Application.isPlaying;
- #endif
- public T Get<T>() => this.GetComponentInChildren<T>();
- public bool TryGet<T>(out T result)
- {
- var found = Get<T>();
- result = found;
- return found != null;
- }
- }
- public interface App
- {
- T Get<T>();
- bool TryGet<T>(out T result);
- }
- public static class Bootstrapping
- {
- static List<ISetup> _setups = new();
- public static void Configure(App app)
- {
- _setups = typeof(ISetup).InstantiateAll().Cast<ISetup>().ToList();
- _setups.ForEach(setup => setup.Configure(app));
- }
- public static void Start(App app) => _setups.ForEach(setup => setup.Start(app));
- public static void Teardown(App app)
- {
- _setups.ForEach(setup => setup.Teardown(app));
- _setups.Clear();
- }
- }
- public interface ISetup
- {
- void Configure(App app);
- void Start(App app);
- void Teardown(App app);
- }
- public abstract class GameSetup : ISetup
- {
- public abstract void Configure(App app);
- public virtual void Start(App app){}
- public virtual void Teardown(App app){}
- }
- public static class BootstrappingExtensions
- {
- public static void ForAll<T>(this IList<T> list, Action<T> action)
- {
- foreach (var item in list)
- action(item);
- }
- public static T[] WithComponents<T>(this IList<GameObject> gameObjects) =>
- gameObjects.Select(x => x.GetComponent<T>()).Where(x => x != null).ToArray();
- public static IList<object> InstantiateAll(this Type type)
- {
- var types = AppDomain.CurrentDomain.GetAssemblies()
- .SelectMany(s => s.GetTypes())
- .Where(p => p.IsClass && !p.IsAbstract && type.IsAssignableFrom(p));
- return types.Select(Activator.CreateInstance).ToList();
- }
- public static GameObject[] Load(this App app, string path) =>
- InstantiateFromResource<GameObject>(path, (app as MagicBootstrap).transform);
- public static T[] InstantiateFromResource<T>(string path, Transform parent = null)
- where T : Object
- {
- var prefabs = Resources.LoadAll<T>(path);
- var instances = new T[prefabs.Length];
- for (int i = 0; i < prefabs.Length; i++)
- {
- var instance = Object.Instantiate(prefabs[i], parent, true);
- instance.name = prefabs[i].name;
- instances[i] = instance;
- }
- return instances;
- }
- }
- public interface IService
- {
- void Configure(App app);
- void Begin(App app);
- void Teardown(App app);
- }
- public abstract class Service : MonoBehaviour, IService
- {
- public abstract void Configure(App app);
- public virtual void Begin(App app)
- {
- }
- public virtual void Teardown(App app)
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement