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

58 lines
1.4 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)
{
2026-02-11 17:30:49 +00:00
if (s == null) return;
2026-02-10 21:27:46 +00:00
target = s.transform;
2026-02-11 17:30:49 +00:00
SetDisplayName(s.displayName);
2026-02-10 21:27:46 +00:00
ApplyColor(color);
FollowTarget(target, offset);
}
2026-02-11 17:30:49 +00:00
public void SetDisplayName(string name)
{
if (nameText != null) nameText.text = name;
}
2026-02-10 21:27:46 +00:00
public void FollowTarget(Transform t, Vector3 offset)
{
if (t == null) return;
2026-02-11 17:30:49 +00:00
2026-02-10 21:27:46 +00:00
Vector3 worldPos = t.position + offset;
transform.position = worldPos;
// Face camera
if (mainCam != null)
{
transform.rotation = Quaternion.LookRotation(transform.position - mainCam.transform.position);
}
}
2026-02-11 17:30:49 +00:00
public void ApplyColor(Color c)
2026-02-10 21:27:46 +00:00
{
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;
}
}
2026-02-11 17:30:49 +00:00