μ λ²μλ μ λν°μμ κ²μ λ°μ΄ν°λ₯Ό JSON νμΌλ‘ μ μ₯ν΄λ΄€λ€.
μ΄λ²μλ μ μ₯ν JSON νμΌμ μ λν°λ‘ λΆλ¬μ€λ μμ
μ ν΄λ³΄μ.
Deserialization)μμ κΈμμ μ°λ¦¬λ κ²μ λ°μ΄ν° κ°μ²΄λ₯Ό JSON νμμΌλ‘ λ³νν΄λ΄€λ€.
κ·Όλ° μ΄μ μ μ₯ν JSON νμΌμ μ΄λ»κ² λΆλ¬μ€κ³ , κ·Έ νμΌμ λ΄μ©μ μ΄λ»κ² μ²λ¦¬ν΄μΌν κΉ?
κ°μ²΄λ₯Ό JSON νμΌλ‘ λ³νμμΌ°λ κ²μ²λΌ, JSON νμΌμ νμ©νκΈ° μν΄μ μ΄ λ°μ΄ν°λ₯Ό λ€μ κ°μ²΄λ‘ λ³νμμΌμΌνλ€. μ΄λ₯Ό μμ§λ ¬ν(Deserialization)λΌ νλ€.
λ€νν μ°λ¦¬κ° μΌλ JsonUtilityμμ JsonUtility.ToJsonλ©μλλ₯Ό ν΅ν΄ κ°λ¨ν κ°μ²΄λ₯Ό μ§λ ¬ν νλ κ²μ²λΌ λΌμ΄λΈλ¬λ¦¬ λ΄λΆμμ JsonUtility.FromJson λ©μλλ₯Ό μ¬μ©ν΄μ μμ§λ ¬ν κΈ°λ₯μ μ¬μ©ν μ μλ€.
κ·ΈλΌ μ΄μ JSON νμΌμ μμ§λ ¬ννλ μ½λλ₯Ό μ΄ν΄λ³΄μ.
string path = Path.Combine(Application.persistentDataPath, "playerData.json");
if (File.Exists(path))
{
string jsonData = File.ReadAllText(path);
}
λΉμ°ν λ°μ΄ν°λ₯Ό λΆλ¬μ€λ¬λ©΄ νμΌμ λ¨Όμ μ½μ΄μΌ νλ€. μ μ½λλ₯Ό λΆμν΄λ³΄λ©΄
Path.Combineμ ν΅ν΄μ path λ³μμ κ²½λ‘κ°μ ν λΉν΄μ€λ€.File.Existsλ₯Ό ν΅ν΄μ path κ²½λ‘μ νμΌμ΄ μ‘΄μ¬νλμ§ νμΈνλ€.File.ReadAllTextλ‘ νμΌ λ΄μ λͺ¨λ ν
μ€νΈμ κ°μ Έμμ jsonData λ³μμ ν λΉνλ€.μ, μ΄λ¬λ©΄ jsonDataλ³μμ νμΌ λ΄μ μλ λͺ¨λ κ°μ΄ μ μ₯μ΄ λλ€.
κ·ΈλΌ μ΄μ μ΄ ν
μ€νΈλ₯Ό μ΄λ»κ² μ²λ¦¬ν΄μΌ λ°μ΄ν° κ°μ²΄λ‘ λ³νμν¬ μ μμκΉ?
PlayerData playerData = JsonUtility.FromJson<PlayerData>(jsonData);
μ΄κ±΄ JSON κ°μ²΄λ‘ λ³νν λ JsonUtility.ToJsonμ μ¬μ©ν κ²κ³Ό μ μ¬νκ², JsonUtility.FromJson λ©μλλ₯Ό μ¬μ©νλ©΄ λλ€.
JsonUtility.FromJsonλ₯Ό ν΅ν΄μ jsonDataμ μ μ₯λ ν
μ€νΈ λ°μ΄ν°λ₯Ό PlayerData νμ
μ κ°μ²΄λ‘ λ³νν ν, playerData λ³μμ κ°μ μ μ₯ν΄μ€λ€.μ΄λ¬λ©΄ λ°μ΄ν°λ₯Ό λΆλ¬μ€λ κΈ°λ³Έ λ‘μ§μ΄ λλ¬λ€!
μ΄μ μμ λ‘μ§μ ν©μ³μ ν μ½λλ‘ λ§λ€λ©΄ μλμ²λΌ μΈ μ μλ€.
using System.IO;
using UnityEngine;
[System.Serializable]
public class PlayerData // μ μ₯ν λ°μ΄ν°κ° λͺ
μλ ν΄λμ€
{
public string name;
public int level;
public string[] items;
}
// λ°μ΄ν° λ‘λμ κ΄λ ¨λ λ©μλκ° μ μλ λ§€λμ ν΄λμ€
public class LoadManager : MonoBehaviour
{
// JSON νμΌμμ PlayerDataλ₯Ό λΆλ¬μ, λ°νν΄μ£Όλ λ©μλ
public PlayerData LoadData()
{
string path = Path.Combine(Application.persistentDataPath, "playerData.json");
if (File.Exists(path))
{
string jsonData = File.ReadAllText(path);
PlayerData data = JsonUtility.FromJson<PlayerData>(jsonData);
Debug.Log("λ°μ΄ν° λ‘λ© μ±κ³΅! // νλ μ΄μ΄ μ΄λ¦ : " + data.name);
return data;
}
else
{
Debug.LogWarning("μΈμ΄λΈ νμΌμ μ°Ύμ μ μμ!");
return null;
}
}
}
μ΄μ μλμ μμ λ₯Ό ν΅ν΄μplayerData.json νμΌμ μ μ₯ ν, λΆλ¬μ보μ.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestJson : MonoBehaviour
{
void Start()
{
PlayerData player = new PlayerData // λ°μ΄ν° κ°μ²΄ μμ±
{
name = "Player1",
level = 5,
items = new string[] { "sword", "shield" }
};
SaveManager saveManager = new SaveManager(); // SaveManager κ°μ²΄ μμ±
saveManager.SaveData(player); // λ°μ΄ν° μ μ₯
LoadManager loadManager = new LoadManager(); // LoadManager κ°μ²΄ μμ±
PlayerData playerData = loadManager.LoadData(); // λ°μ΄ν° λΆλ¬μ€κΈ°
printData(playerData); // λΆλ¬μ¨ λ°μ΄ν° μΆλ ₯
}
void printData(PlayerData playerData)
{
Debug.Log("name : " + playerData.name);
Debug.Log("level : " + playerData.level);
foreach(string item in playerData.items)
Debug.Log("item : " + item);
}
}


