当前位置:网站首页>unity--newtonsoft.json解析
unity--newtonsoft.json解析
2022-06-27 09:26: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]));
}
}边栏推荐
- Reading and writing Apache poi
- 内部类~锁~访问修饰符
- Object contains copy method?
- 2022.6.26-----leetcode. seven hundred and ten
- Conception de plusieurs classes
- 看看volatile你深知多少
- How much memory does the data type occupy? LongVsObject
- How do I get the STW (pause) time of a GC (garbage collector)?
- orthofinder直系同源蛋白分析及结果处理
- Win10 add right-click menu for any file
猜你喜欢
![[system design] proximity service](/img/02/57f9ded0435a73f86dce6eb8c16382.png)
[system design] proximity service

I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!

How do I get the STW (pause) time of a GC (garbage collector)?

A classic interview question covering 4 hot topics

Improving efficiency or increasing costs, how should developers understand pair programming?

Process 0, process 1, process 2

Preliminary understanding of pytorch

Obsidian 一周使用心得(配置、主题和插件)
为智能设备提供更强安全保护 科学家研发两种新方法

提高效率 Or 增加成本,开发人员应如何理解结对编程?
随机推荐
js中的数组对象
IMX8QXP DMA资源和使用(未完结)
集合框架 泛型LinkedList TreeSet
[ 扩散模型(Diffusion Model) ]
Win10 add right-click menu for any file
std::memory_ order_ seq_ CST memory order
Video file too large? Use ffmpeg to compress it losslessly
Quelques exercices sur les arbres binaires
One week's experience of using Obsidian (configuration, theme and plug-in)
0号进程,1号进程,2号进程
Analysis of key technologies for live broadcast pain points -- second opening, clarity and fluency of the first frame
There is no doubt that this is an absolutely elaborate project
Matlab tips (19) matrix analysis -- principal component analysis
main()的参数argc与argv
ucore lab3
Quick start CherryPy (1)
快速入门CherryPy(1)
webrtc入门:12.Kurento下的RtpEndpoint和WebrtcEndpoint
看看volatile你深知多少
Tips for using Jupiter notebook