Files
WhaleAdventure_VR/Assets/02_Scripts/UI/DialogHud.cs
2026-06-19 17:09:36 +09:00

77 lines
2.9 KiB
C#

using TMPro;
using UnityEngine;
// 화자(NPC) 옆에 떠 있는 World-space 대사 HUD 싱글턴.
// DialogPlayer가 대사 노드를 재생할 때 Show()로 화자 이름 + 대사를 표시한다.
//
// Placement(화자 옆 배치 + 빌보드) 로직을 담당한다. 원래 ChoiceHud에 있던 로직을 이리로 옮겼다.
// 자기 자신(transform)을 화자 옆으로 옮기므로, ChoiceHud를 이 오브젝트의 자식으로 두면 함께 따라온다.
// 주의: 이 오브젝트(GO)는 항상 활성 상태여야 한다(LateUpdate가 돌아야 하므로).
// 보이기/숨기기는 자식 패널(_panel)만 토글한다.
public class DialogHud : MonoBehaviour
{
public static DialogHud Instance { get; private set; }
[Header("Refs")]
[SerializeField] private GameObject _panel; // 대사 패널(토글 대상). 보통 이 오브젝트의 자식.
[SerializeField] private TMP_Text _speakerName;
[SerializeField] private TMP_Text _dialogueText;
[Header("Placement")]
[SerializeField] private float _chestHeight = 1.2f; // 화자 발 기준 가슴 높이
[SerializeField] private float _forwardOffset = 0.5f; // 화자→플레이어 방향으로 띄울 거리
private Transform _speakerTransform;
private void Awake()
{
if (Instance != null && Instance != this) { Destroy(gameObject); return; }
Instance = this;
Hide();
}
private void OnDestroy()
{
if (Instance == this) Instance = null;
}
public void Show(CharacterData speaker, string text)
{
_speakerTransform = speaker != null ? CharacterVoiceObject.Find(speaker)?.transform : null;
if (_speakerName != null)
_speakerName.text = speaker != null ? speaker.Name : string.Empty;
if (_dialogueText != null)
_dialogueText.text = text;
if (_panel != null) _panel.SetActive(true);
}
public void Hide()
{
if (_dialogueText != null) _dialogueText.text = string.Empty;
if (_speakerName != null) _speakerName.text = string.Empty;
if (_panel != null) _panel.SetActive(false);
_speakerTransform = null;
}
private void LateUpdate()
{
if (_speakerTransform == null || Camera.main == null) return;
var camTr = Camera.main.transform;
// 화자에서 플레이어 카메라로 향하는 수평 방향 (yaw만)
Vector3 toCam = camTr.position - _speakerTransform.position;
toCam.y = 0f;
if (toCam.sqrMagnitude < 0.0001f) return;
Vector3 dir = toCam.normalized;
Vector3 chestWorld = _speakerTransform.position + Vector3.up * _chestHeight;
transform.position = chestWorld + dir * _forwardOffset;
// 빌보드 — 캔버스의 -Z(읽는 면)가 카메라를 향하도록 +Z를 카메라 반대로
transform.rotation = Quaternion.LookRotation(-dir);
}
}