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 ...
随机推荐
- CSS 页面整体变灰色
body {-webkit-filter: grayscale(100%) !important;-moz-filter: grayscale(100%) !important;-ms-filter: ...
- Windows 注册表是什么
注册表的概念 历史发展 在 Windows 3.x 操作系统中,注册表是一个极小文件,其文件名为 Reg.dat,里面只存放了某些文件类型的应用程序关联,大部分的设置是被放在 win.ini.syst ...
- day06-动态SQL语句
动态SQL语句 1.基本介绍 官方文档 mybatis – MyBatis 3 | 动态 SQL 为什么需要动态SQL? 动态SQL是MyBatis的强大特性之一 使用 JDBC 或其他类似的框架,根 ...
- pip使用阿里云镜像
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ pip config set install.trust ...
- Linux操作命令(一)1.ls命令 2.cd命令 3.pwd命令
1.ls 命令 ls 命令就是 list 的缩写, ls 用来打印出当前目录的清单. 参数 描述 -a –all 列出目录下的所有文件,包括以 . 开头的隐含文件 -l 除了文件名之外,还将文件的权限 ...
- QP之QEP事件分配流程分析
*********************************1*********************************** QActive *AO_Blinky = &l_bl ...
- MDK GCC调试
openocd调试 https://blog.csdn.net/chunyexixiaoyu/article/details/120448515
- vscode开发vue3+ts环境搭建
一.开发环境准备 1.nvm 2.vscode 3.node 4.chome 5.git 6.npm install -g yarn tyarn 7.tyarn global add commitiz ...
- vue2的反向代理
// 在vue.config.js中的配置项配置 devServer: { proxy: { // 当我们的本地的请求 有/api的时候,就会代理我们的请求地址向另外一个服务器发出请求 '/api': ...
- pip安装psycopg2报错Could not find a version that satisfies the requirement psycopg2
pip安装psycopg2报错 在使用命令(pip install psycopg2)安装psycopg2时,会报错: ERROR: Could not find a version that sat ...