位置: 编程技术 - 正文

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怎么移动物体)

  • 海关缴款书有效期多久
  • 取用备用金要188分
  • 增值税税额由哪方承担
  • 非正常损失会计利润调整
  • 开发间接费什么时候计提
  • 机动车销售统一专票有几联
  • 如何降低应用耗电
  • 应收账款已收回但是账面还有余额怎么处理
  • 股东入股资本公司的条件
  • 私营独资企业是公司还是非公司
  • 年度汇算清缴怎么填写
  • 为什么算除税价不算税金
  • 理论增值额的含义
  • 小规模纳税人开票额度
  • 坏账准备核销和转销的区别
  • 没给顾客发票需要罚款吗
  • 以前年度少计收入 会计怎么处理
  • 公积金多交退回怎么申报个税
  • 购入低值易耗品一批,实际成本4000元
  • 福利费不属于工资
  • 费用报销交通费
  • 股权转让受让方要交个人所得税吗
  • 补助是按基本工资还是总工资
  • 营改增后一般纳税人税率
  • 微软发布windows
  • kb4539601安装失败
  • 企业所得税涉税风险通知书
  • 大金中央空调面板图解
  • 委托加工物资账务怎么做
  • 交易性金融资产的交易费用计入哪里
  • smart模板教程
  • 行人检测技术
  • python优化工具箱
  • 房产税缴纳时间点
  • 出口退税后发生退货怎么处理
  • 结算备付金管理办法(2019年修订版)
  • 所得税弥补亏损年限10
  • 出口货物做免税处理
  • 公司缴纳残保金的标准
  • 税务局的罚款记录怎么查
  • 坏账准备怎么结转到本年利润
  • 入库单金额写错可以改吗
  • 出口退税进项税额转出怎么申报
  • 公司注销开户许可证是不是要上交
  • 小微企业营业外收入二级科目怎么
  • 微信转账截图可以作为会计凭证吗
  • 货款尚未收到属于什么会计科目
  • 多少钱算固定资产2022
  • sql数据库怎么执行命令
  • mysql查询时间语句
  • 老毛桃winpe系统对注册表信息进行备份的方法
  • win8蓝屏解决方法
  • freebsd安装教程
  • 丢失msvcp71.dll
  • osx10.11.6版本如何
  • 如何避免系统性风险
  • win10系统光盘制作
  • win8系统怎么直接进入桌面
  • win10开机提醒
  • mac刻录工具
  • mom.exe是什么进程
  • Win10 Mobile 10586.63截图曝光:或为正式推送版本
  • linux tcptraceroute
  • express model
  • cocos2dx-3.1.1 win8+eclipse+android开发 初学者容易迷惑的两个问题
  • cocos2dx入门
  • unity自带的代码编辑器
  • Node.js中的事件循环是什么意思
  • shell for循环执行命令
  • android图片全屏显示
  • linux shell命令大全
  • 代码行数不够了,怎么弄多行
  • Unity uGui RawImage 渲染小地图
  • centos6.5
  • 上海税务网上报税
  • 企业房产税优惠减免政策2020
  • 地税是地方税吗
  • 重庆国税电子税务局
  • 非中山户籍学生可以自己买医保吗
  • 20万块手表交多少税
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设