Files
Click-PointRPG/Assets/Scripts/NameplateController.cs

48 lines
1.3 KiB
C#
Raw Normal View History

2026-02-10 21:27:46 +00:00
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;
}
}