130 lines
No EOL
2.7 KiB
C#
130 lines
No EOL
2.7 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using System;
|
|
|
|
public class MANAGER_BoardUI : MonoBehaviour
|
|
{
|
|
|
|
public static MANAGER_BoardUI Instance { get; private set; }
|
|
|
|
[Header("Main Control")]
|
|
[SerializeField] private GameObject boardRoot;
|
|
[SerializeField] private Canvas parentCanvas;
|
|
|
|
[Header("Pages Setup")]
|
|
[SerializeField] private GameObject[] pages;
|
|
|
|
private int currentPageIndex = 0;
|
|
private List<GameObject> previouslyActiveUI = new List<GameObject>();
|
|
|
|
|
|
public static event Action OnBoardOpened;
|
|
public static event Action OnBoardClosed;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null) Instance = this;
|
|
else Destroy(gameObject);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
|
|
CloseBoard();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
|
|
if (boardRoot.activeSelf && Input.GetKeyDown(KeyCode.Escape))
|
|
{
|
|
CloseBoard();
|
|
}
|
|
}
|
|
|
|
public void OpenBoard()
|
|
{
|
|
if (boardRoot == null) return;
|
|
|
|
ToggleOtherUI(false);
|
|
boardRoot.SetActive(true);
|
|
OpenPage(0);
|
|
|
|
OnBoardOpened?.Invoke();
|
|
}
|
|
|
|
public void CloseBoard()
|
|
{
|
|
if (boardRoot == null) return;
|
|
|
|
boardRoot.SetActive(false);
|
|
ToggleOtherUI(true);
|
|
|
|
OnBoardClosed?.Invoke();
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ToggleOtherUI(bool show)
|
|
{
|
|
if (parentCanvas == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!show)
|
|
{
|
|
previouslyActiveUI.Clear();
|
|
|
|
foreach (Transform child in parentCanvas.transform)
|
|
{
|
|
|
|
if (child.gameObject != boardRoot && child.gameObject.activeSelf)
|
|
{
|
|
previouslyActiveUI.Add(child.gameObject);
|
|
child.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
|
|
foreach (GameObject obj in previouslyActiveUI)
|
|
{
|
|
if (obj != null) obj.SetActive(true);
|
|
}
|
|
previouslyActiveUI.Clear();
|
|
}
|
|
}
|
|
|
|
public void NextPage()
|
|
{
|
|
if (pages == null || pages.Length <= 1) return;
|
|
|
|
currentPageIndex = (currentPageIndex + 1) % pages.Length;
|
|
OpenPage(currentPageIndex);
|
|
}
|
|
|
|
public void PreviousPage()
|
|
{
|
|
if (pages == null || pages.Length <= 1) return;
|
|
|
|
currentPageIndex--;
|
|
if (currentPageIndex < 0) currentPageIndex = pages.Length - 1;
|
|
|
|
OpenPage(currentPageIndex);
|
|
}
|
|
|
|
public void OpenPage(int index)
|
|
{
|
|
if (pages == null || index < 0 || index >= pages.Length) return;
|
|
|
|
for (int i = 0; i < pages.Length; i++)
|
|
{
|
|
if (pages[i] != null)
|
|
pages[i].SetActive(i == index);
|
|
}
|
|
currentPageIndex = index;
|
|
}
|
|
} |