Update
This commit is contained in:
104
Assets/Scripts/UI/HUDController.cs
Normal file
104
Assets/Scripts/UI/HUDController.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using OnlyScove.Scripts;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class HUDController : MonoBehaviour
|
||||
{
|
||||
[Header("UI Document")]
|
||||
public UIDocument hudDocument;
|
||||
|
||||
private VisualElement _healthFill;
|
||||
private VisualElement _staminaFill;
|
||||
private Label _healthText;
|
||||
private Label _noiseLabel;
|
||||
private Label _interactionLabel;
|
||||
private VisualElement _interactionPrompt;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (hudDocument == null)
|
||||
hudDocument = GetComponent<UIDocument>();
|
||||
|
||||
var root = hudDocument.rootVisualElement;
|
||||
|
||||
// Tìm các thành phần UI theo Name (Bạn cần đặt tên này trong UXML)
|
||||
_healthFill = root.Q<VisualElement>("health-fill");
|
||||
_staminaFill = root.Q<VisualElement>("stamina-fill");
|
||||
_healthText = root.Q<Label>("health-text");
|
||||
_noiseLabel = root.Q<Label>("noise-label");
|
||||
_interactionLabel = root.Q<Label>("interaction-text");
|
||||
_interactionPrompt = root.Q<VisualElement>("interaction-prompt");
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Kết nối với Local Player
|
||||
if (PlayerStateMachine.Local != null)
|
||||
{
|
||||
SubscribeToPlayer(PlayerStateMachine.Local);
|
||||
}
|
||||
}
|
||||
|
||||
private PlayerStateMachine _currentPlayer;
|
||||
|
||||
private void SubscribeToPlayer(PlayerStateMachine player)
|
||||
{
|
||||
if (_currentPlayer == player) return;
|
||||
|
||||
// Hủy đăng ký player cũ nếu có
|
||||
if (_currentPlayer != null)
|
||||
{
|
||||
_currentPlayer.OnHealthChanged -= UpdateHealth;
|
||||
_currentPlayer.OnStaminaChanged -= UpdateStamina;
|
||||
_currentPlayer.OnNoiseLevelChanged -= UpdateNoise;
|
||||
_currentPlayer.OnInteractableTargetChanged -= UpdateInteraction;
|
||||
}
|
||||
|
||||
_currentPlayer = player;
|
||||
|
||||
// Đăng ký player mới
|
||||
_currentPlayer.OnHealthChanged += UpdateHealth;
|
||||
_currentPlayer.OnStaminaChanged += UpdateStamina;
|
||||
_currentPlayer.OnNoiseLevelChanged += UpdateNoise;
|
||||
_currentPlayer.OnInteractableTargetChanged += UpdateInteraction;
|
||||
|
||||
// Cập nhật giá trị ban đầu
|
||||
UpdateHealth(_currentPlayer.Health);
|
||||
UpdateStamina(_currentPlayer.Stamina);
|
||||
UpdateNoise(_currentPlayer.NoiseLevel);
|
||||
}
|
||||
|
||||
private void UpdateHealth(float health)
|
||||
{
|
||||
if (_healthFill != null) _healthFill.style.width = Length.Percent(health);
|
||||
if (_healthText != null) _healthText.text = $"HEALTH: {Mathf.RoundToInt(health)}/100";
|
||||
}
|
||||
|
||||
private void UpdateStamina(float stamina)
|
||||
{
|
||||
if (_staminaFill != null) _staminaFill.style.width = Length.Percent(stamina);
|
||||
}
|
||||
|
||||
private void UpdateNoise(float noise)
|
||||
{
|
||||
if (_noiseLabel != null) _noiseLabel.text = $"NOISE: {Mathf.RoundToInt(noise)}%";
|
||||
}
|
||||
|
||||
private void UpdateInteraction(IInteractable interactable)
|
||||
{
|
||||
if (_interactionPrompt == null) return;
|
||||
|
||||
if (interactable != null)
|
||||
{
|
||||
_interactionPrompt.style.display = DisplayStyle.Flex;
|
||||
if (_interactionLabel != null) _interactionLabel.text = interactable.InteractionPrompt;
|
||||
}
|
||||
else
|
||||
{
|
||||
_interactionPrompt.style.display = DisplayStyle.None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UI/HUDController.cs.meta
Normal file
2
Assets/Scripts/UI/HUDController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e79b70607af6eeb458c8eb6605e39b56
|
||||
92
Assets/Scripts/UI/UIManager.cs
Normal file
92
Assets/Scripts/UI/UIManager.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UI/UIManager.cs.meta
Normal file
2
Assets/Scripts/UI/UIManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bcb7b8ed439bb4546b0648c627c2ce5d
|
||||
Reference in New Issue
Block a user