initial commit
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
public class InfoBox : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
|
||||
{
|
||||
// public Vector3 worldOffset = new Vector3(0, 1f, 0);
|
||||
public Vector3 worldOffset = new Vector3(0, 0, 0);
|
||||
|
||||
public UIDocument uiDocument;
|
||||
public bool AlwaysVisible;
|
||||
|
||||
private Camera mainCamera;
|
||||
private IInventoryHolder inventoryHolder;
|
||||
private Label infoBox;
|
||||
|
||||
void Start()
|
||||
{
|
||||
mainCamera = Camera.main;
|
||||
|
||||
if (uiDocument != null)
|
||||
{
|
||||
infoBox = uiDocument.rootVisualElement.Q<Label>("InfoBox");
|
||||
inventoryHolder = GetComponent<IInventoryHolder>();
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// if (infoBox != null)
|
||||
{
|
||||
// Convert world position to screen space
|
||||
Vector3 screenPos = mainCamera.WorldToScreenPoint(transform.position + worldOffset);
|
||||
|
||||
// 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()
|
||||
{
|
||||
// if (infoBox != null)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 687937cfe3daa04489c5323f36aef2e7
|
||||
Reference in New Issue
Block a user