Initial commit: Unity 프로젝트 셋업 + 다이얼로그 시스템 이전
This commit is contained in:
8
Assets/02_Scripts/UI/Communication.meta
Normal file
8
Assets/02_Scripts/UI/Communication.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ff2c4351197ddb44d957395fa78a377c
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
104
Assets/02_Scripts/UI/Communication/ChoiceHud.cs
Normal file
104
Assets/02_Scripts/UI/Communication/ChoiceHud.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
// World-space 선택지 UI 싱글턴.
|
||||
// DialogPlayer가 Show()를 await해서 선택된 인덱스를 받아감.
|
||||
// 배치(Placement)는 DialogHud가 담당하므로, 이 오브젝트를 DialogHud의 자식으로 두면 함께 따라온다.
|
||||
public class ChoiceHud : MonoBehaviour
|
||||
{
|
||||
public static ChoiceHud Instance { get; private set; }
|
||||
|
||||
[Header("Refs")]
|
||||
[SerializeField] private GameObject _root;
|
||||
[SerializeField] private Transform _rowContainer;
|
||||
[SerializeField] private DialogChoiceRow _rowPrefab;
|
||||
[SerializeField] private TMP_Text ChoiceQuestion;
|
||||
|
||||
private readonly List<DialogChoiceRow> _rows = new();
|
||||
private AwaitableCompletionSource<int> _completion;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null && Instance != this) { Destroy(gameObject); return; }
|
||||
Instance = this;
|
||||
if (_root == null) _root = gameObject;
|
||||
Hide();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (Instance == this) Instance = null;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
// 진행 중 대기 정리 (씬 전환 등으로 비활성화될 때)
|
||||
_completion?.TrySetCanceled();
|
||||
_completion = null;
|
||||
}
|
||||
|
||||
public async Awaitable<int> Show(string choiceQuestion, List<DialogChoice> choices)
|
||||
{
|
||||
if (choices == null || choices.Count == 0) return 0;
|
||||
|
||||
SetQuestion(choiceQuestion);
|
||||
ClearRows();
|
||||
for (int i = 0; i < choices.Count; i++)
|
||||
{
|
||||
var row = Instantiate(_rowPrefab, _rowContainer);
|
||||
row.Bind(i, DialogVariables.Format(choices[i].ChoiceText)); // {key} 토큰 치환
|
||||
row.OnClicked += HandleClicked;
|
||||
_rows.Add(row);
|
||||
}
|
||||
|
||||
_root.SetActive(true);
|
||||
_completion = new AwaitableCompletionSource<int>();
|
||||
|
||||
int result;
|
||||
try
|
||||
{
|
||||
result = await _completion.Awaitable;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_completion = null;
|
||||
Hide();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void HandleClicked(int index)
|
||||
{
|
||||
_completion?.TrySetResult(index);
|
||||
}
|
||||
|
||||
private void Hide()
|
||||
{
|
||||
SetQuestion(null);
|
||||
ClearRows();
|
||||
if (_root != null) _root.SetActive(false);
|
||||
}
|
||||
|
||||
// 질문이 비어 있으면 질문 텍스트 자체를 숨긴다. (기본값: 질문 없음 → 안 보임)
|
||||
// 질문은 보통 노드의 대사(TalkText)로 대신하므로 ChoiceQuestion은 비워둬도 된다.
|
||||
private void SetQuestion(string question)
|
||||
{
|
||||
if (ChoiceQuestion == null) return;
|
||||
string text = DialogVariables.Format(question); // {key} 토큰 치환
|
||||
bool hasQuestion = !string.IsNullOrWhiteSpace(text);
|
||||
ChoiceQuestion.text = hasQuestion ? text : string.Empty;
|
||||
ChoiceQuestion.gameObject.SetActive(hasQuestion);
|
||||
}
|
||||
|
||||
private void ClearRows()
|
||||
{
|
||||
foreach (var row in _rows)
|
||||
{
|
||||
if (row == null) continue;
|
||||
row.OnClicked -= HandleClicked;
|
||||
Destroy(row.gameObject);
|
||||
}
|
||||
_rows.Clear();
|
||||
}
|
||||
}
|
||||
2
Assets/02_Scripts/UI/Communication/ChoiceHud.cs.meta
Normal file
2
Assets/02_Scripts/UI/Communication/ChoiceHud.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 66c54b72d69da6b4e9c30ef648aade40
|
||||
26
Assets/02_Scripts/UI/Communication/DialogChoiceRow.cs
Normal file
26
Assets/02_Scripts/UI/Communication/DialogChoiceRow.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class DialogChoiceRow : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private TMP_Text _text;
|
||||
[SerializeField] private Button _button;
|
||||
|
||||
public event Action<int> OnClicked;
|
||||
|
||||
private int _index;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (_button != null)
|
||||
_button.onClick.AddListener(() => OnClicked?.Invoke(_index));
|
||||
}
|
||||
|
||||
public void Bind(int index, string text)
|
||||
{
|
||||
_index = index;
|
||||
if (_text != null) _text.text = text;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 63cb7682c2757f74cad43848f593838f
|
||||
94
Assets/02_Scripts/UI/Communication/DialogHud.cs
Normal file
94
Assets/02_Scripts/UI/Communication/DialogHud.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
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 (기본값 — Show에서 오프셋을 안 넘길 때 폴백)")]
|
||||
[SerializeField] private float _chestHeight = 1.2f; // 화자 발 기준 가슴 높이
|
||||
[SerializeField] private float _forwardOffset = 0.5f; // 화자→플레이어 방향으로 띄울 거리
|
||||
[SerializeField] private float _lateralOffset = 0f; // 좌우 오프셋 (+ 플레이어 시점 오른쪽)
|
||||
|
||||
private Transform _speakerTransform;
|
||||
private float _activeChestHeight;
|
||||
private float _activeForwardOffset;
|
||||
private float _activeLateralOffset;
|
||||
private bool _placed; // 처음 한 번만 배치하고 이후엔 고정
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null && Instance != this) { Destroy(gameObject); return; }
|
||||
Instance = this;
|
||||
Hide();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (Instance == this) Instance = null;
|
||||
}
|
||||
|
||||
// DialogHud 자체 기본 오프셋 사용
|
||||
public void Show(CharacterData speaker, string text)
|
||||
=> Show(speaker, text, _chestHeight, _forwardOffset, _lateralOffset);
|
||||
|
||||
// 배치 오프셋을 직접 넘겨 사용 (DialogPlayer가 NPC/씬별 값 전달)
|
||||
public void Show(CharacterData speaker, string text, float chestHeight, float forwardOffset, float lateralOffset)
|
||||
{
|
||||
_speakerTransform = speaker != null ? CharacterVoiceObject.Find(speaker)?.transform : null;
|
||||
_activeChestHeight = chestHeight;
|
||||
_activeForwardOffset = forwardOffset;
|
||||
_activeLateralOffset = lateralOffset;
|
||||
|
||||
if (_speakerName != null)
|
||||
_speakerName.text = speaker != null ? DialogVariables.Format(speaker.Name) : string.Empty;
|
||||
if (_dialogueText != null)
|
||||
_dialogueText.text = DialogVariables.Format(text); // {key} 토큰 치환
|
||||
|
||||
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;
|
||||
_placed = false; // 다음에 다시 뜰 때 재배치
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (_placed) 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 right = Vector3.Cross(dir, Vector3.up); // 플레이어 시점 기준 오른쪽(수평)
|
||||
Vector3 chestWorld = _speakerTransform.position + Vector3.up * _activeChestHeight;
|
||||
transform.position = chestWorld + dir * _activeForwardOffset + right * _activeLateralOffset;
|
||||
|
||||
// 빌보드 — 캔버스의 -Z(읽는 면)가 카메라를 향하도록 +Z를 카메라 반대로
|
||||
transform.rotation = Quaternion.LookRotation(-dir);
|
||||
|
||||
_placed = true; // 한 번 배치 후 고정
|
||||
}
|
||||
}
|
||||
2
Assets/02_Scripts/UI/Communication/DialogHud.cs.meta
Normal file
2
Assets/02_Scripts/UI/Communication/DialogHud.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8c591cc9e4d86544fa1f82ba1732b43f
|
||||
Reference in New Issue
Block a user