位置: 编程技术 - 正文

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

  • 计提本月个人所得税
  • 转让股份缴纳什么税
  • 涉税专业服务是做什么的
  • 土地增值税计算题
  • 公司办资质的发票入什么科目
  • 合同不交印花税合法吗
  • 个税身份证验证不通过已经申报缴税
  • 企业买车算固定收入吗
  • 房地产企业季度所得税申报
  • 增值税发票税控开票软件怎么下载
  • 一般纳税人劳务税率是多少2023
  • 二手车交易怎么办理过户手续
  • 当月发票开错了已经红冲怎么做分录?
  • 土地不动产登记证办理流程
  • 营业外支出税前扣除比例是多少
  • 外贸出口企业城市排名
  • 企业一次性补助金是多少标准
  • 关于保险机构代收车船税开具增值税发票问题的公告
  • 维修服务费税率是多少?
  • 购买方取得专票会计分录
  • 公司如何确定总股本
  • 自己生产的水泥用于建造厂房
  • 出售单位车辆如何处理业务?
  • 重装系统D盘文件还能恢复吗
  • 企业库存太多后果
  • 有限合伙企业如何退出合伙人股份
  • win7旗舰版系统怎么样
  • 布鲁克斯的故事
  • windows搭建dvwa
  • php获取变量长度
  • php image
  • 中医诊所的税收分类编码
  • 分配股利账务处理
  • php curl_init
  • 月收入10万以下免增值税
  • vue3.0用法
  • java开源二次开发平台
  • python photoshop
  • 母子公司之间的租赁房屋
  • 非公党建党费收缴
  • 手机充值如何开公司发票
  • 资本公积的项目有哪些
  • 罚款可以直接在12123
  • 发票上没有数量可以吗
  • 金蝶可以自动结转增值税吗
  • 给客户退款怎么说
  • 企业向个人租房发票怎么开
  • 收到货款未开票怎么入账
  • 社会团体费怎么做分录
  • 内部债权债务的抵消分录
  • 广告收入计入哪个科目
  • 当月发生的费用下月支付
  • sqlmd5加密后解密
  • mysql子查询嵌套查询
  • windows软件授权服务报告无法激活计算机
  • Windows Server 2008关闭闲置状态的IDE通道
  • centos 离线安装git
  • android7应用程序开发教程
  • linux 下MySQL服务器的启动与停止
  • ubuntu怎么安装程序
  • 电脑关机没反应怎么办出现一个对话框
  • win7 64位系统提示"Windows7不能识别网络打印机"的故障原因及解决方法
  • windows8怎么使用
  • linux 互传文件
  • Linux 后台执行程序如何操作?
  • win10英文版系统怎么完全改为中文
  • win10预览版好吗
  • linux怎么配置raid
  • jquery编程
  • bat批处理脚本教程
  • python 简单例子
  • function函数的定义
  • 批处理应用实例
  • vue.js 2.x
  • 批处理/l
  • unityui
  • 保险赔款是免征还是不征?
  • 巩义市公路管理局 邰江华简历
  • 葫芦岛市税务局电话
  • 解放服务站总部电话
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设