Update
This commit is contained in:
@@ -1,92 +1,144 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
[ExecuteAlways]
|
||||
public class UIManager : MonoBehaviour
|
||||
{
|
||||
[Serializable]
|
||||
public static UIManager Instance { get; private set; }
|
||||
|
||||
[System.Serializable]
|
||||
public class ScreenData
|
||||
{
|
||||
public string screenName;
|
||||
public UIDocument document;
|
||||
public bool isActive;
|
||||
public bool isActive; // Thêm lại để tương thích với Editor gốc
|
||||
public bool isOverlay;
|
||||
public Texture2D customCursor;
|
||||
}
|
||||
|
||||
[Header("Screens Management")]
|
||||
public List<ScreenData> screens = new List<ScreenData>();
|
||||
|
||||
[Header("Live Preview (Editor Only)")]
|
||||
[Range(0, 1)] public float globalOpacity = 1f;
|
||||
[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()
|
||||
{
|
||||
// Tự động cập nhật giao diện ngay khi thay đổi thông số trong Inspector (không cần Play)
|
||||
SyncScreens();
|
||||
// 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 OnEnable()
|
||||
private void Update()
|
||||
{
|
||||
if (Application.isPlaying)
|
||||
if (Input.GetKeyDown(KeyCode.Escape))
|
||||
{
|
||||
SetupEvents();
|
||||
ToggleSettings();
|
||||
}
|
||||
SyncScreens();
|
||||
}
|
||||
|
||||
public void SyncScreens()
|
||||
{
|
||||
foreach (var screen in screens)
|
||||
{
|
||||
if (screen.document == null) continue;
|
||||
|
||||
var root = screen.document.rootVisualElement;
|
||||
if (root == null) continue;
|
||||
|
||||
// Bật tắt display dựa trên biến isActive
|
||||
root.style.display = screen.isActive ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
root.style.opacity = globalOpacity;
|
||||
if (screen != null && screen.document != null && screen.document.rootVisualElement != null)
|
||||
{
|
||||
screen.document.rootVisualElement.style.display = screen.isActive ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Hàm tiện ích để bật duy nhất 1 màn hình từ Code hoặc Button
|
||||
public void ShowOnly(string name)
|
||||
{
|
||||
foreach (var screen in screens)
|
||||
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)
|
||||
{
|
||||
screen.isActive = (screen.screenName == name);
|
||||
if (s.screenName != nextScreenName && !s.isOverlay && s.isActive)
|
||||
{
|
||||
var root = s.document.rootVisualElement.Q<VisualElement>();
|
||||
root.AddToClassList("hidden");
|
||||
s.isActive = false;
|
||||
}
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(0.3f);
|
||||
SyncScreens();
|
||||
}
|
||||
|
||||
private void SetupEvents()
|
||||
{
|
||||
// Logic đăng ký event thông minh ở đây (tương tự như trước nhưng linh hoạt hơn)
|
||||
var mainMenu = GetDocument("MainMenu");
|
||||
if (mainMenu != null)
|
||||
// Bật màn hình mới
|
||||
var nextData = screens.Find(s => s.screenName == nextScreenName);
|
||||
if (nextData != null && nextData.document != null)
|
||||
{
|
||||
mainMenu.rootVisualElement.Q<Button>("btn-settings")?.RegisterCallback<ClickEvent>(e => ShowOnly("Settings"));
|
||||
mainMenu.rootVisualElement.Q<Button>("btn-create")?.RegisterCallback<ClickEvent>(e => ShowOnly("Lobby"));
|
||||
nextData.isActive = true;
|
||||
_currentScreenRoot = nextData.document.rootVisualElement.Q<VisualElement>();
|
||||
_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;
|
||||
|
||||
var settings = GetDocument("Settings");
|
||||
settings?.rootVisualElement.Q<Button>("btn-back")?.RegisterCallback<ClickEvent>(e => ShowOnly("MainMenu"));
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public UIDocument GetDocument(string name)
|
||||
private void ApplyCursor(Texture2D texture)
|
||||
{
|
||||
return screens.Find(s => s.screenName == name)?.document;
|
||||
}
|
||||
|
||||
// Thêm hàm để Update Text/Image nhanh từ Inspector hoặc Script khác
|
||||
public void SetElementText(string screenName, string elementName, string newText)
|
||||
{
|
||||
var doc = GetDocument(screenName);
|
||||
var label = doc?.rootVisualElement.Q<Label>(elementName);
|
||||
if (label != null) label.text = newText;
|
||||
UnityEngine.Cursor.SetCursor(texture, Vector2.zero, CursorMode.Auto);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user