μ΄κ±Έλ‘ κ²μ μ μ₯ νμΌμ JSON νμμΌλ‘ μ μ₯νλ κ²μ μ΄μ΄μ λΆλ¬μ€λ κΈ°λ₯κΉμ§ ꡬνν΄λ΄€λ€.
μμ μμ μ½λλ₯Ό μ’ λ λ€λ¬μ μ½λλ₯Ό 첨λΆνκ³ κΈμ λ§μΉκ² λ€.
try-catch λ¬Έμ μ¬μ©ν΄ μμμΉ λͺ»ν μ€λ₯ μμΈμ²λ¦¬ifλ¬Έ ꡬ문 μ½κ° μμ (λ‘μ§μ λμΌν¨)using System.IO;
using UnityEngine;
public class LoadManager : MonoBehaviour
{
public PlayerData LoadData()
{
string path = Path.Combine(Application.persistentDataPath, "playerData.json");
if (!File.Exists(path))
{
Debug.LogWarning("μΈμ΄λΈ νμΌμ μ°Ύμ μ μμ΅λλ€. λν΄νΈ νμΌμ λΆλ¬μ΅λλ€.");
return new PlayerData { name = "DefaultPlayer", level = 1, items = new string[] { } };
}
try
{
string jsonData = File.ReadAllText(path);
PlayerData playerData = JsonUtility.FromJson<PlayerData>(jsonData);
Debug.Log("λ°μ΄ν° λ‘λ μ±κ³΅!");
return playerData;
}
catch (System.Exception e)
{
Debug.LogError("JSON νμΌμ λΆλ¬μ€λλ° μ€λ₯ λ°μ: " + e.Message);
return new PlayerData { name = "DefaultPlayer", level = 1, items = new string[] { } };
}
}
}
try-catchλ μ ν μκ° λͺ»νκ³ μμλ€μ. μ μ½κ³ κ°λλ€!