Unity/Csharp/Json/NewtonJson
提供: 初心者エンジニアの簡易メモ
NewtonJsonのdllをDL
- https://www.nuget.org/packages/Newtonsoft.Json をDLし、.nupkgを.zipに変更し、解凍。
- 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
WebGLで出力したとき
以下エラーが出た
an error occurred runnning the unity content on this page. see your browser javascript console for more info. the error was: uncaught abort(64) at error at jsStackTrack(Build.wasm.framework.unityweb:8:15602) at stackTrace [Object.stackTrace] (Build.wasm.framework.unityweb:8:15773) at object.onAbort(http://localhost:54514/Build/UnityLoader.js:4:11273)