49 lines
1.9 KiB
C#
49 lines
1.9 KiB
C#
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class LockOnManager : MonoBehaviour{
|
|
[ShowInInspector]
|
|
public UIDocument lockOnDocument;
|
|
public GameObject lockOnTarget;
|
|
|
|
private Label elementLabelName;
|
|
private VisualElement elementRoot;
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start()
|
|
{
|
|
if (lockOnTarget.GetComponent<ILockOnTarget>() == null) {
|
|
Debug.LogError($"Game Object {lockOnTarget.name} does not implement the ILockOnTarget interface. Not processing lock-on actions!");
|
|
}
|
|
|
|
elementRoot = lockOnDocument.rootVisualElement.Query<VisualElement>("LockOnGroup");
|
|
elementLabelName = lockOnDocument.rootVisualElement.Query<Label>("LockOnName").First();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void LateUpdate(){
|
|
if (lockOnTarget.GetComponent<ILockOnTarget>() != null) {
|
|
// This is just test logic to get an image above a lock on.
|
|
// TODO: Replace with something less silly
|
|
Vector2 screenPos = RuntimePanelUtils.CameraTransformWorldToPanel(
|
|
lockOnDocument.rootVisualElement.panel,
|
|
lockOnTarget.GetComponent<ILockOnTarget>().GetReticlePosition(),
|
|
Camera.main.GetComponent<Camera>()
|
|
);
|
|
|
|
// Set name
|
|
elementLabelName.name = lockOnTarget.name;
|
|
|
|
// Set position (add the width/height of the element)
|
|
elementRoot.style.top = new StyleLength(screenPos.y - elementRoot.resolvedStyle.height * .7f );
|
|
elementRoot.style.left = new StyleLength(screenPos.x - elementRoot.resolvedStyle.width / 2f);
|
|
|
|
// Set enabled
|
|
elementRoot.SetEnabled(true);
|
|
} else {
|
|
elementRoot.SetEnabled(false);
|
|
}
|
|
|
|
}
|
|
}
|