반응형
선택지가 있는 대화 시스템은 계층 구조로 대화를 설계해야합니다.
대화 시스템 설계 로직
[Serializable]
public class PlayerResponse
{
public DialogDataSO nextDialogue;
[TextArea(3, 10)]
public string playerText;
}
public class DialogDataSO : ScriptableObject
{
[TextArea(5, 20)]
public string npcText;
public List<PlayerResponse> answerList = new List<PlayerResponse>();
}
DialogDataSO는 하나의 대화를 의미합니다. (NPC의 말과 플레이어의 응답)
그리고 플레이어의 응답에는 다음에 실행될 대화를 넣게됩니다.
void UpdateDialogUI(DialogDataSO dialog)
{
if (dialog == null)
{
CloseUI();
return;
}
npcText.text = dialog.npcText;
for (int i = 0; i < dialog.response.Count; i++)
{
int idx = i;
playerAnswers[i].GetComponent<Button>().onClick.RemoveAllListeners();
playerAnswers[i].GetComponent<Button>().onClick.AddListener(() =>
{
UpdateDialogUI(dialog.response[idx].nextDialogue);
});
playerAnswers[i].GetComponentInChildren<Text>().text = dialog.response[i].playerText;
playerAnswers[i].SetActive(true);
}
for (int i = dialog.response.Count; i < playerAnswers.Count; i++)
playerAnswers[i].SetActive(false);
}
UI에서 적용되는 코드입니다.
NPC Text를 출력하고 플레이어의 응답들을 버튼에 담아서 표시합니다.
그리고 각 버튼의 onClick. AddListner를 통해 UpdateDialogUI가 재귀 호출되도록 합니다.
이런식의 계층적 설계를 통해 대화 시스템의 뼈대를 만들 수 있습니다.
이후에 특정 대화를 종료 하면 퀘스트가 등록 되거나, NPC의 상태에 따라 다른 대화가 실행되게 하는 방식으로 확장이 가능합니다.
반응형
LIST
'Unity' 카테고리의 다른 글
Unity 3D TPS 카메라 컨트롤러 구현 (0) | 2025.03.05 |
---|---|
유니티 무한 로딩 오류 (0) | 2025.02.20 |
유니티 GoogleAdmob 보상형 광고 로드 최적화 (0) | 2025.01.08 |
Unity Shader Graph 이용한 외곽선 생성법 (0) | 2025.01.07 |
Unable to load DLL 'native-googlesignin' 해결법 (0) | 2024.12.20 |