Files
BABA_YAGA/Assets/Scripts/UI/UIManager.cs

148 lines
5.3 KiB
C#
Raw Normal View History

2026-04-25 18:20:16 +07:00
using System.Collections;
2026-04-23 23:09:54 +07:00
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace UI
{
public class UIManager : MonoBehaviour
{
2026-04-25 18:20:16 +07:00
public static UIManager Instance { get; private set; }
[System.Serializable]
2026-04-23 23:09:54 +07:00
public class ScreenData
{
public string screenName; // Phải khớp với chuỗi gọi trong ShowScreen
2026-04-23 23:09:54 +07:00
public UIDocument document;
public bool isActive;
2026-04-25 18:20:16 +07:00
public bool isOverlay;
public Texture2D customCursor;
2026-04-23 23:09:54 +07:00
}
public List<ScreenData> screens = new List<ScreenData>();
2026-04-25 18:20:16 +07:00
[Header("Default Settings")]
public Texture2D defaultCursor;
public string initialScreen = "MainMenu";
2026-04-25 18:20:16 +07:00
private string _currentScreenName;
private void Awake()
{
if (Instance == null) Instance = this;
else Destroy(gameObject);
// Khởi tạo trạng thái ban đầu: Ẩn tất cả trừ màn hình mặc định
2026-04-25 18:20:16 +07:00
foreach (var screen in screens)
{
if (screen.document == null) continue;
if (screen.screenName == "Settings") screen.document.sortingOrder = 999;
screen.isActive = (screen.screenName == initialScreen);
screen.document.rootVisualElement.style.display = screen.isActive ? DisplayStyle.Flex : DisplayStyle.None;
2026-04-25 18:20:16 +07:00
}
_currentScreenName = initialScreen;
2026-04-25 18:20:16 +07:00
}
private void Update()
2026-04-25 18:20:16 +07:00
{
if (Input.GetKeyDown(KeyCode.Escape)) ToggleSettings();
2026-04-25 18:20:16 +07:00
}
2026-04-23 23:09:54 +07:00
public void ShowOnly(string name) => ShowScreen(name);
2026-04-23 23:09:54 +07:00
public void ShowScreen(string name)
2026-04-23 23:09:54 +07:00
{
if (_currentScreenName == name) return;
2026-04-23 23:09:54 +07:00
// Kiểm tra xem màn hình mục tiêu có tồn tại không trước khi tắt cái cũ
var nextData = screens.Find(s => s.screenName == name);
if (nextData == null)
2026-04-23 23:09:54 +07:00
{
Debug.LogError($"[UIManager] Screen '{name}' not found in the list! Check your Inspector names.");
return;
2026-04-23 23:09:54 +07:00
}
2026-04-25 18:20:16 +07:00
StartCoroutine(TransitionRoutine(nextData));
2026-04-25 18:20:16 +07:00
}
private IEnumerator TransitionRoutine(ScreenData nextData)
2026-04-25 18:20:16 +07:00
{
// 1. Fade Out các màn hình chính hiện tại (trừ Overlay)
2026-04-25 18:20:16 +07:00
foreach (var s in screens)
2026-04-23 23:09:54 +07:00
{
if (s.isActive && !s.isOverlay)
2026-04-25 18:20:16 +07:00
{
var root = s.document.rootVisualElement.Q<VisualElement>();
if (root != null) root.AddToClassList("hidden");
2026-04-25 18:20:16 +07:00
s.isActive = false;
}
2026-04-23 23:09:54 +07:00
}
2026-04-25 18:20:16 +07:00
yield return new WaitForSeconds(0.3f);
SyncScreens(); // Ẩn hẳn display
2026-04-23 23:09:54 +07:00
// 2. Hiện màn hình mới
nextData.isActive = true;
_currentScreenName = nextData.screenName;
var nextRoot = nextData.document.rootVisualElement.Q<VisualElement>();
if (nextRoot != null)
2026-04-23 23:09:54 +07:00
{
nextData.document.rootVisualElement.style.display = DisplayStyle.Flex;
nextRoot.AddToClassList("hidden");
yield return null; // Chờ 1 frame để UI Toolkit cập nhật
nextRoot.RemoveFromClassList("hidden");
2026-04-23 23:09:54 +07:00
}
ApplyCursor(nextData.customCursor != null ? nextData.customCursor : defaultCursor);
2026-04-23 23:09:54 +07:00
}
2026-04-25 18:20:16 +07:00
public void ToggleSettings()
2026-04-23 23:09:54 +07:00
{
2026-04-25 18:20:16 +07:00
var settingsData = screens.Find(s => s.screenName == "Settings");
if (settingsData == null) return;
settingsData.isActive = !settingsData.isActive;
settingsData.document.rootVisualElement.style.display = settingsData.isActive ? DisplayStyle.Flex : DisplayStyle.None;
if (settingsData.isActive)
{
UnityEngine.Cursor.visible = true;
UnityEngine.Cursor.lockState = CursorLockMode.None;
ApplyCursor(settingsData.customCursor != null ? settingsData.customCursor : defaultCursor);
}
else
{
// Nếu tắt Settings, quay về trạng thái của màn hình hiện tại
var current = screens.Find(s => s.screenName == _currentScreenName);
if (current != null) ApplyCursor(current.customCursor != null ? current.customCursor : defaultCursor);
// Tùy vào game là FPS hay Menu mà ẩn chuột
if (_currentScreenName == "HUD")
{
UnityEngine.Cursor.visible = false;
UnityEngine.Cursor.lockState = CursorLockMode.Locked;
}
}
}
public void SyncScreens()
{
foreach (var screen in screens)
{
if (screen.document != null && screen.document.rootVisualElement != null)
{
screen.document.rootVisualElement.style.display = screen.isActive ? DisplayStyle.Flex : DisplayStyle.None;
}
2026-04-25 18:20:16 +07:00
}
2026-04-23 23:09:54 +07:00
}
2026-04-25 18:20:16 +07:00
private void ApplyCursor(Texture2D texture)
2026-04-23 23:09:54 +07:00
{
2026-04-25 18:20:16 +07:00
UnityEngine.Cursor.SetCursor(texture, Vector2.zero, CursorMode.Auto);
2026-04-23 23:09:54 +07:00
}
}
}