位置: 编程技术 - 正文

unity中从txt文件中读取解析json数据&&unity中json数据写入txt文件(unity怎么把文件导入项目)

编辑:rootadmin
using UnityEngine;using System.Collections;using LitJson;using System.IO;#if UNITY_EDITORusing UnityEditor;#endifpublic class NewBehaviourScript : MonoBehaviour { public TextAsset txt; public string filePath; public string fileName; // Use this for initialization void Start () { filePath = Application.dataPath &#; "/TextFile"; fileName = filePath &#; "/File.txt"; } // Update is called once per frame void Update () { } void ReadJson() { //string str = "{&#;name&#;:&#;taotao&#;,&#;id&#;:,&#;items&#;:[{&#;itemid&#;:,&#;itemname&#;:&#;dtao&#;},{&#;itemid&#;:,&#;itemname&#;:&#;test_2&#;}]}"; //string str = "{'name':'taotao','id':'','items':[{'itemid':'','itemname':'dtao'},{'itemid':'','itemname:test_2'}]}"; string str=@"{""album"" : {""name"" : ""The Dark Side of the Moon"",""artist"" : ""Pink Floyd"",""year"" : ,""tracks"" : [""Speak To Me"",""Breathe"",""On The Run""]}}"; //这里是json解析了 LitJson.JsonData jd = LitJson.JsonMapper.ToObject(str); for(int ii=0;ii<jd["album"]["tracks"].Count;ii&#;&#;) { Debug.Log(jd["album"]["tracks"][ii]); } } void ReadJsonFromTXT() { //解析json JsonData jd = JsonMapper.ToObject(txt.text); JsonData jditem = jd["results"]; for (int i = 0; i < jditem.Count; i&#;&#;) { Debug.Log(jditem[i]["id"]); //Debug.Log(i); //Debug.Log(jd["results"]); //Debug.Log("id="&#;jditem["id"]); //Debug.Log("color="&#;weapon["color"]); //Debug.Log("durability="&#;weapon["durability"]); } } void WriteJsonAndPrint() { System.Text.StringBuilder strB = new System.Text.StringBuilder(); JsonWriter jsWrite = new JsonWriter(strB); jsWrite.WriteObjectStart(); jsWrite.WritePropertyName("Name"); jsWrite.Write("taotao"); jsWrite.WritePropertyName("Age"); jsWrite.Write(); jsWrite.WritePropertyName("MM"); jsWrite.WriteArrayStart(); jsWrite.WriteObjectStart(); jsWrite.WritePropertyName("name"); jsWrite.Write("xiaomei"); jsWrite.WritePropertyName("age"); jsWrite.Write(""); jsWrite.WriteObjectEnd(); jsWrite.WriteObjectStart(); jsWrite.WritePropertyName("name"); jsWrite.Write("xiaoli"); jsWrite.WritePropertyName("age"); jsWrite.Write(""); jsWrite.WriteObjectEnd(); jsWrite.WriteArrayEnd(); jsWrite.WriteObjectEnd(); Debug.Log(strB); JsonData jd = JsonMapper.ToObject(strB.ToString()); Debug.Log("name=" &#; jd["Name"]); Debug.Log("age=" &#; jd["Age"]); JsonData jdItems = jd["MM"]; for (int i = 0; i < jdItems.Count; i&#;&#;) { Debug.Log("MM name=" &#; jdItems[i]["name"]); Debug.Log("MM age=" &#; jdItems[i]["age"]); } } void WriteJsonToFile(string path,string fileName) { System.Text.StringBuilder strB = new System.Text.StringBuilder(); JsonWriter jsWrite = new JsonWriter(strB); jsWrite.WriteObjectStart(); jsWrite.WritePropertyName("Name"); jsWrite.Write("taotao"); jsWrite.WritePropertyName("Age"); jsWrite.Write(); jsWrite.WritePropertyName("MM"); jsWrite.WriteArrayStart(); jsWrite.WriteObjectStart(); jsWrite.WritePropertyName("name"); jsWrite.Write("xiaomei"); jsWrite.WritePropertyName("age"); jsWrite.Write(""); jsWrite.WriteObjectEnd(); jsWrite.WriteObjectStart(); jsWrite.WritePropertyName("name"); jsWrite.Write("xiaoli"); jsWrite.WritePropertyName("age"); jsWrite.Write(""); jsWrite.WriteObjectEnd(); jsWrite.WriteArrayEnd(); jsWrite.WriteObjectEnd(); Debug.Log(strB); //创建文件目录 DirectoryInfo dir = new DirectoryInfo(path); if (dir.Exists) { Debug.Log("This file is already exists"); } else { Directory.CreateDirectory(path); Debug.Log("CreateFile"); #if UNITY_EDITOR AssetDatabase.Refresh(); #endif } //把json数据写到txt里 StreamWriter sw; if (File.Exists(fileName)) { //如果文件存在,那么就向文件继续附加(为了下次写内容不会覆盖上次的内容) sw = File.AppendText(fileName); Debug.Log("appendText"); } else { //如果文件不存在则创建文件 sw = File.CreateText(fileName); Debug.Log("createText"); } sw.WriteLine(strB); sw.Close(); #if UNITY_EDITOR AssetDatabase.Refresh(); #endif } void OnGUI() { GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height)); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.BeginVertical(); GUILayout.FlexibleSpace(); if (GUILayout.Button("ReadJson")) { ReadJson(); } if (GUILayout.Button("ReadJsonFromTXT")) { ReadJsonFromTXT(); } if (GUILayout.Button("WriteJsonAndPrint")) { WriteJsonAndPrint(); } if (GUILayout.Button("WriteJsonToFile")) { WriteJsonToFile(filePath,fileName); } GUILayout.FlexibleSpace(); GUILayout.EndVertical(); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.EndArea(); }}

