68 lines
1.8 KiB
C#
68 lines
1.8 KiB
C#
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<string, Canvas> 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<Pauser>();
|
|
audioSource = gameObject.GetComponent<AudioSource>();
|
|
}
|
|
|
|
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<string, Canvas>(mainCanvasTransform.childCount);
|
|
|
|
for (int i = 0; i < mainCanvasTransform.childCount; i++)
|
|
{
|
|
Transform child = mainCanvasTransform.GetChild(i);
|
|
canvases.Add(child.name, child.GetComponent<Canvas>());
|
|
}
|
|
}
|
|
|
|
public void PlayClickSound() => audioSource.Play();
|
|
}
|
|
}
|