当前位置:网站首页>unity--newtonsoft. JSON parsing
unity--newtonsoft. JSON parsing
2022-06-27 09:27:00 【Gragragra】
1.level1.txt json file
{
"level": {
"floorCount": 2,
"maxNum": 2,
"left": 1,
"right": 1,
"time":[
{
"floor1":0.2
},
{
"floor2":0.5
}
]
}
}2.LevelEntity.cs json Entity class
[Serializable]
public class LevelEntity
{
public int _floorCount;
public int _maxNum;
public int _left;
public int _right;
public List<float> _time = new List<float>();
}3. Read StreamingAssets Under folder json file
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. Read Resources Under folder json file
// Read Resources Under the json file
public void ReadJson(string fileName)
{
// get Json character string
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]));
}
}边栏推荐
猜你喜欢

Privacy computing fat offline prediction

There is no doubt that this is an absolutely elaborate project

Some exercises about binary tree

Video file too large? Use ffmpeg to compress it losslessly

0号进程,1号进程,2号进程

高等数学第七章微分方程

支付宝微信支付业务流程图

Analysis of orthofinder lineal homologous proteins and result processing

Preliminary understanding of pytorch

我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
随机推荐
文件名设置导致writelines写入报错:OSError: [Errno 22] Invalid argument
ucore lab4
Obsidian 一周使用心得(配置、主题和插件)
Analysis of orthofinder lineal homologous proteins and result processing
That is, a one-stop live broadcast service with "smooth live broadcast" and full link upgrade
Getting started with webrtc: 12 Rtendpoint and webrtcendpoint under kurento
main()的参数argc与argv
ucore lab5
Installation and usage of source insight tool
js中的数组对象
JS 客户端存储
2022.06.26 (LC Luo 6101 Luo determines whether the matrix is an X matrix)
Semi supervised learning—— Π- Introduction to model, temporary assembling and mean teacher
About the problem that the El date picker Click to clear the parameter and make it null
Use CAS to complete concurrent operations with atomic variables
Markem imaje马肯依玛士喷码机维修9450E打码机维修
Process 0, process 1, process 2
多个类的设计
不容置疑,这是一个绝对精心制作的项目
ucore lab4