61 lines
1.6 KiB
C#
61 lines
1.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class InfoBox : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
|
|
{
|
|
[SerializeField]
|
|
private VisualTreeAsset uiAsset;
|
|
|
|
[SerializeField]
|
|
private PanelSettings uiPanelSettings;
|
|
|
|
public bool AlwaysVisible;
|
|
|
|
private UIDocument uiDocument;
|
|
private Camera mainCamera;
|
|
private IInventoryHolder inventoryHolder;
|
|
private Label infoBox;
|
|
|
|
void Start()
|
|
{
|
|
mainCamera = Camera.main;
|
|
inventoryHolder = GetComponent<IInventoryHolder>();
|
|
|
|
uiDocument = gameObject.AddComponent<UIDocument>();
|
|
uiDocument.panelSettings = uiPanelSettings;
|
|
uiDocument.visualTreeAsset = uiAsset;
|
|
|
|
infoBox = uiDocument.rootVisualElement.Q<Label>("InfoBox");
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
// Convert world position to screen space
|
|
Vector3 screenPos = mainCamera.WorldToScreenPoint(transform.position);
|
|
|
|
// Update panel position (UI Toolkit uses top-left origin)
|
|
infoBox.style.left = screenPos.x;
|
|
infoBox.style.top = Screen.height - screenPos.y;
|
|
infoBox.style.visibility = AlwaysVisible ? Visibility.Visible : Visibility.Hidden;
|
|
|
|
UpdateText();
|
|
}
|
|
|
|
void UpdateText()
|
|
{
|
|
infoBox.text = inventoryHolder.Inventory.ToString();
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
infoBox.style.visibility = Visibility.Visible;
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
if (!AlwaysVisible)
|
|
infoBox.style.visibility = Visibility.Hidden;
|
|
}
|
|
}
|