Unity/수업 내용

[Unity 2020.3] JSON .NET For Unity, json 불러와서 사용하기

JSH1 2021. 10. 26. 17:46

https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347

 

JSON .NET For Unity | 입출력 관리 | Unity Asset Store

Get the JSON .NET For Unity package from parentElement, LLC and speed up your game development process. Find this & other 입출력 관리 options on the Unity Asset Store.

assetstore.unity.com

 


 

 

using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System.Linq;

public class JsonSingleton
{
    private static JsonSingleton instance;

    private JsonSingleton() { }

    public static JsonSingleton Instance
    {
        get
        {
            if (instance == null)
                instance = new JsonSingleton();
            return instance;
        }
    }

    Dictionary<int, ItemData> dicItemDatas = new Dictionary<int, ItemData>();

    public void LoadData()
    {
        TextAsset json = Resources.Load<TextAsset>("Data/item_table");
        dicItemDatas = JsonConvert.DeserializeObject<ItemData[]>(json.text).ToDictionary(x => x.id);

        Debug.Log(dicItemDatas.Count);
        Debug.Log(dicItemDatas[100].name);
        Debug.Log(dicItemDatas[101].name);
        Debug.Log(dicItemDatas[102].name);
    }
}

 

JsonSingleton.Instance.LoadData();