61 lines
1.5 KiB
C#
61 lines
1.5 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public interface IGlobalUISectionController { }
|
|
|
|
public sealed class GlobalUIController : MonoBehaviour
|
|
{
|
|
public static GlobalUIController Instance { get; private set; }
|
|
|
|
private readonly HashSet<IGlobalUISectionController> sectionControllers = new();
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance != null && Instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
Instance = this;
|
|
|
|
foreach (MonoBehaviour behaviour in FindObjectsByType<MonoBehaviour>(
|
|
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<T> GetControllers<T>() 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;
|
|
}
|
|
}
|
|
}
|