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. 代码随想录算法训练营day04 | leetcode

    基础知识 记录一下栈实现及操作 public class ArrayDequeStack { public void main() { ArrayDeque stack = new ArrayDequ ...

  2. 常用的"小脚本"-json数据处理

    小背景: 我们公司项目中的小脚本是一些工具类,比如常用的是MapUtil工具类的一些方法 写公司的MapUtil工具类的方法要注意,方法名的命名,因为方法名,在公司的项目的某个业务流程有对方法名的进行 ...

  3. Oracle & MSSql 数据库表映射方法(dblink or other)

    一.Oracle 1.在旧库创建公共链接 命名为 bidblink create public database link bidblink connect to c##v26_xxxx IDENTI ...

  4. dotnet core 托管

    1.托管形式 dotnet core的托管形式分为 进程内托管(InProcess).进程外托管 (OutOfProcess) 新建的Web程序csproj带有 <AspNetCoreHosti ...

  5. ABP微服务系列学习-对接前端界面

    前面我们把后端的微服务架子基本搭建完成并成功启动了,现在我们可以对接前端界面了.这里我们直接用ABP模板里面的Angular的前端界面. 创建应用程序模板 使用ABPCli创建一个应用程序模板,前端选 ...

  6. kali日常快捷命令(用到就更新)

    什么BASH命令? 在Linux上采用bash作为标准,基本上它描述了对带有".sh"扩展名的vi编辑器等文本的处理并执行. 与编程一样,它有许多函数,如变量,函数和算术处理,所以 ...

  7. 桌面应用(.exe)设置窗口默认最大化、全屏(electron)

    设置窗口默认最大化.全屏(electron) 一.默认最大化 win = new BrowserWindow({show: false}) win.maximize() win.show() 二.默认 ...

  8. array copy() 的简单使用

    源码: public static native void arraycopy(Object src, int srcPos, Object dest, int destPos,int length) ...

  9. 钉钉h5开发流程

    1.先在钉钉开发者后台 https://login.dingtalk.com/oauth2/challenge.htm?redirect_uri=https%3A%2F%2Fopen-dev.ding ...

  10. Altium Designer Winter 09 — 01 — 快速创建项目

    新建项目 新建原理图 导入所需的库 添加元器件和接插件 连接导线 自动标注.修改元件属性 编译前--修改项目属性 编译,查看消息 生成网表.BOM.简易BOM,打印文件