当前位置:网站首页>unity--newtonsoft.json解析
unity--newtonsoft.json解析
2022-06-27 09:22:00 【格拉格拉】
1.level1.txt json文件
{
"level": {
"floorCount": 2,
"maxNum": 2,
"left": 1,
"right": 1,
"time":[
{
"floor1":0.2
},
{
"floor2":0.5
}
]
}
}2.LevelEntity.cs json实体类
[Serializable]
public class LevelEntity
{
public int _floorCount;
public int _maxNum;
public int _left;
public int _right;
public List<float> _time = new List<float>();
}3.读取StreamingAssets文件夹下的json文件
public IEnumerator LoadLevel(string fileName)
{
string filePath = "";
string result = "";
#if UNITY_EDITOR
filePath = Application.dataPath + "/StreamingAssets/" + fileName + ".txt";
#elif UNITY_IPHONE
filePath = Application.dataPath +"/Raw/"+fileName+".txt";
#elif UNITY_ANDROID
filePath = “jar:file://” + Application.dataPath + “!/assets//"+fileName+".txt";
#endif
Debug.LogError("FilePath:" + filePath);
if (filePath.Contains("://"))
{
UnityWebRequest www = UnityWebRequest.Get(filePath);
yield return www.SendWebRequest();
if (www.isDone == false)
{
Debug.LogError("read json error:" + fileName);
}
result = www.downloadHandler.text;
}
else
{
result = System.IO.File.ReadAllText(filePath);
}
Debug.Log(result);
JObject jsonData = JObject.Parse(result);
JToken jToken = jsonData["Data"];
le = new LevelEntity
{
_floorCount = int.Parse(jToken["floorCount"].ToString()),
_maxNum = int.Parse(jToken["maxNum"].ToString()),
_random = int.Parse(jToken["random"].ToString()),
_left = int.Parse(jToken["left"].ToString()),
_right = int.Parse(jToken["right"].ToString())
};
}
4.读取Resources文件夹下的json文件
//读取Resources下的json文件
public void ReadJson(string fileName)
{
//获得Json字符串
string json = "";
TextAsset text = Resources.Load<TextAsset>("levels/" + fileName);
json = text.text;
if (string.IsNullOrEmpty(json))
{
json = "";
Debug.LogError(fileName + "...json is null or empty!");
}
Debug.Log(json);
JObject jsonData = JObject.Parse(json);
JToken jToken = jsonData["level"];
JArray ja = JArray.Parse(jsonData["level"]["time"].ToString());
le = new LevelEntity();
le._floorCount = int.Parse(jToken["floorCount"].ToString());
le._maxNum = int.Parse(jToken["maxNum"].ToString());
le._left = int.Parse(jToken["left"].ToString());
le._right = int.Parse(jToken["right"].ToString());
for (int i = 0; i < ja.Count; i++)
{
string str1 = ja[i].ToString().Replace("{", "").Replace("}", "");
string[] str2 = str1.Split(':');
le._time.Add(float.Parse(str2[1]));
}
}边栏推荐
- Getting started with webrtc: 12 Rtendpoint and webrtcendpoint under kurento
- [original] typescript string UTF-8 encoding and decoding
- Design of a solar charge pump power supply circuit
- (original) custom drawable
- ThreadLocal再次挖掘它的知识点
- i=i++;
- 视频文件太大?使用FFmpeg来无损压缩它
- When multiple network devices exist, how to configure their Internet access priority?
- 借助原子变量,使用CAS完成并发操作
- 招聘需求 视觉工程师
猜你喜欢

Installation and use of SVN version controller

数字IC-1.9 吃透通信协议中状态机的代码编写套路

Object contains copy method?
为智能设备提供更强安全保护 科学家研发两种新方法

【生动理解】深度学习中常用的各项评价指标含义TP、FP、TN、FN、IoU、Accuracy

Apache POI的读写

MATLAB小技巧(19)矩阵分析--主成分分析

有關二叉樹的一些練習題

如何获取GC(垃圾回收器)的STW(暂停)时间?

Understand neural network structure and optimization methods
随机推荐
Obsidian 一周使用心得(配置、主题和插件)
Fake constructor???
Imx8qxp DMA resources and usage (unfinished)
提高效率 Or 增加成本,开发人员应如何理解结对编程?
Analysis of orthofinder lineal homologous proteins and result processing
orthofinder直系同源蛋白分析及结果处理
When multiple network devices exist, how to configure their Internet access priority?
Rockermq message sending and consumption mode
June 26, 2022 (LC 6100 counts the number of ways to place houses)
Semi supervised learning—— Π- Introduction to model, temporary assembling and mean teacher
内部类~锁~访问修饰符
[vivid understanding] the meanings of various evaluation indicators commonly used in deep learning TP, FP, TN, FN, IOU and accuracy
ServletConfig and ServletContext
CLassLoader
最全H桥电机驱动模块L298N原理及应用
ucore lab3
Object含有Copy方法?
Markem imaje马肯依玛士喷码机维修9450E打码机维修
初步认识pytorch
RMAN-08137 主库无法删除归档文件