Unity 读取Json文件、创建Json文件
using System.IO;
using UnityEngine; public class ReadJson:MonoBehaviour {
public static TestSetting test = new TestSetting();
//文件路径
public static string JsonPath;
public static string TM;
public static string PDD1;
public static string PDD2;
public static string JD1;
public static string JD2;
public static string SH; public static string AndroidDirectory = "/storage/emulated/0/Android/资源/配置文件/";//安卓路径
public static string WindowsDirectory = System.Environment.CurrentDirectory + "/资源/配置文件/";//Windows路径
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
public static void Start() { // Windows设置
print(Directory.Exists(WindowsDirectory)+ "Windows设置");
if(Directory.Exists(WindowsDirectory)) {
JsonPath = WindowsDirectory + "Config.json";
print("Windows:Json文件已创建无需创建");
ReadMyJson();
return;
} else if(!Directory.Exists(WindowsDirectory)) {
Directory.CreateDirectory(WindowsDirectory);//不存在则创建
StreamWriter sw = new StreamWriter(WindowsDirectory + "Config.json");
sw.WriteLine("{\r\n\r\n \"Port\": \"10086\",\r\n \"PDD1\": \"0\",\r\n \"PDD2\": \"0\",\r\n \"JD1\": \"0\",\r\n \"JD2\": \"1920\",\r\n \"SH\": \"1080\"\r\n}");
sw.Close();
JsonPath = WindowsDirectory + "Config.json";
print("Windows:创建新的Json文件");
ReadMyJson();
return;
} // Android设置
print(Directory.Exists(AndroidDirectory) + "Android设置");
if(Directory.Exists(AndroidDirectory)) {
JsonPath = AndroidDirectory + "Config.json";
print("Android:Json文件已创建无需创建");
ReadMyJson();
return;
} else if(!Directory.Exists(AndroidDirectory)) {
Directory.CreateDirectory(AndroidDirectory);//不存在则创建
StreamWriter sw = new StreamWriter(AndroidDirectory + "Config.json");
sw.WriteLine("{\r\n\r\n \"Port\": \"10086\",\r\n \"PDD1\": \"0\",\r\n \"PDD2\": \"0\",\r\n \"JD1\": \"0\",\r\n \"JD2\": \"1920\",\r\n \"SH\": \"1080\"\r\n}");
sw.Close();
JsonPath = AndroidDirectory + "Config.json";
print("Android:创建新的Json文件");
ReadMyJson();
return;
} } //读取方法
public static void ReadMyJson() {
string jsonString = File.ReadAllText(JsonPath);
TestSetting mytest = JsonUtility.FromJson<TestSetting>(jsonString);
test = mytest;
TM = mytest.TM;
PDD1 = mytest.PDD1;
PDD2 = mytest.PDD2;
JD1 = mytest.JD1;
JD2 = mytest.JD2;
SH = mytest.SH;
print("端口号:" + TM);
print("倒计时:" + PDD1); }
} //数据类型,用来存储从json中读到的键值对
public struct TestSetting {
public string TM;
public string PDD1;
public string PDD2;
public string JD1;
public string JD2;
public string SH;
}
Unity 读取Json文件、创建Json文件的更多相关文章
- Unity 读取、写入自定义路径文件,调用System.Windows.Forms
调用System.Windows.Forms DLL 首先在Unity新建Plugins文件夹添加System.Windows.Forms.dll 然后代码中添加引用 using System; us ...
- Unity读取StreamingAssets路径下的文件
/// <summary> ///读取StreamingAssets中的文件 /// </summary> /// <param name="path" ...
- Java学习笔记之I/O流(读取压缩文件以及压缩文件)
1.读取压缩文件:ZipInputStream 借助ZipFile类的getInputStream方法得到压缩文件的指定项的内容,然后传递给InputStreamReader类的构造方法,返回给Buf ...
- 经纬坐标(BLH)数据创建.kml文件小工具设计 Java版
技术背景 KML,是标记语言(Keyhole Markup Language)的缩写,最初由Keyhole公司开发,是一种基于XML 语法与格式的.用于描述和保存地理信息(如点.线.图像.多边形和模型 ...
- 【Set jsonObj = toJson( jsonString )】创建JSON实例
创建JSON实例: 原型: toJson( jsonString ) 说明: 创建JSON实例 返回: [JSON] 参数: jsonString [可选] 可以用json格式字符串创建实例 示例: ...
- Oracle_管理控制文件和日志文件
控制文件: 控制文件在数据库创建时被自动创建,并在数据库发生物理变化时更新.控制文件被不断更新,并且在任何时候都要保证控制文件是可用的.只有Oracle进程才能安全地更新控制文件的内容,所以,任何时候 ...
- 在/proc文件系统中增加一个目录hello,并在这个目录中增加一个文件world,文件的内容为hello world
一.题目 编写一个内核模块,在/proc文件系统中增加一个目录hello,并在这个目录中增加一个文件world,文件的内容为hello world.内核版本要求2.6.18 二.实验环境 物理主机:w ...
- unity读取json文件
首先填表 [escel转json]注意,粘贴表之后,需要把最后的空行删掉 http://www.bejson.com/json/col2json/ [json格式化] http://www.bejso ...
- 在Unity中使用LitJson解析json文件
LitJson 这个库需要找资源,找到LitJson.dll后将它放在Assets文件夹下,在脚本中使用using引入即可 测试代码 json文件: {"Archice":[{&q ...
- php 关于php创建 json文件 和 对文件增删改查 示例
<?php //fopen($file,'w'); //打开文件/创建文件 //file_put_contents($file,$content)//写入文件 //file_get_conten ...
随机推荐
- CCRD_TOC_2008年第4期
中信国健临床通讯 2008年第4期 目 录 类风湿关节炎 1. 大型系统评价分析:生物制剂与传统DMARD联用是MTX难治性RA患者的最佳治疗策略 Donahue KE, et al ...
- forEach、for in 、 for of三者的区别
1.for 原始的遍历: 其实除了这三种方法以外还有一种最原始的遍历,自Javascript诞生起就一直用的 就是for循环,它用来遍历数组 var arr = [1,2,3,4] for(var i ...
- Angular UI库
1.angular 使用 bootstrap 安装bootstrap npm install bootstrap --save 安装bootstrap-icons npm i bootstrap-ic ...
- (七) Mysql 之 binlog redolog 二阶段提交
(一)背景知识: 1 binlog binlog 我们中文一般称作归档日志, 是 MySQL Server 层的日志,而不是存储引擎自带的日志,它记录了所有的 DDL 和 DML(不包含数据查询语句) ...
- js中各种事件监听
html.push('<input type="button" id="autocount_' + sysTime + '" class="la ...
- 第八周作业-N67044-张铭扬
1. 完成ftp的mysql虚拟用户 数据库服务器:10.0.0.152 FTP服务器:10.0.0.156 #配置数据库服务器 [root@centos8 ~]# yum -y install ma ...
- 【redis】配置优化及从库优先级
https://blog.51cto.com/u_15902893/5912902 vim /etc/redis/redis.conf ##################基础############ ...
- fabric学习笔记5
fabric学习笔记5 20201303张奕博 2023.1.15 fabric中的docker命令 1.查看本机的镜像列表 docker images 2.拉取镜像 docker pull hype ...
- java--BigDecimal 类型介绍
BigDecimal.add(); //加法 BigDecimal.subtract(); //减法 BigDecimal.multiply(); //乘法 BigDecimal.divi ...
- 数值分析之解线性方程组的直接方法 5.X
矩阵 谱分解 设 \(\boldsymbol{A}=a_{i j} \in \mathbb{R}^{n \times n}\) , 若存在数 \(\lambda\) (实数或复数) 和非零向量 \(\ ...