2026-07-10 다이얼로그 시스템 대대적 수정

This commit is contained in:
2026-07-10 12:17:10 +09:00
parent 5edc62d6c4
commit 51056f0994
28 changed files with 2173 additions and 64 deletions

View File

@@ -1,3 +1,4 @@
using System.Collections.Generic;
using TMPro;
using UnityEngine;
@@ -25,6 +26,28 @@ public class DialogHud : MonoBehaviour
[SerializeField] private float _lateralOffset = 0f; // 좌우 오프셋 (+ 화자와 마주보는 시점 오른쪽)
[SerializeField] private Vector3 _rotationOffset = Vector3.zero; // 화자 회전 기준 추가 회전 (+α, 오일러 각)
// ── 후보 위치 자동 선택 ─────────────────────────────────────
// 대사창을 띄울 때 후보를 우선순위 순서대로 검사해서, 벽/지형에 겹치지 않고
// 플레이어에게 가려지지 않는 첫 후보 위치에 창을 띄운다.
// 후보 목록 자체는 NPC마다 크기가 달라 화자의 DialogHudPlacement에서 넘어온다.
// 후보 하나가 완전한 배치값을 갖는다 (기본값 + 보정 방식 아님 — 그대로 적용됨)
[System.Serializable]
public struct PlacementCandidate
{
public string Name; // 인스펙터 식별용
public float Height; // 화자 발 기준 높이
public float Forward; // 화자가 바라보는 방향으로 띄울 거리
public float Lateral; // 좌우 오프셋 (+ 화자와 마주보는 시점 오른쪽)
public Vector3 RotationOffset; // 추가 회전 (오일러 각)
}
[Header("Auto Placement (후보 위치 자동 선택)")]
[Tooltip("대사창이 피해야 할 레이어 (벽/지형 = Default). NPC·플레이어 레이어는 넣지 말 것")]
[SerializeField] private LayerMask _obstacleMask = 1; // Default 레이어
[Tooltip("충돌 검사에 쓰는 대사창 절반 크기 — 실제 캔버스 크기에 맞출 것")]
[SerializeField] private Vector3 _panelHalfExtents = new(0.5f, 0.3f, 0.05f);
private Transform _speakerTransform;
private float _activeChestHeight;
private float _activeForwardOffset;
@@ -43,21 +66,37 @@ private void OnDestroy()
if (Instance == this) Instance = null;
}
// DialogHud 자체 기본 오프셋 사용
public void Show(CharacterData speaker, string text)
=> Show(speaker, text, _chestHeight, _forwardOffset, _lateralOffset, _rotationOffset);
// 배치 오프셋을 직접 넘겨 사용 (DialogPlayer가 NPC/씬별 값 전달)
public void Show(CharacterData speaker, string text, float chestHeight, float forwardOffset, float lateralOffset, Vector3 rotationOffset)
// 화자 옆에 대사 표시.
// - 배치는 화자 오브젝트의 DialogHudPlacement 값 사용, 없으면 이 컴포넌트의 기본값 폴백
// - speakerNameOverride가 비어있지 않으면 CharacterData.Name 대신 그 이름을 표시한다 (예: "???")
// - 화자의 DialogHudPlacement에 후보 목록이 있으면 벽/가림 검사를 통과하는 첫 후보 위치로 자동 배치
public void Show(CharacterData speaker, string text, string speakerNameOverride = null)
{
_speakerTransform = speaker != null ? CharacterVoiceObject.Find(speaker)?.transform : null;
_activeChestHeight = chestHeight;
_activeForwardOffset = forwardOffset;
_activeLateralOffset = lateralOffset;
_activeRotationOffset = rotationOffset;
var voiceObj = speaker != null ? CharacterVoiceObject.Find(speaker) : null;
_speakerTransform = voiceObj != null ? voiceObj.transform : null;
DialogHudPlacement placement = null;
if (voiceObj != null) voiceObj.TryGetComponent(out placement);
if (placement != null && placement.Candidates != null && placement.Candidates.Count > 0)
{
ApplyCandidate(placement.Candidates[0]); // 일단 첫 후보(기본)로 배치하고
ApplyBestCandidate(placement.Candidates); // 검사를 통과하는 후보가 있으면 교체
}
else
{
_activeChestHeight = _chestHeight;
_activeForwardOffset = _forwardOffset;
_activeLateralOffset = _lateralOffset;
_activeRotationOffset = _rotationOffset;
}
if (_speakerName != null)
_speakerName.text = speaker != null ? DialogVariables.Format(speaker.Name) : string.Empty;
{
string speakerName = !string.IsNullOrEmpty(speakerNameOverride) ? speakerNameOverride
: speaker != null ? speaker.Name : string.Empty;
_speakerName.text = DialogVariables.Format(speakerName); // {key} 토큰 치환
}
if (_dialogueText != null)
_dialogueText.text = DialogVariables.Format(text); // {key} 토큰 치환
@@ -72,6 +111,51 @@ public void Hide()
_speakerTransform = null;
}
// 선택된 후보의 배치값을 그대로 적용한다
private void ApplyCandidate(PlacementCandidate cand)
{
_activeChestHeight = cand.Height;
_activeForwardOffset = cand.Forward;
_activeLateralOffset = cand.Lateral;
_activeRotationOffset = cand.RotationOffset;
}
// 후보 위치들을 우선순위대로 검사해 통과하는 첫 후보를 적용한다. 전부 실패하면 첫 후보 유지.
// 검사 기준은 호출 시점의 화자→플레이어(카메라) 방향 — 대화가 시작되면 NPC가
// 플레이어 쪽으로 돌아서므로, 돌아선 뒤의 실제 배치와 일치한다.
private void ApplyBestCandidate(List<PlacementCandidate> candidates)
{
if (candidates == null || candidates.Count == 0) return;
if (_speakerTransform == null || Camera.main == null) return;
Vector3 camPos = Camera.main.transform.position;
Vector3 dir = camPos - _speakerTransform.position;
dir.y = 0f;
if (dir.sqrMagnitude < 0.0001f) return;
dir.Normalize();
Vector3 right = Vector3.Cross(dir, Vector3.up);
foreach (var cand in candidates)
{
Vector3 pos = _speakerTransform.position
+ Vector3.up * cand.Height
+ dir * cand.Forward
+ right * cand.Lateral;
Quaternion rot = Quaternion.LookRotation(-dir) * Quaternion.Euler(cand.RotationOffset);
// 벽/지형에 겹치는가
if (Physics.CheckBox(pos, _panelHalfExtents, rot, _obstacleMask, QueryTriggerInteraction.Ignore))
continue;
// 플레이어 눈에서 벽에 가려지는가
if (Physics.Linecast(camPos, pos, _obstacleMask, QueryTriggerInteraction.Ignore))
continue;
ApplyCandidate(cand);
return;
}
// 전부 실패 — 미리 적용해 둔 첫 후보(기본) 그대로 사용
}
private void LateUpdate()
{
if (_speakerTransform == null) return;