当前位置:   article > 正文

JsonUtility解析json文件

jsonutility

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
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;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
//用Unity自带的json解析集合必须添加这个标签
[Serializable]
public class Root_UnityJosn
{
    public List<DaraItem> DaraItem;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号