2022-02-26 20:09:12 +03:00
|
|
|
using System;
|
2022-02-23 14:53:01 +03:00
|
|
|
using System.Collections.Generic;
|
2022-02-26 19:53:47 +03:00
|
|
|
using Pausable;
|
2022-02-23 14:53:01 +03:00
|
|
|
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
|
|
|
|
2022-02-26 19:53:47 +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;
|
2022-02-23 14:53:01 +03:00
|
|
|
|
|
|
|
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>();
|
|
|
|
}
|
2022-02-26 19:53:47 +03:00
|
|
|
|
2022-02-23 14:53:01 +03:00
|
|
|
public void SwitchToCanvas(string canvasName)
|
|
|
|
{
|
|
|
|
Initialize();
|
|
|
|
|
|
|
|
CloseAllCanvases();
|
|
|
|
|
|
|
|
canvases[canvasName].gameObject.SetActive(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CloseAllCanvases()
|
|
|
|
{
|
2022-02-24 00:21:59 +03:00
|
|
|
Initialize();
|
|
|
|
|
2022-02-23 14:53:01 +03:00
|
|
|
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();
|
2022-02-23 14:53:01 +03:00
|
|
|
}
|
|
|
|
}
|