51 lines
1.4 KiB
C#
51 lines
1.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class UIToolkitInfoPanel : MonoBehaviour
|
|
{
|
|
public Transform targetObject;
|
|
public Vector3 worldOffset = new Vector3(0, 1f, 0);
|
|
|
|
private UIDocument uiDocument;
|
|
private VisualElement infoPanel;
|
|
private Label infoLabel;
|
|
private Camera mainCamera;
|
|
|
|
void Start()
|
|
{
|
|
uiDocument = GetComponent<UIDocument>();
|
|
mainCamera = Camera.main;
|
|
|
|
if (uiDocument != null)
|
|
{
|
|
infoPanel = uiDocument.rootVisualElement.Q<VisualElement>("InfoPanel");
|
|
infoLabel = uiDocument.rootVisualElement.Q<Label>("InfoLabel");
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (targetObject != null && infoPanel != null)
|
|
{
|
|
// Convert world position to screen space
|
|
Vector3 screenPos = mainCamera.WorldToScreenPoint(
|
|
targetObject.position + worldOffset
|
|
);
|
|
|
|
// Update panel position (UI Toolkit uses top-left origin)
|
|
infoPanel.style.left = screenPos.x;
|
|
infoPanel.style.top = Screen.height - screenPos.y;
|
|
|
|
// Update content
|
|
UpdateInfoContent();
|
|
}
|
|
}
|
|
|
|
void UpdateInfoContent()
|
|
{
|
|
if (infoLabel != null)
|
|
{
|
|
infoLabel.text = $"{targetObject.name}\nPosition: {targetObject.position}";
|
|
}
|
|
}
|
|
} |