À¯´ÏƼ 5.3 ÀÌ»óºÎÅÍ JsonUtility¸¦ Áö¿øÇÑ´Ù. ¿ÜºÎ JSON ¶óÀ̺귯¸®°¡ ÇÊ¿ä ¾ø´Ù. < JsonUtility ¸â¹ö ÇÔ¼ö >
1. ±âº» »ç¿ë¹ýEnemy¿¡¼´Â publicÀ» »ç¿ëÇÏÁö ¾Ê°í SerializeField¸¦ ÀÌ¿ëÇß´Ù.Äڵ尡 º¹ÀâÇÏÁö ¾Ê±â ¶§¹®¿¡ ÀÚ¼¼ÇÑ ¼³¸íÀº »ý·«ÇÑ´Ù. using UnityEngine;
using System; using System.Collections.Generic; [Serializable] public class Enemy { [SerializeField] string name; [SerializeField] List<string> skills; public Enemy(string name, List<string> skills) { this.name = name; this.skills = skills; } } //Test string jsonStr = JsonUtility.ToJson(new Enemy("¿À°Å", new List<string>() { "¹°¸®°ø°Ý", "¸¶¹ý" })); Debug.Log(jsonStr); Enemy enemy0 = JsonUtility.FromJson<Enemy>(jsonStr); Enemy enemy1 = new Enemy("empty", new List<string>() { }); JsonUtility.FromJsonOverwrite(jsonStr, enemy1); °á°ú) jsonStrÀÇ Ãâ·Â °á°ú´Â ´ÙÀ½°ú °°´Ù. {"name":"¿À°Å","skills":["¹°¸®°ø°Ý","¸¶¹ý"]} enemy1 °ªÀÌ ¿À°Å, ¹°¸®°ø°Ý, ¸¶¹ýÀ¸·Î º¯°æµÈ´Ù. 2. List Á÷·ÄÈ´ÙÀ½°ú °°ÀÌ List¸¦ JsonÀ¸·Î º¯È¯ÇÏ¸é ºó ¹®ÀÚ¿¸¸ º¯È¯ÇÑ´Ù. var enemies = new
List<Enemy>();
enemies.Add(new Enemy("¿À°Å", new List<string>() { "¹°¸®°ø°Ý", "¸¶¹ý" })); enemies.Add(new Enemy("Æ®·Ñ", new List<string>() { "°ø°Ý", "ȸº¹", "ºÎÈ°" })); string emptyStr = JsonUtility.ToJson(enemies); List´Â º°µµ·Î Á÷·ÄÈ ½ÃÄÑ ÁÖ¾î¾ß ÇÑ´Ù. [Serializable]
public class Serialization<T> { [SerializeField] List<T> target; public List<T> ToList() { return target; } public Serialization(List<T> target) { this.target = target; } } //Test var enemies = new List<Enemy>(); enemies.Add(new Enemy("¿À°Å", new List<string>() { "¹°¸®°ø°Ý", "¸¶¹ý" })); enemies.Add(new Enemy("Æ®·Ñ", new List<string>() { "°ø°Ý", "ȸº¹", "ºÎÈ°" })); string str = JsonUtility.ToJson(new Serialization<Enemy>(enemies)); Debug.Log(str); List<Enemy> retEnemies = JsonUtility.FromJson<Serialization<Enemy>>(str).ToList(); °á°ú) strÀÇ Ãâ·Â °á°ú´Â ´ÙÀ½°ú °°´Ù. {"target":[{"name":"¿À°Å","skills":["¹°¸®°ø°Ý","¸¶¹ý"]},{"name":"Æ®·Ñ","skills":[" °ø°Ý","ȸº¹","ºÎÈ°"]}]} retEnemies¿¡ °ªÀ» ³Ö´Â´Ù. 3. Dictionary Á÷·ÄÈ[Serializable]
public class Serialization<TKey, TValue> : ISerializationCallbackReceiver { [SerializeField] List<TKey> keys; [SerializeField] List<TValue> values; Dictionary<TKey, TValue> target; public Dictionary<TKey, TValue> ToDictionary() { return target; } public Serialization(Dictionary<TKey, TValue> target) { this.target = target; } public void OnBeforeSerialize() { keys = new List<TKey>(target.Keys); values = new List<TValue>(target.Values); } public void OnAfterDeserialize() { var count = Math.Min(keys.Count, values.Count); target = new Dictionary<TKey, TValue>(count); for (var i = 0; i < count; ++i) { target.Add(keys[i], values[i]); } } } //Test var enemies = new Dictionary<int, Enemy>(); enemies.Add(1000, new Enemy("¿À°Å", new List<string>() { "¹°¸®°ø°Ý", "¸¶¹ý" })); enemies.Add(1001, new Enemy("Æ®·Ñ", new List<string>() { "°ø°Ý", "ȸº¹", "ºÎÈ°" })); string str = JsonUtility.ToJson(new Serialization<int, Enemy>(enemies)); Debug.Log(str); Dictionary<int, Enemy> retEemies = JsonUtility.FromJson<Serialization<int, Enemy>>(str).ToDictionary(); °á°ú) strÀÇ Ãâ·Â °á°ú´Â ´ÙÀ½°ú °°´Ù. {"keys":[1000,1001],"values":[{"name":"¿À°Å","skills":["¹°¸®°ø°Ý","¸¶¹ý"]}, {"name":"Æ®·Ñ","skills":["°ø°Ý","ȸº¹","ºÎÈ°"]}]} ISerializationCallbackReceiver·Î Á÷·ÄÈ ±â´ÉÈ®ÀåUnity 5.3¿¡¼ "UnityEngine.ISerializationCallbackReceiver"°¡ Ãß°¡ µÇ¾ú´Ù.ISerializationCallbackReceiver¸¦ »ó¼ÓÇϸé Á÷·ÄÈ(ToJson)Àü¿¡ OnBeforeSerialize°¡ È£ÃâµÇ°í ¿ªÁ÷·ÄÈ(FromJson)Àü¿¡ OnAfterDeserialize°¡ È£ÃâµÈ´Ù. public class Character : ISerializationCallbackReceiver
{ public void OnBeforeSerialize () { Debug.Log ( "OnBeforeSerialize" ); } public void OnAfterDeserialize () { Debug.Log ( "OnAfterDeserialize" ); } } ÂüÁ¶) http://kou-yeung.hatenablog.com/entry/2015/12/31/014611 https://docs.unity3d.com/ScriptReference/JsonUtility.html https://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.html |