93 lines
3.0 KiB
C#
93 lines
3.0 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.UIElements;
|
||
|
|
|
||
|
|
namespace UI
|
||
|
|
{
|
||
|
|
[ExecuteAlways]
|
||
|
|
public class UIManager : MonoBehaviour
|
||
|
|
{
|
||
|
|
[Serializable]
|
||
|
|
public class ScreenData
|
||
|
|
{
|
||
|
|
public string screenName;
|
||
|
|
public UIDocument document;
|
||
|
|
public bool isActive;
|
||
|
|
}
|
||
|
|
|
||
|
|
[Header("Screens Management")]
|
||
|
|
public List<ScreenData> screens = new List<ScreenData>();
|
||
|
|
|
||
|
|
[Header("Live Preview (Editor Only)")]
|
||
|
|
[Range(0, 1)] public float globalOpacity = 1f;
|
||
|
|
|
||
|
|
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();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnEnable()
|
||
|
|
{
|
||
|
|
if (Application.isPlaying)
|
||
|
|
{
|
||
|
|
SetupEvents();
|
||
|
|
}
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 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)
|
||
|
|
{
|
||
|
|
screen.isActive = (screen.screenName == name);
|
||
|
|
}
|
||
|
|
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)
|
||
|
|
{
|
||
|
|
mainMenu.rootVisualElement.Q<Button>("btn-settings")?.RegisterCallback<ClickEvent>(e => ShowOnly("Settings"));
|
||
|
|
mainMenu.rootVisualElement.Q<Button>("btn-create")?.RegisterCallback<ClickEvent>(e => ShowOnly("Lobby"));
|
||
|
|
}
|
||
|
|
|
||
|
|
var settings = GetDocument("Settings");
|
||
|
|
settings?.rootVisualElement.Q<Button>("btn-back")?.RegisterCallback<ClickEvent>(e => ShowOnly("MainMenu"));
|
||
|
|
}
|
||
|
|
|
||
|
|
public UIDocument GetDocument(string name)
|
||
|
|
{
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|