位置: 编程技术 - 正文

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

  • 增值税开票软件ukey版
  • 一分钟了解货币
  • 公司间借款利息增值税进项能否抵扣
  • 上级补助收入支出决算科目
  • 发票金额多开了有事吗
  • 通过应交税费核算的
  • 固定资产加速折旧法
  • 资产减值损失借贷方向
  • 购买金蝶软件计入什么费用
  • 已经给员工交了社保能退吗
  • 固定资产减值准备增加记哪方
  • 注销时分公司欠款怎么办
  • 果蔬免征增值税有哪些
  • 行邮税税率表2023
  • 想要避免虚开发票,你就要注意以下行为
  • 往期从价房产税未申报怎么处理
  • 一般纳税人税控盘维护费会计分录
  • 内账一定要权责发生制吗
  • 车辆违章罚款怎么入账
  • 跨市工程预交的税怎么算
  • 建筑业月末账务处理
  • 小规模纳税人月销售额不超过10万免征
  • 注册资本与利息的税前扣除
  • 原材料计入资产成本吗
  • 用工会经费给员工发工资
  • 期末调汇凭证怎么调
  • 单位车辆车船税缴纳
  • 房地产企业个人所得税
  • php和aspnet哪个好
  • 合同资产减值怎么填
  • PHP:Memcached::decrement()的用法_Memcached类
  • php数组函数有哪些
  • 医疗保险在外地交了老家还要交吗
  • linux双ip路由设置方法
  • 企业所得税的税收筹划
  • form表单参数传递
  • php中global
  • 金融机构存放的保证金存款
  • js去除属性
  • 年报数据和四季度数据一样吗
  • 汽车进项税什么时候抵扣
  • 土方工程合同签了一般多久开工
  • 用工会经费给员工发工资
  • mongodb游记
  • db2数据库安装服务器的环境
  • 利润分配未分配利润是净利润吗
  • 营改增后服务业账务处理
  • 销项负数发票如何作废
  • 员工在异地缴纳社保,公司还能上吗
  • 增值税欠税及滞纳金计算
  • 社保缴纳的时候可以用吗
  • 公司购买银行理财产品怎么做账
  • 关于新修订的未保法
  • 固定资产转固流程图
  • 劳务报酬个税如何入账
  • 外汇账户具体包括哪些
  • 如果以前做了错事怎么办
  • 事业单位小规模纳税人咨询服务的税率
  • mysql 数据库
  • ubuntu和window双系统
  • 什么叫脚本错误
  • wsinspector.exe是什么进程
  • WINDOWS系统中删除放入回收站的文件占用什么空间
  • win7系统如何创建新用户
  • win7主要有哪些内容
  • win10系统版本20h2
  • win7怎样禁用无线网络连接
  • win8怎么设置
  • win8新建用户
  • win8系统电脑卡
  • cocos2dx4.0入门
  • unity3d入门教程中文图解
  • css fontstyle
  • python3m
  • java jsonstring
  • js中的!
  • 个人所得税自行纳税申报表a表填写模板
  • 网上税务局网址
  • 国外寄东西到国内要身份证号码吗
  • 地税局社保宣传标语
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设