반응형
JsonData를 사용하기 위해서는 먼저 에셋폴더에 플러그인 폴더에 LitJson파일을 복사해 넣어줘야 한다.
아래와같이 해주면 사용할 수 있다.
using LitJson;
//////////////////////////////////////
//// Resources.load 사용해서 불러올때. ////
//////////////////////////////////////
private JsonData JsonList;
IEnumerator Start ()
{
TextAsset t = (TextAsset) Resources.Load("listData", typeof(TextAsset));
JsonList = JsonMapper.ToObject( t ); // TextAsset사용시에는 .json 형태의 파일을 .txt확장명으로 바꿔줘야함.
Debug.Log ("text data - " + t.text);
yield return t;
ProcessList(t.text);
}
private void ProcessList(string jsonString)
{
JsonList = JsonMapper.ToObject(jsonString);
}
///////////////////////////
//// WWW 사용해서 불러올때. ////
///////////////////////////
//로컬파일 불러올때.
string url = "file:System OSX/Users/beom-seokjang/Documents/unity/24_MSC_Memento_prototype/Assets/Resources/listData.json";
//웹에 있는 파일 불러올때.
string url = "http://bamsiki.com/listData.json";
WWW www = new WWW(url);
//Load the data and yield (wait) till it's ready before we continue executing the rest of this method.
yield return www;
if (www.error == null)
{
//Sucessfully loaded the JSON string
Debug.Log("Loaded following JSON string" + www.data);
//Process books found in JSON file
ProcessList(www.data);
}
else
{
Debug.Log("ERROR: " + www.error);
}
반응형
'unity C#' 카테고리의 다른 글
[Unity] 리스트 재 정렬하기 list resorting (0) | 2015.10.26 |
---|---|
[Unity] C# Threading 사용하기 (0) | 2015.03.04 |
[Unity] RectTransform anchor 스크립트상에서 변경하기. (0) | 2015.02.14 |
[Unity] sublime 연동하기 (0) | 2015.02.11 |
[Unity] 코루틴(Coroutine)의 기본 개념 및 활용 (0) | 2015.02.04 |