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. 为什么不建议使用 @Autowired 注解进行注入

    在Spring中,Bean的注入一般有三种方式:属性注入.set方法注入.构造器注入. 1.Autowired注入的原理 @Autowired属于属性注入,默认按照类型装配,默认情况下要求依赖的对象必 ...

  2. 3D数字孪生场景编辑器介绍

    1.背景 数字孪生的建设流程涉及建模.美术.程序.仿真等多种人才的协同作业,人力要求高,实施成本高,建设周期长.如何让小型团队甚至一个人就可以完成数字孪生的开发,是数字孪生工具链要解决的重要问题.目前 ...

  3. 基于Linux编译JDK18

    1.概述 JDK都没手动编译过,敢说自己是Java程序员吗?(By 羊哥--JDK都没手动编译过,敢说自己是Java程序员吗?实战编译Java源码(JDK源码,JVM)视频教程_哔哩哔哩_bilibi ...

  4. Lambda表达式和Collection集合

    Lambda表达式 Lambda表达式理解 Lambda表达式是Jdk 8 开始新增的一种语法形式:作用:用于简化匿名内部类的代码写法 注意:Lambda表达式只能简化函数式接口的匿名内部类!!! 什 ...

  5. C# datatable group by分组

    DataTable dt = new DataTable(); dt.Columns.Add("A"); dt.Columns.Add("B"); dt.Col ...

  6. 时间戳转换为yyyy-MM-dd格式

    原文链接https://blog.csdn.net/hu104160112/article/details/111167033

  7. Nodejs杀死本地应用(win)

    windows端nodejs检查应用运行并杀死. import {exec, execSync} from "child_process" import {decode} from ...

  8. ubuntu 22.04 网络配置ib网卡配置

    第一步:查看Ubuntu版本与内核版本 cat /etc/issue 这说明系统的版本为:Ubuntu 20.04.4 LTS \n \l uname -a Linux gacs-gm-11 5.4. ...

  9. 在windows服务中托管asp.net.core

    参考:https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-3.1& ...

  10. 利用for循环同步执行异步方法

    //定义一个异步函数 const foo1 = (i) => { return new Promise((resolve, reject) => { setTimeout(() => ...