推荐整理分享unity中从txt文件中读取解析json数据&&unity中json数据写入txt文件(unity怎么把文件导入项目),希望有所帮助,仅作参考,欢迎阅读内容。

unity中从txt文件中读取解析json数据&&unity中json数据写入txt文件(unity怎么把文件导入项目)

文章相关热门搜索词:unity怎么把文件导入项目,unity读取txt文件,unity文件怎么导出,unity文件怎么导出,unity写入txt,unity文件怎么导出,unity读取txt文件,unity读取文本文件,内容如对您有帮助,希望把文章链接给更多的朋友!

Unity3D让物体始终朝向摄像机的2中方法 1、设置rotation此方法的基础是将对象绑定在场景中的一个对象上,然后设置对象的rotation或者lookat,需要在Update中时时更新//方法1//m_Name.transform.LookAt(Came

Unity3d 5.0 动态播放视频,播放音乐 1、Unity3d5.0动态播放视频工具:MobileMovieTexture.unitypackage2.1.1问题描述:目前这个第三方播放视频工具不提供播放时音乐,并且网上教材都是将视频直接拖

unity脚本的一些笔记(一) MonoBehavior事件响应一.启动与刷新函数1.启动Awake()初始化函数,在游戏开始时系统自动调用一般用来创建变量无论脚本组件时否被激活,都能被调用Start()

标签: unity怎么把文件导入项目

本文链接地址:https://www.jiuchutong.com/biancheng/377231.html 转载请保留说明!

上一篇:unity多点触控(unity触摸屏控件)

下一篇:Unity3D让物体始终朝向摄像机的2中方法(unity3d怎么移动物体)

  • 公司车保险费用高么
  • 公司用的口罩放哪个科目
  • 固定资产投资额包括哪些会计科目
  • 车费报销计入什么费用
  • 收到所得税退税怎么做账务处理
  • 多开票要交多少税
  • 一般纳税人利润100万要交多少税
  • 非货币性交换需要确认主营业务收入吗
  • 辅导期纳税人会计处理
  • 外购货物准予抵扣进项税额26万元,货物已验收入库
  • 受托加工费直接计入生产成本
  • 委托加工设备
  • 部分退货退款剩下的钱什么时候给卖家
  • 签订设备维修合同会计分录
  • 转让购入的设备税率
  • 销售礼盒增值税税率
  • 办公室租金相关法规
  • 物业费收入的增值税税率是多少
  • 虚开增值税发票具体操作是怎样的?
  • 借款合同印花税纳税期限
  • 通行费如何填报表二
  • 注册资本 变更
  • 企业应交增值税的计算公式
  • 分配现金股利是什么活动
  • 个人收回转让的股权个税应如何处理?
  • 增值税步骤
  • 新版edge浏览器历史记录
  • 关闭客户端快捷键
  • 大学期末 知乎
  • 收据大写后面的数字
  • javascript中
  • linux中nano什么指令
  • 营改增的会计分录
  • 员工出差补贴怎么入账
  • 酒店行业存货周转率多少合适
  • 短期借款转长期借款利息
  • 高新技术企业研发费用占比要求
  • js鼠标事件包括哪几种
  • 卷积神经网络有哪些
  • laravel使用了哪些设计模式
  • javaweb:jquery中ajax的应用
  • 外国人在我国境内被刑事拘留
  • 固定资产折旧准备属于什么科目
  • sqlserver连接到服务器登录名
  • 包装的设计要遵循哪些要求?
  • 同一控制下企业合并
  • 季节性生产企业有哪些
  • 小额贷款涉及的法律
  • 单位购买公文包合法吗
  • 实时缴税计入什么科目
  • 补交增值税税款怎么做账
  • 负债清偿损益明细表计税基础怎么填
  • 企业之间土地交易
  • 企业固定资产标准最新规定
  • sql server储存过程的创建与使用
  • sysbench测试mysql,自带的测试套
  • win2008下的HPC群集是什么有什么作用
  • windows打不开添加打印机
  • Ubuntu Eclipse MyEclipse 添加GBK支持 不乱码
  • mac系统删除系统软件
  • 微信付费使用是真的吗
  • psimsvc.exe - psimsvc是什么进程 有什么用
  • 系统恢复 win10
  • win10系统怎么设置电脑密码
  • js对话框确定事件
  • 批量创建用户
  • django forloop
  • unity的ugui
  • jquery与js
  • javascript绘制图形
  • node.js中使用文件流进行文件复制,首先需要创建一个
  • 跟我从零基础学到了什么
  • jquery json对象
  • jquery单击事件和双击事件
  • python生成随机数表
  • Android之Broadcast与BroadcastReceiver
  • 出售废铁的会计分录
  • 公积金提取需要几个工作日
  • 平安车险如何开票报销
  • 洛阳市房屋契税新政策
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设