json, xml ๋ฑ ๋ค์ํ ํํ์ ๋ฐ์ดํฐ ํฌ๋งท์ด ์๋ค.
๊ทธ ์ค ๋ณด๊ธฐ๋ ์ฒ๋ฆฌ๋ ํธํ Json ๋ฐ์ดํฐ ํฌ๋งท์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฒ์ ์์๋ณด์.
Json ํฌ๋งท์ ์ง์ํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ Unity ์์ ๊ธฐ๋ณธ์ผ๋ก ์ ๊ณตํ๋ JsonUtility ํด๋์ค, Newtonsoft์ Newtowsoft.json, Asset Store์์ ๋ค์ด๋ฐ์ ์ ์๋ JSON.NET(ํ์ฌ ์ง์์ข ๋ฃ) ๋ฑ์ด ์๋ค.
๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ณ๋ก ์ง์ํ๋ ๊ธฐ๋ฅ์ด ๋ค๋ฅธ๋ฐ, ๊ฐ๋จํ FromJson, ToJson ๊ธฐ๋ฅ๋ง ์ด์ฉํ๋ ค๋ฉด Unity ๊ธฐ๋ณธ JsonUtility ํด๋์ค๋ฅผ ์ด์ฉํ๋ฉด ๋๋ค.
Newtonsoft์ Newtowsoft.json ์ ์๋์ ๊ฐ์ด ์ฌ์ฉํ๋ฉฐ Dictionary ํํ์ ์๋ฃ๊ตฌ์กฐ๋ Serialize์ Deserialize ๊ธฐ๋ฅ์ ์ง์ํ๋ค. ๊ทธ์ธ์๋ [JsonProperty("๋ฐ์ดํฐ๋ช ")] ํค์๋๋ก ๋ฐ์ดํฐ ์ด๋ฆ์ ๋ฐ๊ฟ ์๋ ์๋ค.
string ObjectToJson(object obj)
{
return JsonConvert.SerializeObject(obj);
}
T JsonToOject<T>(string jsonData)
{
return JsonConvert.DeserializeObject<T>(jsonData);
}
string json = @"{
'href': '/account/login.aspx',
'target': '_blank'
}";
Dictionary<string, string> htmlAttributes = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
Console.WriteLine(htmlAttributes["href"]);
// /account/login.aspx
Console.WriteLine(htmlAttributes["target"]);
// _blank
[JsonProperty("regDt")] public DateTime Date { get; private set; }
Resource ํด๋์ ์๋ json ํ์ผ์ ์ฝ์ด์ค๋ ์ฝ๋๋ ์๋์ ๊ฐ๋ค.
using System.IO;
using UnityEngine;
namespace Main {
public class MainManager : MonoBehaviour {
private const string JSON_FILE = "data.json";
private void Start() {
var jsonFile = Resources.Load<TextAsset>(JSON_FILE);
MainData mainData = JsonUtility.FromJson<MainData>(jsonFile.ToString());
}
}
}
Json online editor ์ฌ์ดํธ: http://json.parser.online.fr/
Resource Load ํจ์๋ค: https://docs.unity3d.com/kr/current/ScriptReference/Resources.Load.html
'๐ฎ Unity Study > Unity' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Unity] Custom Unity Package in Git ๋งน๋ค๊ธฐ (0) | 2023.09.22 |
---|---|
[Unity] Url to Image (RawImage, UnityWebRequest, Couroutine, Action<Texture>) (0) | 2023.08.30 |
[Unity] ๋ณ๋ชฉํ์๊ณผ ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ (0) | 2023.08.22 |
[Unity] Garbage Collector (0) | 2023.08.21 |
[Unity] Asset, Library ์ ํ ๊ธฐ์ค (0) | 2023.08.19 |