using System.Collections.Generic; using UnityEngine; public interface IGlobalUISectionController { } public sealed class GlobalUIController : MonoBehaviour { public static GlobalUIController Instance { get; private set; } private readonly HashSet sectionControllers = new(); private void Awake() { if (Instance != null && Instance != this) { Destroy(gameObject); return; } Instance = this; foreach (MonoBehaviour behaviour in FindObjectsByType( FindObjectsInactive.Include, FindObjectsSortMode.None)) { if (behaviour is IGlobalUISectionController controller) Register(controller); } } public void Register(IGlobalUISectionController controller) { if (controller != null) sectionControllers.Add(controller); } public void Unregister(IGlobalUISectionController controller) { if (controller != null) sectionControllers.Remove(controller); } public IEnumerable GetControllers() where T : class, IGlobalUISectionController { foreach (IGlobalUISectionController controller in sectionControllers) { if (controller is T typedController) yield return typedController; } } private void OnDestroy() { if (Instance == this) { sectionControllers.Clear(); Instance = null; } } }