怎么在unity3D工程中导入Newtonsoft.Json

怎么在unity3D工程中导入Newtonsoft.Json

unity旧版本自带的json接口太难用了(JsonUtility),不能序列化字典和列表等对象,只能序列化基础类型对象,所以基本等于没有。

在git上下载Newtonsoft.Json-for-Unity-master的压缩文件(.zip),解压之后,复制到unity3D工程的Asset/Plugins文件夹下就可以用了,
在这里插入图片描述
在脚本中就可以使用Newtonsoft.Json了,例如下述代码:

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

public class Player : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        string json = JsonConvert.SerializeObject(
            new Dictionary<string, string> {
    
    
                {
    
     "123", "123" },{
    
     "456", "456" }, });
        string path = Application.persistentDataPath + "/saveFile.json";
        File.WriteAllText(path, json);

    }

    // Update is called once per frame
    void Update()
    {
    
    
        
    }
}

祝好!

猜你喜欢

转载自blog.csdn.net/qq_41841073/article/details/131669856