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文件的更多相关文章

  1. Unity 读取、写入自定义路径文件,调用System.Windows.Forms

    调用System.Windows.Forms DLL 首先在Unity新建Plugins文件夹添加System.Windows.Forms.dll 然后代码中添加引用 using System; us ...

  2. Unity读取StreamingAssets路径下的文件

    /// <summary> ///读取StreamingAssets中的文件 /// </summary> /// <param name="path" ...

  3. Java学习笔记之I/O流(读取压缩文件以及压缩文件)

    1.读取压缩文件:ZipInputStream 借助ZipFile类的getInputStream方法得到压缩文件的指定项的内容,然后传递给InputStreamReader类的构造方法,返回给Buf ...

  4. 经纬坐标(BLH)数据创建.kml文件小工具设计 Java版

    技术背景 KML,是标记语言(Keyhole Markup Language)的缩写,最初由Keyhole公司开发,是一种基于XML 语法与格式的.用于描述和保存地理信息(如点.线.图像.多边形和模型 ...

  5. 【Set jsonObj = toJson( jsonString )】创建JSON实例

    创建JSON实例: 原型: toJson( jsonString ) 说明: 创建JSON实例 返回: [JSON] 参数: jsonString [可选] 可以用json格式字符串创建实例 示例: ...

  6. Oracle_管理控制文件和日志文件

    控制文件: 控制文件在数据库创建时被自动创建,并在数据库发生物理变化时更新.控制文件被不断更新,并且在任何时候都要保证控制文件是可用的.只有Oracle进程才能安全地更新控制文件的内容,所以,任何时候 ...

  7. 在/proc文件系统中增加一个目录hello,并在这个目录中增加一个文件world,文件的内容为hello world

    一.题目 编写一个内核模块,在/proc文件系统中增加一个目录hello,并在这个目录中增加一个文件world,文件的内容为hello world.内核版本要求2.6.18 二.实验环境 物理主机:w ...

  8. unity读取json文件

    首先填表 [escel转json]注意,粘贴表之后,需要把最后的空行删掉 http://www.bejson.com/json/col2json/ [json格式化] http://www.bejso ...

  9. 在Unity中使用LitJson解析json文件

    LitJson 这个库需要找资源,找到LitJson.dll后将它放在Assets文件夹下,在脚本中使用using引入即可 测试代码 json文件: {"Archice":[{&q ...

  10. php 关于php创建 json文件 和 对文件增删改查 示例

    <?php //fopen($file,'w'); //打开文件/创建文件 //file_put_contents($file,$content)//写入文件 //file_get_conten ...

随机推荐

  1. 推荐一个Dapper扩展CRUD基本操作的开源库

    在C#众多ORM框架中,Dapper绝对称得上微型ORM之王,Dapper以灵活.性能好而著名,同样也是支持各种数据库,但是对于一些复杂的查询,我们写原生的SQL语句问题不大,对于CRUD基本操作,我 ...

  2. WPF Xaml标签的一些特殊符号 如何输入

    小于号 <   输入 <  注意有分号 大于号 >   输入  > 符号 &   输入 & 引号 "  输入 "

  3. 模拟浏览器与服务器交互(简易TomCat框架)

    模拟浏览器发送请求到服务器获取资源的思想和代码实现 浏览器发送请求到服务器获取资源的流程和概念 日常我们使用的浏览器,底层都是帮我们做了很多事情,我们只需要用,比如输入www.baidu.com,就可 ...

  4. python路径含空格导致打开文件报错

    2023.02.14更新: python中使用os.listdir不行,但是可以用os.walk,可以绕过空格的问题. 在windows中总有奇怪的问题,比如路径或文件名带空格,打开文件就会报错. 一 ...

  5. 使用Vue来制作导航栏超级简单

    在我们还没有学习Vue的时候一般都是使用JQ来制作导航栏,但是太复杂. 而使用Vue 来制作你会发现异常简单话不多说上代码 css代码随便写一写样式 HTML 部分,要给div设置一个ID   这里面 ...

  6. Mac系统中安装Nginx

    ​ 一.前言 本文介绍一下,如何在Mac系统中安装Nginx,把详细过程记录下来,方便以后查看,也方便大家学习. 二.正文 1.安装 Homebrew homebrew是什么?它是Mac中的一款软件包 ...

  7. Linux 在miniconda和anaconda同时安装时,卸载miniconda

    1. 找到miniconda目录,删除. rm -rf miniconda3/ 2. 编辑bashrc sudo vim .bashrc setup路径改回anaconda3的,注释掉"&g ...

  8. SSL certificate problem: unable to get local issuer certificate 错误解决

    终端报了如下错误git SSL certificate problem: unable to get local issuer certificate 这个问题是由于没有配置信任的服务器HTTPS验证 ...

  9. 初学银河麒麟linux笔记 第二章 虚拟机VMware16 共享文件

    由于需要把我在WINDOWS上写好的QT程序转移过来,因此这里需要设置共享文件.需要在虚拟机系统上安装VMwareTools,在主机上设置共享文件夹,这里参考了 https://blog.csdn.n ...

  10. ipvsadm DR模型的实现方式

    DR模型的lvs ----在实现DR模型的时候所有RS都要屏蔽掉ARP请求的响应 实现方法:1.可以用路由器绑定静态VIP:MAC(DVIP)2.可以使用arptables3.可以使用 kernel ...