[๊ตฌํ] json ์ด์ฉ ์ ๋ํฐ ๋ด ๋ํ ๊ฐ๋จํ๊ฒ ๊ตฌํํ๊ธฐ 2
1. json ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
์ฐ๋ฆฌ ๊ฒ์์์๋ ์ธ์ด ๋ณ๊ฒฝํ๋ ์ฝ๋์์ ๋ถ๋ฌ์๋ค.
์์ด์ ํ๊ตญ์ด, ์ด 2๊ฐ๊ตญ์ด๋ฅผ ์๋น์ค ํ๊ณ ์๋ค.
public class LanguageSaver : MonoBehaviour
{
...
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
...
if(SceneManager.GetActiveScene().buildIndex != 0)
{
DatabaseManager dbManager = FindObjectOfType<DatabaseManager>();
DialogueParser theParser = FindObjectOfType<DialogueParser>();//parser์ ์ธ
Dialogue[] dialogues;
if (languageIndex == 0)
{
dbManager.en = false;
}
else if (languageIndex == 1)
{
dbManager.en = true;
}
if (dbManager.en)
dialogues = theParser.Parse("Dialogue_EN");
else
dialogues = theParser.Parse("Dialogue_KR");
for (int i = 0; i < dialogues.Length; i++)
{
dbManager.dialogueDic.Add(i, dialogues[i]);
}
}
}
...
}
์ด ์ฝ๋๋ DontDestroyOnLoad๋ก ๋์ด์์ด์ ํ๋ฒ ๋ฐฐ์ด์ ๋ง๋ค๋ฉด ๊ณ์ ๊ฐ์ ธ๋ค ์ธ ์ ์๋ค.
DatabaseManager๋ผ๋ ์คํฌ๋ฆฝํธ์ en์ด๋ผ๋ ๋ณ์๊ฐ true๋ฉด ์์ด, flase๋ฉด ํ๊ตญ์ด๋ก ํด๋์๋ค.
(๋ณด๊ธฐ ๋ถํธํ๊ฒ ์๊ทธ๋ฌ์ง...)
(์ด ๋ณ์๋ ์คํฌ๋ฆฝํธ ์์น๋ ์ ๊ฒฝ ์ธ ํ์ ์๊ณ ์ด๋์ ์๋ ๋ํ ์ธ์ด ์ข ๋ฅ ๋ณ์์ด๊ธฐ๋ง ํ๋ฉด ๋๋ค.)
DialogueParser๋ผ๋ ์คํฌ๋ฆฝํธ์ Parseํจ์์ ํด๋น ์ธ์ด๋ผ๋ฉด ๋ถ๋ฌ์์ผ ํ๋ jsonํ์ผ์ ์ด๋ฆ์ ๋ฃ์ด์ค๋ค.
(์์ด๋ฒ์ ์ Dialogue_EN.json, ํ๊ตญ์ด๋ฒ์ ์ Dialogue_KR.json์ด๋ค)
๊ทธ๋ ค๋ฉด theParser.Parse๋ ํด๋น ์ธ์ด์ ๋ชจ๋ ๋ํ ๋ด์ฉ์ Dialogue(์ด๋ฆ, ํด๋น ์ด๋ฆ์ ๋ํ๋ด์ฉ ๋ฆฌ์คํธ) ๋ฐฐ์ด์ ๋ฐํํด์ค๋ค.
Dialogue๋ ๋ํ ์๋์ ์ด ์๋์ ๋๋ ๋ํ ๋ฆฌ์คํธ๊ฐ ๋ค์ด์๋ ๋ฐฐ์ด์ด๋ค.
public class Dialogue
{
public string name;
public string[] contexts;
}
๊ฐ๋จํ๊ฒ DialogueParser๋ก ๋์ฌ ๋ด์ฉ์ ๋ฐ์์์ DatabaseManager๋ก ๋นผ๋ค ์ด๋ค๊ณ ์๊ฐํ๋ฉด ๋๋ค.
2. DialogueParser : jsonํ์ผ ๋ฐฐ์ด๋ง๋ค๊ธฐ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
public class DialogueParser : MonoBehaviour
{
public Dialogue[] Parse(string _FileName)
{
List<Dialogue> dialogueList = new List<Dialogue>();//๋์ฌ ๋ฆฌ์คํธ
TextAsset dialogueString = Resources.Load<TextAsset>(_FileName);
JsonData dialogueData = JsonMapper.ToObject(dialogueString.ToString());
for (int i = 0; i < dialogueData.Count;i++)
{
Dialogue dialogue = new Dialogue();
dialogue.name = dialogueData[i]["name"].ToString();
List<string> contextList = new List<string>();
for(int j = 0; j < dialogueData[i]["lines"].Count; j++)
{
contextList.Add(dialogueData[i]["lines"][j]["line"].ToString());
}
dialogue.contexts = contextList.ToArray();
dialogueList.Add(dialogue);
}
return dialogueList.ToArray();//๊ฐ ์บ๋ฆญํฐ์ ๋์ฌ๋ค ๋ฐฐ์ด๋ก ๋ฆฌํด
}
}
๋ฐ๋ผ ์ฝ์ด๋ณด๋ฉด ์ค๋ช ์์ด ์ถฉ๋ถํ ์ดํด ๊ฐ๋ฅํ ์ฝ๋๋ผ๊ณ ์๊ฐํ๋ค!
FileName์ ์๊น ๋ฐ์ Dialogue_EN, Dialogue_KR ์ด๊ฑฐ๋ค.
LitJson์ ์ฌ์ฉํ๋ค. Unity์์ ๊ธฐ๋ณธ์ผ๋ก ์ ๊ณตํด์ฃผ๋ JsonUtility์ ๋น์ทํ ๊ฒ์ธ๋ฐ
๋์ค์ ์ค๋ช ํ ๊ธฐํ๊ฐ ์๊ธฐ๋ฉด ์ค๋ช ํ ๊ฒ์ด๋ค. ์ผ๋จ ๊ถ๊ธํ๋ฉด ๊ตฌ๊ธ๋ง ํด๋ณด์ธ์ฌ
JsonData๋ก ๋ฐ์ ๋ฐฐ์ด์ ํ๋์ฉ ๋ฏ์ด ๋ํ๋ณ(๋ง๋๋ณ XX ๋ง๋๋ List<string>์ผ๋ก ๋ฌถ์ด๋ฒ๋ฆฐ๋ค.)๋ก List<Dialogue>ํํ๋ก ๋ง๋ค์ด์ฃผ๋ ๊ฒ์ด๋ค.
3. DatabaseManager : index๋ก ๋์ฌ ์กฐํ
public class DatabaseManager : MonoBehaviour
{
public bool en = false;//์์ด ์ฌ๋ถ
public Dictionary<int, Dialogue> dialogueDic = new Dictionary<int, Dialogue>();
...
public string GetDialogue(int id, int index)
{
if (index == dialogueDic[id - 1].contexts.Length)
return null;
else
return dialogueDic[id - 1].contexts[index];
}
}
์์์ ๋ง๋ค์ด์ง ๋ฐฐ์ด์ index๋ก ์กฐํ ๋ฑ ๋๊ฒ index, Dialgoueํํ์ Dictionary์ ์ต์ข ์ ์ฅํด์ค๋ค.
(์ฝ๋ ๋ถ๋ฆฌ, ์กฐํ ํธ๋ฆฌ์ฑ ๋๋ฌธ์ ์ด๋ ๊ฒ ํ์ง๋ง ์ ์ ์ฝ๋์์ ๋ฐ๋ก Dictionary์ ๊ฝ๋์ง ํด๋ ๋ ๊ฒ ๊ฐ๊ธด ํ๋ค.)
์ด์ GetDialogue๋ก (์ฌ๊ธฐ์ id๊ฐ jsonํ์ผ index๋ฅผ ์๋ฏธํ๊ณ index๋ ์ด ๋ํ ๋ช๋ฒ ์งธ ๋ง๋์ธ์ง๋ฅผ ์๋ฏธํ๋ค ์ฌ๊ธฐ์ ๋ณ์ ์ด๋ฆ์ด ๋ ์์๊ตฐ...ใ ใ ) ๋ช ๋ฒ์งธ ๋ํ(id)์ ๋ช ๋ฒ์งธ ๋ง๋(index)์ธ์ง ๋ฑ ๋ฑ ์ซ์ ๋ฃ์ด์ ๋ฑ ๋ถ๋ฌ์ฃผ๋ฉด ๋์ด๋ค!
์ด ๋ถ๋ฅด๋ ๊ฒ์ ๋ค์ ํฌ์คํ ์ผ๋ก ๋๊ธฐ๊ฒ ๋ค... ๊ตฌ๋ผ ์๋
๊ทธ๋ฆฌ๊ณ ์ด๊ฑฐ ์ฒ์ ๊ตฌํํ ๋ ์ด ์์์ ๋ง์ด ์ฐธ๊ณ ํ ๊ฒ ๊ฐ๋ค.
๋ํํ
ํ์ ์๋ ๊ธฐ๋ฅ ๋นผ๊ณ ๊ทธ๋ฌ๋ค ๋ณด๋
๋ชป ์์๋ณผ ์ ๋๋ก ์ฝ๋๊ฐ ๊ต์ฅํ ๊ฐ๋จํด์ง๊ณ ๋ง์ด ๋ฐ๋๊ธด ํ๋ค.
https://www.youtube.com/watch?v=N4fudqV49TU