BGJ-2022.1/Assets/Scripts/UI/UIManager.cs

53 lines
1.4 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace UI
{
public class UIManager : MonoBehaviour, IUIManager
{
private static UIManager _instance = null;
public static UIManager Instance => _instance;
private Dictionary<string, Canvas> canvases = null;
private void Awake()
{
if (_instance == null)
_instance = this;
if (_instance != this)
Destroy(this);
}
public void SwitchToCanvas(string canvasName)
{
Initialize();
CloseAllCanvases();
canvases[canvasName].gameObject.SetActive(true);
}
public void CloseAllCanvases()
{
foreach (Canvas canvas in canvases.Values)
canvas.gameObject.SetActive(false);
}
public void ExitGame() => Application.Quit();
private void Initialize()
{
if (canvases != null)
return;
Transform mainCanvasTransform = transform.Find("Main Canvas");
canvases = new Dictionary<string, Canvas>(mainCanvasTransform.childCount);
for (int i = 0; i < mainCanvasTransform.childCount; i++)
{
Transform child = mainCanvasTransform.GetChild(i);
canvases.Add(child.name, child.GetComponent<Canvas>());
}
}
}
}