JsonUtility解析json文件

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];
                        }
                    }
                }
               
            }
        }
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值