当前位置:网站首页>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]));
}
}
边栏推荐
猜你喜欢
SVN版本控制器的安装及使用方法
有關二叉樹的一些練習題
数字IC-1.9 吃透通信协议中状态机的代码编写套路
Advanced mathematics Chapter 7 differential equations
Improving efficiency or increasing costs, how should developers understand pair programming?
Apache POI的读写
The largest rectangle in the bar graph of force buckle 84
Some exercises about binary tree
That is, a one-stop live broadcast service with "smooth live broadcast" and full link upgrade
Semi-supervised Learning入门学习——Π-Model、Temporal Ensembling、Mean Teacher简介
随机推荐
Enumeration? Constructor? Interview demo
NoSQL database redis installation
内存泄露的最直接表现
E+h secondary meter repair pH transmitter secondary display repair cpm253-mr0005
std::memory_order_seq_cst内存序
SVN版本控制器的安装及使用方法
The largest rectangle in the bar graph of force buckle 84
Understand neural network structure and optimization methods
One week's experience of using Obsidian (configuration, theme and plug-in)
C# 解决使用SQLite 的相对路径问题
文件名设置导致writelines写入报错:OSError: [Errno 22] Invalid argument
有關二叉樹的一些練習題
Matlab tips (19) matrix analysis -- principal component analysis
最全H桥电机驱动模块L298N原理及应用
Rough reading DS transunet: dual swing transformer u-net for medical image segmentation
There is no doubt that this is an absolutely elaborate project
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
了解神经网络结构和优化方法
Reading and writing Apache poi
JS 文件上传下载