赞
踩
Unity内置的JsonUtility类解析Json文件
注意:右上角的两个按钮功能还没实现,保存和读档也不是特别难相信再看博客的你已经会做了。
这个是要解析的json表自己随便写的凑合用吧能实现效果就行了
{
“DaraItem”:
[
{
“Name”: “盖伦”,
“Hp”: [ 100, 200, 500 ],
“Mp”: [ 200, 300, 400 ],
“Skill”: [
{
“Name”: “Q技能”,
“type”: “追击”,
“mp”: [ 10, 20, 30 ],
“CD”: [ 1, 3, 5 ]
},
{
“Name”: “W技能”,
“type”: “护甲”,
“mp”: [ 10, 20, 30 ],
“CD”: [ 1, 7, 3 ]
},
{
“Name”: “E技能”,
“type”: “旋转”,
“mp”: [ 10, 20, 30 ],
“CD”: [ 10, 9, 1 ]
},
{
“Name”: “R技能”,
“type”: “制裁”,
“mp”: [ 10, 20, 30 ],
“CD”: [ 100, 70, 10 ]
}
]
},
{
“Name”: “赵信”,
“Hp”: [ 150, 250, 550 ],
“Mp”: [ 250, 350, 450 ],
“Skill”: [
{
“Name”: “Q技能”,
“type”: “鸣枪”,
“mp”: [ 3, 2, 1 ],
“CD”: [ 3, 2, 0 ]
},
{
“Name”: “W技能”,
“type”: “冲锋”,
“mp”: [ 30, 50, 100 ],
“CD”: [ 5, 7, 10 ]
},
{
“Name”: “E技能”,
“type”: “七进七出”,
“mp”: [ 200, 400, 800 ],
“CD”: [ 120, 100, 800 ]
},
{
“Name”: “R技能”,
“type”: “一打九”,
“mp”: [ 1000, 800, 500 ],
“CD”: [ 6000, 5000, 0 ]
}
]
}
]
}
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; [Serializable] public class DaraItem { /// <summary> /// 盖伦 /// </summary> public string Name; /// <summary> /// /// </summary> public List<int> Hp; /// <summary> /// /// </summary> public List<int> Mp; /// <summary> /// /// </summary> public List<SkillItem> Skill; }
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; [Serializable] public class SkillItem { /// <summary> /// Q技能 /// </summary> public string Name; /// <summary> /// 追击 /// </summary> public string type; /// <summary> /// /// </summary> public List<int> mp; /// <summary> /// /// </summary> public List<int> CD; }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
//用Unity自带的json解析集合必须添加这个标签
[Serializable]
public class Root_UnityJosn
{
public List<DaraItem> DaraItem;
}
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.IO; //存 //string json = JsonUtility.ToJson(Path); //取 //Player player = JsonUtility.FromJson<Player>(json); public class JsonUnityDemo : MonoBehaviour { public InputField _input; public Button Btn_ChaXun; public Button Btn_BaoChun; public Button Btn_DuDang; public Text text0; public Text text1; public Text text2; public Text text3; public Text text4; Root_UnityJosn r; void Start () { /// <summary> /// json文档路径 /// </summary> string Path = Application.dataPath + "/SceneDataDemo/Json/Unity集成JsonDemo/StreamingAssets/JsonUnityDemo.json"; string str = File.ReadAllText(Path); r = JsonUtility.FromJson<Root_UnityJosn>(str); Btn_ChaXun.onClick.AddListener(ChaXunClick); } void ChaXunClick() { if (_input.text!="") { foreach (var item in r.DaraItem) { if (_input.text == item.Name) { for (int a = 0; a < item.Hp.Count; a++) { //血量 text0.text = "名字:"+item.Name+"\n"+"Hp:"+ item.Hp[0]+"\n"+"Mp:"+item.Mp[0]; } for (int i = 0; i < item.Skill.Count; i++) { for (int j = 0; j < item.Skill[2].mp.Count; j++) { //技能名与类型 //技能消耗 text1.text = "名称:" + item.Skill[0].Name + "\n" + "类型:" + item.Skill[0].type + "\n" + "Mp:" + item.Skill[0].mp[0] + "\n" + "CD:" + item.Skill[0].CD[0]; text2.text = "名称:" + item.Skill[1].Name + "\n" + "类型:" + item.Skill[1].type + "\n" + "Mp:" + item.Skill[1].mp[0] + "\n" + "CD:" + item.Skill[1].CD[0]; text3.text = "名称:" + item.Skill[2].Name + "\n" + "类型:" + item.Skill[2].type + "\n" + "Mp:" + item.Skill[2].mp[0] + "\n" + "CD:" + item.Skill[2].CD[0]; text4.text = "名称:" + item.Skill[3].Name + "\n" + "类型:" + item.Skill[3].type + "\n" + "Mp:" + item.Skill[3].mp[0] + "\n" + "CD:" + item.Skill[3].CD[0]; } } } } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。