using TMPro;
using UnityEngine;
namespace NightclubArcadia.Interaction
{
///
/// A floating text label that turns to face the camera.
///
/// Lifted out of NPCStandIn, which created its label in Awake and billboarded it in
/// LateUpdate. That worked, but it meant the text only existed at runtime and could
/// not be seen or positioned in the Editor. This is a normal component on a normal
/// child object, so a prefab can show what it will look like.
///
[RequireComponent(typeof(TextMeshPro))]
public sealed class WorldSpaceLabel : MonoBehaviour
{
[SerializeField] bool billboard = true;
TextMeshPro text;
Camera view;
void Awake() => text = GetComponent();
public void SetText(string value)
{
if (text == null)
{
text = GetComponent();
}
text.text = value;
}
void LateUpdate()
{
if (!billboard)
{
return;
}
// Camera.main is a tagged lookup; cache it, but re-acquire if the camera was
// destroyed with a scene, which happens when the Systems scene reloads.
if (view == null)
{
view = Camera.main;
if (view == null)
{
return;
}
}
transform.rotation = Quaternion.LookRotation(transform.position - view.transform.position);
}
}
}