반응형

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);
}
반응형

+ Recent posts