48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
using UnityEngine;
|
|
using TMPro;
|
|
using UnityEngine.UI;
|
|
|
|
public class NameplateController : MonoBehaviour
|
|
{
|
|
[Header("UI refs")]
|
|
public TextMeshProUGUI nameText;
|
|
public Image backgroundImage; // optional background to tint
|
|
public Outline uiOutline; // optional UI outline component (for name text)
|
|
|
|
Camera mainCam;
|
|
Transform target;
|
|
|
|
void Awake()
|
|
{
|
|
mainCam = Camera.main;
|
|
}
|
|
|
|
public void Initialize(Selectable s, Color color, Vector3 offset)
|
|
{
|
|
target = s.transform;
|
|
if (nameText != null) nameText.text = s.displayName;
|
|
ApplyColor(color);
|
|
FollowTarget(target, offset);
|
|
}
|
|
|
|
public void FollowTarget(Transform t, Vector3 offset)
|
|
{
|
|
if (t == null) return;
|
|
Vector3 worldPos = t.position + offset;
|
|
transform.position = worldPos;
|
|
|
|
// Face camera
|
|
if (mainCam != null)
|
|
{
|
|
transform.rotation = Quaternion.LookRotation(transform.position - mainCam.transform.position);
|
|
}
|
|
}
|
|
|
|
void ApplyColor(Color c)
|
|
{
|
|
if (nameText != null) nameText.color = c;
|
|
if (backgroundImage != null) backgroundImage.color = new Color(c.r, c.g, c.b, backgroundImage.color.a);
|
|
if (uiOutline != null) uiOutline.effectColor = c;
|
|
}
|
|
}
|