JSON 예시
먼저, 대화와 선택지의 구조를 정의하여 JSON 파일로 변환해보겠습니다.
{
"dialogues": [
{
"character": "닉넴",
"text": "대사!"
},
{
"character": "닉넴",
"text": "대사..."
},
{
"character": "닉넴",
"text": "대사!"
},
{
"character": "닉넴",
"text": "대사..."
},
{
"character": "닉넴",
"text": "..."
},
{
"character": "닉넴",
"text": "대사?"
},
{
"character": "닉넴",
"text": "대사."
},
{
"character": "닉넴",
"text": "대사.",
"choices": [
{
"option": "1: 선택.",
"nextDialogue": 10
},
{
"option": "2: 선택.",
"nextDialogue": 11
}
]
},
{
"character": "마스터",
"text": "대사."
},
{
"character": "닉넴",
"text": "대사?"
},
{
"character": "닉넴",
"text": "대사."
},
{
"character": "닉넴,
"text": "대사!"
},
{
"character": "닉넴",
"text": "대사."
},
{
"character": "닉넴",
"text": "대사."
}
]
}
JSON 구조 설명
- dialogues: 대화의 배열이며, 각 항목은 하나의 대화를 나타냅니다.
- character: 대화를 하는 캐릭터의 이름.
- text: 캐릭터가 말하는 대사.
- choices: 선택지가 필요한 대화에는 선택지 배열이 추가됩니다.
- option: 선택지에 표시될 텍스트.
- nextDialogue: 선택 후 진행될 대사의 인덱스를 나타냅니다.
Unity에서 JSON 데이터 불러오기
위 JSON 파일을 Unity에서 불러와 사용할 수 있도록 기존 DialogueManager를 확장하면, 선택지 처리가 가능합니다.
DialogueManager 수정
[System.Serializable]
public class Dialogue
{
public string character;
public string text;
public Choice[] choices; // 선택지가 있는 경우 처리
}
[System.Serializable]
public class Choice
{
public string option;
public int nextDialogue; // 선택지 선택 후 이동할 대사의 인덱스
}
[System.Serializable]
public class DialogueList
{
public Dialogue[] dialogues;
}
public class DialogueManager : MonoBehaviour
{
public Text characterNameText;
public Text dialogueText;
public GameObject choicesPanel; // 선택지 버튼들을 담는 패널
public Button choiceButtonPrefab; // 선택지 버튼 프리팹
private DialogueList dialogueList;
private int currentDialogueIndex = 0;
void Start()
{
LoadDialogueData();
DisplayNextDialogue();
}
void LoadDialogueData()
{
TextAsset jsonData = Resources.Load<TextAsset>("dialogue");
dialogueList = JsonUtility.FromJson<DialogueList>(jsonData.text);
}
public void DisplayNextDialogue()
{
if (currentDialogueIndex < dialogueList.dialogues.Length)
{
Dialogue dialogue = dialogueList.dialogues[currentDialogueIndex];
characterNameText.text = dialogue.character;
dialogueText.text = dialogue.text;
if (dialogue.choices != null && dialogue.choices.Length > 0)
{
ShowChoices(dialogue.choices);
}
else
{
HideChoices();
currentDialogueIndex++;
}
}
else
{
Debug.Log("모든 대화를 마쳤습니다.");
}
}
void ShowChoices(Choice[] choices)
{
choicesPanel.SetActive(true);
foreach (Transform child in choicesPanel.transform)
{
Destroy(child.gameObject); // 기존 선택지 삭제
}
foreach (Choice choice in choices)
{
Button choiceButton = Instantiate(choiceButtonPrefab, choicesPanel.transform);
choiceButton.GetComponentInChildren<Text>().text = choice.option;
choiceButton.onClick.AddListener(() => OnChoiceSelected(choice.nextDialogue));
}
}
void HideChoices()
{
choicesPanel.SetActive(false);
}
void OnChoiceSelected(int nextDialogueIndex)
{
currentDialogueIndex = nextDialogueIndex;
DisplayNextDialogue();
}
}
Unity UI 설정
- Text UI: 대화와 캐릭터 이름을 표시할 텍스트 UI를 설정합니다.
- Button Prefab: 선택지를 표시할 버튼 프리팹을 준비하고, Button 컴포넌트를 활용해 선택지 버튼을 생성합니다.
- Choices Panel: 선택지 버튼들이 담길 패널을 준비합니다.
요약
- 대화 데이터를 JSON 파일로 저장하고, 캐릭터 대사와 선택지를 구조화합니다.
- Unity에서 JSON 파일을 불러와 캐릭터의 대사를 출력하고, 선택지에 따라 다음 대사로 넘어가는 로직을 구현합니다.
- 선택지가 있는 대사는 선택 버튼을 생성하여 사용자가 선택할 수 있도록 합니다.
이 방법으로 게임 내 대화와 선택지를 처리하고, 히든 엔딩 등의 요소를 구현할 수 있습니다.
'개인 프로젝트' 카테고리의 다른 글
Text UI에 대사 추가해서 Prefab (2) | 2024.09.16 |
---|---|
유니티 UI에서 채팅형식으로 보여주려면? (0) | 2024.09.15 |
json으로 대사처리 (2) | 2024.09.15 |
다른 데이터베이스 연동 (1) | 2024.09.14 |
데이터베이스 SQLite 사용 (2) | 2024.09.14 |