Unity 5.3 버전에서의 변경 사항 중에는 JSON API의 추가가 포함되어 있습니다. (http://blogs.unity3d.com/kr/2015/12/08/unity-5-3-all-new-features-and-more-platforms/)
이전 버전의 유니티에서는 JSON을 사용하려면 외부 라이브러리를 사용하여야 했는데,
각각의 JSON 라이브러리는 iOS/IL2CPP와 호환이 좋지 않은 것들도 있었으며, 게임에 추가한 외부 플러그인에서 JSON 라이브러리를 사용하는 경우도 있어 실제 게임 앱은 하나인데 그 안에 JSON 라이브러리만 2~3종류가 들어가있는 경우도 종종 있었습니다.
유니티에서는 아마 이러한 문제점들을 파악하고 이를 위한 해결책으로 Unity 자체적으로 JSON 라이브러리를 제공하게 된 것 같습니다.
유니티의 새로운 API인 Json Utility는 매우 쓰기 쉽고 기본적인 기능만 제공하지만 게임에서 쓰기에는 기능이 충분합니다. 여기서는 간단한 JsonUtility의 사용법들을 알아보도록 하겠습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
[Serializable] | |
class Data{ | |
public string name; | |
public List<string> likes; | |
public int level; | |
} | |
public class JsonTest : MonoBehaviour { | |
void Start () { | |
var data = new Data (); | |
data.name = "Park"; | |
data.level = 10; | |
data.likes = new List<string> () { | |
"dog", "cat" | |
}; | |
/* | |
{ | |
"name": "Park", | |
"likes": [ | |
"dog", | |
"cat" | |
], | |
"level": 10 | |
} | |
*/ | |
Debug.Log (JsonUtility.ToJson (data, prettyPrint: true)); | |
} | |
void Update () { | |
} | |
} |
이렇게 게임에서 사용하는 세이브 데이터 등을 Json으로 저장할 수 있습니다.
주의할점은 public field만 변환됩니다. public property는 변환되지 않습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
[Serializable] | |
class Data{ | |
public string name; | |
public List<string> likes; | |
public int level; | |
} | |
public class FromJsonTest : MonoBehaviour { | |
void Start () { | |
var json = @" | |
{ | |
""name"": ""Park"", | |
""likes"": [ | |
""dog"", | |
""cat"" | |
], | |
""level"": 10 | |
} | |
"; | |
var data = JsonUtility.FromJson<Data> (json); | |
Debug.Log (data.name); // Park | |
Debug.Log (data.level); // 10 | |
// Dog | |
// Cat | |
foreach (var v in data.likes) { | |
Debug.Log (v); | |
} | |
} | |
} |
제너릭으로 오브젝트의 타입을 지정합니다. Dictionary<string, object> 타입은 동작하지 않는 것 처럼 보입니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
[Serializable] | |
class Data{ | |
public string name; | |
public List<string> likes; | |
public int level; | |
} | |
public class FromJsonOverrideTest : MonoBehaviour { | |
void Start () { | |
var data = new Data (); | |
data.name = "Park"; | |
data.level = 5; | |
data.likes = new List<string> () { "chicken" }; | |
var json = @" | |
{ | |
""level"": 99 | |
} | |
"; | |
JsonUtility.FromJsonOverwrite(json, data); | |
Debug.Log (data.name); // Park | |
Debug.Log (data.level); // 99 | |
// chicken | |
foreach (var v in data.likes) { | |
Debug.Log (v); | |
} | |
} | |
} |
'C# > Unity' 카테고리의 다른 글
[Unity] Unhandled Exception 처리하기 (0) | 2016.06.15 |
---|---|
[Unity] ISO LanguageCode 가져오기 (0) | 2016.06.09 |
[Unity] IL2CPP 에서 MissingMethodException 가 발생할 때 (0) | 2016.04.14 |
[IL2CPP] Assembly.GetCallingAssembly 가 동작하지 않을 때 (0) | 2016.01.26 |
[Unity] Android keystore 경로 상대경로로 지정하기 (1) | 2016.01.20 |