facebook twitter hatena line email

「Unity/Csharp/Json/NewtonJson」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(サンプル)
(WebGLで出力したとき)
行39: 行39:
 
an error occurred runnning the unity content on this page.
 
an error occurred runnning the unity content on this page.
 
see your browser javascript console for more info.
 
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]
 
</pre>
 
</pre>
  

2021年7月5日 (月) 17:03時点における版

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

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]

参考

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

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