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

68 lines
1.8 KiB
C#
Raw Normal View History

2022-02-26 20:09:12 +03:00
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;
2022-02-26 20:09:12 +03:00
public Pauser Pauser { get; private set; } = null;
2022-02-26 20:09:12 +03:00
private Dictionary<string, Canvas> canvases = null;
private AudioSource audioSource = null;
private void Awake()
{
if (_instance == null)
_instance = this;
if (_instance != this)
Destroy(this);
}
2022-02-26 20:09:12 +03:00
private void Start()
{
Pauser = gameObject.AddComponent<Pauser>();
audioSource = gameObject.GetComponent<AudioSource>();
}
public void SwitchToCanvas(string canvasName)
{
Initialize();
CloseAllCanvases();
canvases[canvasName].gameObject.SetActive(true);
}
public void CloseAllCanvases()
{
2022-02-24 00:21:59 +03:00
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<string, Canvas>(mainCanvasTransform.childCount);
for (int i = 0; i < mainCanvasTransform.childCount; i++)
{
Transform child = mainCanvasTransform.GetChild(i);
canvases.Add(child.name, child.GetComponent<Canvas>());
}
}
2022-02-26 20:09:12 +03:00
public void PlayClickSound() => audioSource.Play();
}
}