using System; using System.Collections.Generic; using Pausable; using UnityEngine; namespace UI { public class UIManager : MonoBehaviour, IUIManager { private static UIManager _instance = null; public static UIManager Instance => _instance; public Pauser Pauser { get; private set; } = null; private Dictionary canvases = null; private AudioSource audioSource = null; private void Awake() { if (_instance == null) _instance = this; if (_instance != this) Destroy(this); } private void Start() { Pauser = gameObject.AddComponent(); audioSource = gameObject.GetComponent(); } public void SwitchToCanvas(string canvasName) { Initialize(); CloseAllCanvases(); canvases[canvasName].gameObject.SetActive(true); } public void CloseAllCanvases() { Initialize(); 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(mainCanvasTransform.childCount); for (int i = 0; i < mainCanvasTransform.childCount; i++) { Transform child = mainCanvasTransform.GetChild(i); canvases.Add(child.name, child.GetComponent()); } } public void PlayClickSound() => audioSource.Play(); } }