先贴个基础的测试代码:
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; [System.Serializable] public class JsonData { public string panelTypeString; public string path; } [System.Serializable] public class Item { public List<JsonData> infoList; } public enum Type { ItemMessage, SkillMessage, } public class UIPanelMgr : MonoBehaviour { void Start () { TextAsset itemAsset = Resources.Load<TextAsset>("Json/UIPanelType"); Item jsonList = JsonUtility.FromJson<Item>(itemAsset.text); foreach (var it in jsonList.infoList) { Type name = (Type)System.Enum.Parse(typeof(Type), it.panelTypeString); string path = it.path; Debug.Log(name); Debug.Log(path); } } void Update () { } } /* json: { "infoList": [ { "panelTypeString": "ItemMessage", "path": "UIPane/ItemMessagePanel" }, { "panelTypeString": "SkillMessage", "path": "UIPane/SkillMessagePanel" } ] } */
之后是这次写的主体逻辑代码
在最近的版本已经不需要json的插件了,U3D自带了一个JsonUtility类。
不过JsonUtility必须
[ { "id": 1, "name": "血瓶", "type": "Consumable", "quality": "Common", "description": "这个是用来加血的", "capacity": 10, "buyPrice": 10, "sellPrice": 5, "hp": 10, "mp": 0, "sprite": "Sprites/Items/hp" }, { "id": 2, "name": "蓝瓶", "type": "Consumable", "quality": "Common", "description": "这个是用来加蓝的", "capacity": 10, "buyPrice": 10, "sellPrice": 5, "hp": 0, "mp": 10, "sprite": "Sprites/Items/mp" }]
类似于这样的数组json文件解析, 必须返回一个object..所以解决的办法可以是类似于下面这样,先定义一个object类,之后再从中遍历的形式来解析。
{ "inforList": [ { "id": 1, "name": "血瓶", "type": "Consumable", "quality": "Common", "description": "这个是用来加血的", "capacity": 10, "buyPrice": 10, "sellPrice": 5, "hp": 10, "mp": 0, "sprite": "Sprites/Items/hp" }, { "id": 2, "name": "蓝瓶", "type": "Consumable", "quality": "Common", "description": "这个是用来加蓝的", "capacity": 10, "buyPrice": 10, "sellPrice": 5, "hp": 0, "mp": 10, "sprite": "Sprites/Items/mp" } ] }
u3d的代码解析部分:
using System.Collections; using System.Collections.Generic; using UnityEngine; using CommonLib; using System.Text; using System.IO; namespace ItemSystem { [System.Serializable] public class JsonData { public string name; public string type; public string quality; public string description; public string sprite; public int capacity; public int buyPrice; public int sellPrice; public int id; public int hp; public int mp; public int strength; public int intellect; public int agility; public int stamina; public string euqipType; } [System.Serializable] public class ItemList { public List<JsonData> inforList; } public class InventoryMgr : Singleton<InventoryMgr> { private List<Item> itemList=new List<Item>(); public void ParseItemJson() { TextAsset itemAsset=Resources.Load<TextAsset>("Json/Items"); string itemsJson = itemAsset.text; ItemList list = JsonUtility.FromJson<ItemList>(itemsJson); foreach(var item in list.inforList) { //先解析公共属性 Item.ItemType type =(Item.ItemType) System.Enum.Parse(typeof(Item.ItemType), item.type); Item.Quality quality = (Item.Quality)System.Enum.Parse(typeof(Item.Quality),item.quality); string description = item.description; string name = item.name; string sprite = item.sprite; int capacity = item.capacity; int buyPrice = item.buyPrice; int sellPrice = item.sellPrice; int id = item.id; Item myItem = null; switch (type) { case Item.ItemType.Consumable: int hp = item.hp; int mp = item.mp; myItem = new Consumable(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, hp, mp); break; case Item.ItemType.Equipment: int strengh = item.strength; int agility = item.agility; int intellect = item.intellect; int stamina = item.stamina; //myItem = new Equipment(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite,); break; case Item.ItemType.Weapon: break; case Item.ItemType.Material: break; } if (myItem!=null) { itemList.Add(myItem); Debug.Log(myItem); } } } } }
要注意的是:ItemList类中名称,必须与json文件中总的名称一致,否则无法解析
这个Mgr是个单例类,如果要运行,可自行改成单例 或者挂载调用。