facebook twitter hatena line email

Unity/Csharp/Json/NewtonJson

提供: 初心者エンジニアの簡易メモ
2021年7月5日 (月) 16:56時点におけるAdmin (トーク | 投稿記録)による版

移動: 案内検索

NewtonJsonのdllをDL

  1. https://www.nuget.org/packages/Newtonsoft.Json をDLし、.nupkgを.zipに変更し、解凍。
  2. lib\netstandard2.0の下のNewtonsoft.Json.dllをPluginsの下に設置

サンプル

using UnityEngine;
using UnityEngine.UI;
using Newtonsoft.Json;
using System;
public class SampleScene : MonoBehaviour
{
    [Serializable]
    public class Item
    {
        public string Name;
        public int Value;
    }
    void Start()
    {
        GameObject.Find("Button").GetComponent<Button>().onClick.AddListener(OnClick);
    }
    void OnClick()
    {
        Item itemSource = new Item();
        itemSource.Name = "aaa";
        itemSource.Value = 111;
        string json = JsonConvert.SerializeObject(itemSource);
        Item item = JsonConvert.DeserializeObject<Item>(json);
        Debug.Log("name=" + item.Name + " value=" + item.Value);
        GameObject.Find("Text").GetComponent<Text>().text = "name=" + item.Name + " value=" + item.Value;
    }

ログ:name=aaa value=111

参考

https://qiita.com/safu9/items/b9f654a5083d794442a2

https://spi8823.hatenablog.com/entry/2016/04/16/001641