美文网首页
Unity读写Json文件

Unity读写Json文件

作者: betterplace | 来源:发表于2021-03-09 01:15 被阅读0次

方法一 直接传结构体与保持文件名字

  public void WriteJson(object ob, string jsonName)

    {

        string json = LitJson.JsonMapper.ToJson(ob);

        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(json);

        string filePath = Application.dataPath + "/Resources/" + jsonName + ".json";

        using (System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))

        {

            stream.Write(bytes, 0, bytes.Length);

        }

        UnityEditor.AssetDatabase.Refresh();//属性Asset,生成完成后立刻可以看到文件

    }

方法二 jsonwriter 半编译写法

  void CreateJson()

    {

        string path = Application.dataPath + "/Resources/Jsons/" + objBase.transform.name + ".json";

        FileInfo fileInfo = new FileInfo(path);

        StreamWriter sw = fileInfo.CreateText();

        StringBuilder sb = new StringBuilder();

        JsonWriter jsonwriter = new JsonWriter(sb);

        jsonwriter.WriteObjectStart();//1

        for (int i = 0; i < numbers.Count; i++)

        {

            jsonwriter.WritePropertyName("Number" + (i + 1));

            jsonwriter.WriteObjectStart();//1-1

            type = new List<Transform>();

            foreach (Transform child in numbers[i].transform)

                type.Add(child);

            jsonwriter.WritePropertyName("TypeCount"); jsonwriter.Write(type.Count);

            for (int j = 0; j < type.Count; j++)

            {

                jsonwriter.WritePropertyName("Type" + (j + 1));

                jsonwriter.WriteObjectStart();//1-1-1

                pos = new List<Transform>();

                foreach (Transform child in type[j].transform)

                    pos.Add(child);

                jsonwriter.WritePropertyName("Size"); jsonwriter.Write(pos[0].transform.localScale.x.ToString());

                jsonwriter.WritePropertyName("Pos");

                jsonwriter.WriteArrayStart();

                for (int k = 0; k < pos.Count; k++)

                {

                    jsonwriter.Write("(" + pos[k].transform.localPosition.x + "," + pos[k].transform.localPosition.y + ")");

                }

                jsonwriter.WriteArrayEnd();

                jsonwriter.WriteObjectEnd();//1-1-1

            }

            jsonwriter.WriteObjectEnd();//1-1

        }

        jsonwriter.WriteObjectEnd();//1

        sw.WriteLine(sb.ToString());

        sw.Close();

        AssetDatabase.Refresh();

    }

Json 里面的数据是double 类型,使用float 会出问题

double.Parse  float .Parse    不一样

相关文章

  • Unity读写Json文件

    方法一 直接传结构体与保持文件名字 public void WriteJson(object ob, strin...

  • python中的json

    1、读写文件读写文件主要有json.dump() 与 json.load() 两个函数json.dump()将py...

  • 58. (android开发)Json文件的读写

    Json格式是常见的读写形式。读写Json文件也是常用的操作。这次来实践一下Json文件的读写。首先在SD卡上的读...

  • python request/读写/上传文件

    python 读写文件: data_json = json.dumps(result_r)#json字符串 f =...

  • day09 作业 2018-07-26

    文本文件读写 滚滚长江东逝水   二进制文件读写   json文件读写   姓名灰太狼电话234567[{'nam...

  • unity读写文件

    读文件 private void ReadFile(string country){ StreamReader s...

  • python 读写文件,json,目录

    常见的一些json读写的操作读取某个目录下的文件 读取某些json字段 读写文件 读取shell参数,参数下标从1...

  • 技术 | Windows平台上,用Python处理Json文件的

    示例 Windows平台上,用Python处理Json文件在json文件中记录程序的执行次数: 以读写模式打开文件...

  • iOS读写json文件

    一.获取沙盒路径 每个iOS应用都有自己专属的应用沙盒,应用沙盒就是文件系统中的目录。但是iOS系统会将每个应用的...

  • python读写json文件

    将字典保存为本地json文件 从本地json文件读取数据 NOTE : 这里的dump()方法和load方法()没...

网友评论

      本文标题:Unity读写Json文件

      本文链接:https://www.haomeiwen.com/subject/xrtuphtx.html