대화 프로토타입

This commit is contained in:
2026-06-19 17:09:36 +09:00
parent 3c8daa9c8d
commit 711c9a5986
24 changed files with 985 additions and 66 deletions

View File

@@ -0,0 +1,19 @@
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
[RequireComponent(typeof(DialogPlayer))]
public class DialogInteractable : MonoBehaviour
{
private DialogPlayer _player;
private void Awake()
{
_player = GetComponent<DialogPlayer>();
}
public void HandleActivated(ActivateEventArgs args)
{
if (_player == null || _player.IsPlaying) return;
_ = _player.Play();
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b05ae7fb07619844f8a7e36f20f64ba8

View File

@@ -33,11 +33,10 @@ private void Awake()
}
}
public void Play()
public async Awaitable Play()
{
if(_dialogGroups.Count > 0)
_ = Play(_dialogGroups[0].DialogGroupName);
}
public async Awaitable Play(string groupName)
@@ -71,6 +70,8 @@ public async Awaitable Play(string groupName)
finally
{
IsPlaying = false;
if (DialogHud.Instance != null)
DialogHud.Instance.Hide();
RestoreDefaultAnimations();
RestoreRotations();
}
@@ -133,8 +134,9 @@ private async Awaitable RotateToRotation(Transform target, Quaternion targetRota
private async Awaitable PlayNode(DialogNode node)
{
var speakerName = node.Speaker != null ? node.Speaker.Name : "(누구?)";
Debug.Log($"[{speakerName}] {node.TalkText}");
// 화자 옆 DialogHud에 대사 표시
if (DialogHud.Instance != null)
DialogHud.Instance.Show(node.Speaker, node.TalkText);
// 보이스 재생
if (node.Voice != null && node.Speaker != null)
@@ -182,7 +184,7 @@ private async Awaitable<int> WaitForChoice(DialogNode node)
Debug.LogWarning("[DialogPlayer] ChoiceHud 없음 — 0번 자동 선택");
return 0;
}
return await ChoiceHud.Instance.Show(node.Speaker, node.ChoiceQuestion ,node.Choices);
return await ChoiceHud.Instance.Show(node.ChoiceQuestion, node.Choices);
}

View File

@@ -92,10 +92,10 @@ public override void OnImportAsset(AssetImportContext ctx)
}
else
{
dn.ChoiceQuestion = GetInputPortValue<string>(gn.GetInputPortByName(DialogLineNode.PORT_QUESTION));
dn.ChoiceQuestion = GetInputPortValue<DialogText>(gn.GetInputPortByName(DialogLineNode.PORT_QUESTION)).Value;
for (int i = 0; i < choiceCount; i++)
{
var choiceText = GetInputPortValue<string>(gn.GetInputPortByName(DialogLineNode.ChoiceTextPort(i)));
var choiceText = GetInputPortValue<DialogText>(gn.GetInputPortByName(DialogLineNode.ChoiceTextPort(i))).Value;
var dest = GetConnectedNode(gn, DialogLineNode.ChoiceOutPort(i));
dn.Choices.Add(new DialogChoice
{

View File

@@ -61,11 +61,12 @@ protected override void OnDefinePorts(IPortDefinitionContext context)
}
// 가변 N지선다
context.AddInputPort<string>(PORT_QUESTION).WithDisplayName("Choice Question").Build();
// (string 포트는 GraphToolkit 기본 에디터의 IME 중복입력 버그가 있어 DialogText로 통일)
context.AddInputPort<DialogText>(PORT_QUESTION).WithDisplayName("Choice Question").Build();
for (int i = 0; i < choiceCount; i++)
{
context.AddInputPort<string>(ChoiceTextPort(i))
context.AddInputPort<DialogText>(ChoiceTextPort(i))
.WithDisplayName($"Choice {i + 1} Text")
.Build();
AddExecOutput(context, ChoiceOutPort(i), $"Choice {i + 1} →");

View File

@@ -2,8 +2,9 @@
using TMPro;
using UnityEngine;
// 화자 몸통 앞에 떠 있는 World-space 선택지 UI 싱글턴
// DialogPlayer가 Show()를 await해서 선택된 인덱스를 받아감
// World-space 선택지 UI 싱글턴.
// DialogPlayer가 Show()를 await해서 선택된 인덱스를 받아감.
// 배치(Placement)는 DialogHud가 담당하므로, 이 오브젝트를 DialogHud의 자식으로 두면 함께 따라온다.
public class ChoiceHud : MonoBehaviour
{
public static ChoiceHud Instance { get; private set; }
@@ -14,11 +15,6 @@ public class ChoiceHud : MonoBehaviour
[SerializeField] private DialogChoiceRow _rowPrefab;
[SerializeField] private TMP_Text ChoiceQuestion;
[Header("Placement")]
[SerializeField] private float _chestHeight = 1.2f; // 화자 발 기준 가슴 높이
[SerializeField] private float _forwardOffset = 0.5f; // 화자→플레이어 방향으로 띄울 거리
private Transform _speakerTransform;
private readonly List<DialogChoiceRow> _rows = new();
private AwaitableCompletionSource<int> _completion;
@@ -42,13 +38,11 @@ private void OnDisable()
_completion = null;
}
public async Awaitable<int> Show(CharacterData speaker,string choiceQuestion, List<DialogChoice> choices)
public async Awaitable<int> Show(string choiceQuestion, List<DialogChoice> choices)
{
if (choices == null || choices.Count == 0) return 0;
_speakerTransform = speaker != null ? CharacterVoiceObject.Find(speaker)?.transform : null;
ChoiceQuestion.text = choiceQuestion;
SetQuestion(choiceQuestion);
ClearRows();
for (int i = 0; i < choices.Count; i++)
{
@@ -81,10 +75,19 @@ private void HandleClicked(int index)
private void Hide()
{
ChoiceQuestion.text = "";
SetQuestion(null);
ClearRows();
if (_root != null) _root.SetActive(false);
_speakerTransform = null;
}
// 질문이 비어 있으면 질문 텍스트 자체를 숨긴다. (기본값: 질문 없음 → 안 보임)
// 질문은 보통 노드의 대사(TalkText)로 대신하므로 ChoiceQuestion은 비워둬도 된다.
private void SetQuestion(string question)
{
if (ChoiceQuestion == null) return;
bool hasQuestion = !string.IsNullOrWhiteSpace(question);
ChoiceQuestion.text = hasQuestion ? question : string.Empty;
ChoiceQuestion.gameObject.SetActive(hasQuestion);
}
private void ClearRows()
@@ -97,24 +100,4 @@ private void ClearRows()
}
_rows.Clear();
}
private void LateUpdate()
{
if (_root == null || !_root.activeSelf) return;
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;
_root.transform.position = chestWorld + dir * _forwardOffset;
// 빌보드 — 캔버스의 -Z(읽는 면)가 카메라를 향하도록 +Z를 카메라 반대로
_root.transform.rotation = Quaternion.LookRotation(-dir);
}
}

View File

@@ -0,0 +1,76 @@
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);
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 8c591cc9e4d86544fa1f82ba1732b43f