using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements; namespace UI { public class UIManager : MonoBehaviour { public static UIManager Instance { get; private set; } [System.Serializable] public class ScreenData { public string screenName; public UIDocument document; public bool isActive; // Thêm lại để tương thích với Editor gốc public bool isOverlay; public Texture2D customCursor; } public List screens = new List(); [Header("Default Settings")] public Texture2D defaultCursor; private VisualElement _currentScreenRoot; private string _currentScreenName; private void Awake() { Instance = this; SyncScreens(); // Đồng bộ trạng thái ban đầu foreach (var screen in screens) { if (screen.document != null && screen.screenName == "Settings") screen.document.sortingOrder = 999; } } private void Start() { if (!Application.isPlaying) return; ShowScreen("MainMenu"); } private void OnValidate() { // Chạy trong Editor để cập nhật UI ngay lập tức khi check vào ô isActive if (!Application.isPlaying) SyncScreens(); } private void Update() { if (Input.GetKeyDown(KeyCode.Escape)) { ToggleSettings(); } } public void SyncScreens() { foreach (var screen in screens) { if (screen != null && screen.document != null && screen.document.rootVisualElement != null) { screen.document.rootVisualElement.style.display = screen.isActive ? DisplayStyle.Flex : DisplayStyle.None; } } } public void ShowOnly(string name) { ShowScreen(name); } public void ShowScreen(string name) { if (_currentScreenName == name) return; StartCoroutine(TransitionRoutine(name)); } private IEnumerator TransitionRoutine(string nextScreenName) { // Tắt các màn hình khác foreach (var s in screens) { if (s.screenName != nextScreenName && !s.isOverlay && s.isActive) { var root = s.document.rootVisualElement.Q(); root.AddToClassList("hidden"); s.isActive = false; } } yield return new WaitForSeconds(0.3f); SyncScreens(); // Bật màn hình mới var nextData = screens.Find(s => s.screenName == nextScreenName); if (nextData != null && nextData.document != null) { nextData.isActive = true; _currentScreenRoot = nextData.document.rootVisualElement.Q(); _currentScreenName = nextScreenName; _currentScreenRoot.style.display = DisplayStyle.Flex; _currentScreenRoot.AddToClassList("hidden"); yield return null; _currentScreenRoot.RemoveFromClassList("hidden"); ApplyCursor(nextData.customCursor != null ? nextData.customCursor : defaultCursor); } } public void ToggleSettings() { 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 { UnityEngine.Cursor.visible = false; UnityEngine.Cursor.lockState = CursorLockMode.Locked; } } private void ApplyCursor(Texture2D texture) { UnityEngine.Cursor.SetCursor(texture, Vector2.zero, CursorMode.Auto); } } }