位置: 编程技术 - 正文

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

  • 计提房产税会计分录怎么做
  • 投资性房地产出售时公允价值变动损益
  • 向境外支付赔偿金需要发票么
  • 职工罚款收入计入什么科目
  • 个人抬头发票可以抵税个人所得税吗
  • 经营租入的固定资产是资产吗
  • 假发票是怎么开出来的?
  • 电子承兑汇票如何贴现网银操作
  • 交了定金签了合同
  • 无形资产的摊销,可摊销的标准是什么?
  • 应交税金应交增值税已交税金怎么结转
  • 代扣代缴手续费返还需要缴纳增值税吗
  • 销售的行为
  • 业务宣传及广告费超比例
  • 装卸搬运费是否含税
  • 财务怎么查询微信转账单号真假
  • 营改增后工程税收怎么计算
  • 本月应该确认收入,但是一般下月才开票该怎么处理?
  • 哪里查询发票是否抵扣
  • 出口退税需要什么单据
  • 建筑行业收到劳务发票入工程施工科目
  • 兼职人员的工资需要发票吗
  • 筹建期的工资
  • 报销差旅费退回余款填什么凭证
  • 事业单位财政补助收入会计分录
  • 公司人格否认制度构成要件
  • 企业所得税法规定,企业购置并实际使用
  • 被雪覆盖的山峰
  • 复现开源论文代码总结
  • 海峡群岛属于哪个洲
  • 委托加工应税消费品以委托人为消费税的纳税义务人
  • pdo php
  • 无法偿还的应付账款计入什么科目
  • 基于网络创新形成的大数据的最突出特征是什么?( )
  • 逆回购要手续费吗
  • 培训费用是什么成本
  • python访问字符串
  • 织梦cms可以商用吗
  • static在c语言中用法
  • 织梦会员功能
  • 铝合金门窗行业利润率
  • 工会经费为0是不用申报的?
  • 长期待摊费用如何做分录
  • 企业筹建期间的费用处理
  • 土地使用权转让合法吗
  • 现金折扣是啥
  • 小规模纳税人未开票收入如何申报增值税
  • 货款去零头分录
  • 免抵税额下个月还能退吗
  • 运费少给怎么办
  • 计提社保公积金的账务处理
  • 固定资产增值税发票如何入账
  • 减免与抵免
  • 出现事故保险公司负责协商吗
  • 交易性金融资产包括哪些项目
  • 折现率算现值公式
  • 会计出账入账怎么做
  • SQL Server Alert发送告警邮件少了的原因
  • sql必知必会第四版
  • win2008r
  • freebsd使用wifi
  • ubuntu左侧菜单不见了
  • windows打开
  • Win10 Mobile 10586.164上手体验视频评测
  • 在Linux系统中如果想移出已安装的软件
  • win10系统自带虚拟机怎么用
  • 编辑器组件
  • bootstrap导航都有哪些
  • Android游戏开发读后感
  • android属性动画
  • unity设置update间隔
  • python连接Oracle数据库
  • 纳税人不如实申报税收怎么办
  • 支付境外服务费需要缴纳哪些税费
  • 浙江发票查验不了什么原因
  • 成都水费查询系统
  • 卷票是什么票
  • 通用定额发票分经营类型吗
  • 税控盘如何查询季度统计
  • 郑州国税发票查询系统
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设