/// <summary> /// 解析物品的信息 /// </summary> void ParseItemJson() { itemList = new List <Item>(); //Resource加载text资源 TextAsset itemText = Resources.Load <TextAsset>("Items"); //将text转为json支持的jsonReader的类型 string itemJson = itemText.text; JsonReader itemReader = new JsonReader(itemJson); //获取json的对象 JsonData jsonData = JsonMapper.ToObject(itemReader); //遍历json对象 foreach (JsonData temp in jsonData) { string typeStr = temp["type"].ToString(); //将string 类转为object类,再转为type类型 Item.ItemType type = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), typeStr); int id = int.Parse(temp["id"].ToString()); string name = temp["name"].ToString(); Item.Quality quality = (Item.Quality)System.Enum.Parse(typeof(Item.Quality), temp["quality"].ToString()); string description = temp["description"].ToString(); int capacity = int.Parse(temp["capacity"].ToString()); int price = int.Parse(temp["price"].ToString()); int sellPrice = int.Parse(temp["sellPrice"].ToString()); string sprite = temp["sprite"].ToString(); Item item = null; switch (type) { case Item.ItemType.Consumable: int hp = int.Parse(temp["hp"].ToString()); int mp = int.Parse(temp["mp"].ToString()); item = new Consumable(id, name, type, quality, description, capacity, price, sellPrice, hp, mp, sprite); break; case Item.ItemType.Equipment: int strength = int.Parse(temp["strength"].ToString()); int intelligence = int.Parse(temp["intelligence"].ToString()); int agility = int.Parse(temp["agility"].ToString()); int stamina = int.Parse(temp["stamina"].ToString()); Equipment.EquipmentType equipType = (Equipment.EquipmentType)System.Enum.Parse(typeof(Equipment.EquipmentType), temp["equipmentType"].ToString()); item = new Equipment(id, name, type, quality, description, capacity, price, sellPrice, strength, intelligence, agility, stamina, equipType, sprite); break; case Item.ItemType.Weapon: int damage = int.Parse(temp["damage"].ToString()); Weapon.WeaponType weaponType = (Weapon.WeaponType)System.Enum.Parse(typeof(Weapon.WeaponType), temp["weaponType"].ToString()); item = new Weapon(id, name, type, quality, description, capacity, price, sellPrice, damage, weaponType, sprite); break; case Item.ItemType.Material: item = new Material(id, name, type, quality, description, capacity, price, sellPrice, sprite); break; default: break; } itemList.Add(item); } }
//public bool IsPickItem //{ // get { return isPickItem; } // set { IsPickItem = value; } //} #endregion #region 使用json解析物品信息,读取json中的物品信息 void ParseItemJson()//从json文件中取出所有物品信息 { itemList = new List <Item>(); TextAsset itemText = Resources.Load <TextAsset>("Items"); string itemJson = itemText.text;//物品信息的json格式 JsonData jd = JsonMapper.ToObject(itemJson); foreach (JsonData i in jd) { // print(i["type"]); //提取出类型,判断当前物品的类型 Item.ItemType it = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), i["type"].ToString()); //获取type属性字符串并将其转为枚举 //共有属性先提取出来 int id = int.Parse(i["id"].ToString()); // print(id); string name = i["name"].ToString(); // print(name); Item.Quality quality = (Item.Quality)System.Enum.Parse(typeof(Item.Quality), i["quality"].ToString()); // print(quality); string description = i["Description"].ToString(); //print(description); int capacity = int.Parse(i["Capacity"].ToString()); //print(capacity); int buyprice = int.Parse(i["BuyPrice"].ToString()); //print(buyprice); int sellprice = int.Parse(i["SellPrice"].ToString()); //print(sellprice); string sprite = i["sprite"].ToString(); //print(sprite); Item item = null;//创建item接收物体 switch (it) { case Item.ItemType.Consumable: int hp = int.Parse(i["hp"].ToString()); int mp = int.Parse(i["mp"].ToString()); item = new Consumable(hp, mp, id, name, it, quality, description, capacity, buyprice, sellprice, sprite); break; case Item.ItemType.Equipment: int stength = int.Parse(i["stength"].ToString()); int intellect = int.Parse(i["intellect"].ToString()); int agility = int.Parse(i["agility"].ToString()); int stamina = int.Parse(i["stamina"].ToString()); Equipment.EquipmentType equipType = (Equipment.EquipmentType)System.Enum.Parse(typeof(Equipment.EquipmentType), i["equipType"].ToString()); item = new Equipment(stength, intellect, agility, stamina, equipType, id, name, it, quality, description, capacity, buyprice, sellprice, sprite); break; case Item.ItemType.Weapon: int Damage = int.Parse(i["damage"].ToString());; Weapon.WeaponType wpType = (Weapon.WeaponType)System.Enum.Parse(typeof(Weapon.WeaponType), i["wpType"].ToString()); item = new Weapon(Damage, wpType, id, name, it, quality, description, capacity, buyprice, sellprice, sprite); break; case Item.ItemType.Material: //材料类 item = new Material(id, name, it, quality, description, capacity, buyprice, sellprice, sprite); break; default: break; } itemList.Add(item);//将物品保存在链表中 } }
void ParseItemJson() { itemList = new List <Item>(); TextAsset itemText = Resources.Load <TextAsset>("Items"); string itemJson = itemText.text;//物品信息的JSon格式 JSONObject j = new JSONObject(itemJson); foreach (JSONObject temp in j.list) { //遍历这个列表目的是要解析jSon文件中对应的属性 //这个JSon文件保存的是消耗品类型的属性,也就是说,这个对象就是Consumable类的实例对象中的属性 //对消耗品类中的私有类型进行解析 //首先解析item类型 string typeStr = temp["type"].str; Item.ItemType itemType = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), typeStr); //接下来进行解析基类中共有属性 int id = (int)temp["id"].n; string name = temp["name"].str; Item.Quality quality = (Item.Quality)System.Enum.Parse(typeof(Item.Quality), temp["quality"].str); string description = temp["description"].str; int capacity = (int)temp["capacity"].n; int buyPrice = (int)temp["buyPrice"].n; int sellPrice = (int)temp["sellPrice"].n; string sprite = temp["sprite"].str; //以上就将Json文件中的所有属性都解析完了,接下来就该用解析所得的属性,创建消费者对象了 Item consum = null;//这样也算是初始化了一个consum对象 switch (itemType) { case Item.ItemType.Consumble: int hp = (int)temp["hp"].n; int mp = (int)temp["mp"].n; //放在这里进行初始化对象 原因是hp 和mp是在选择语句这里进行赋值的所以,若不在这里初始化对象,那么就会报mp,hp值不确定的错误 //一定要注意 consum = new Consumable(id, name, itemType, quality, description, capacity, buyPrice, sellPrice, sprite, hp, mp); break; case Item.ItemType.Equipment: int strength = (int)temp["strength"].n; int intellect = (int)temp["intellect"].n; int agility = (int)temp["agility"].n; int stamina = (int)temp["stamina"].n; Equipment.EquipType equipType = (Equipment.EquipType)System.Enum.Parse(typeof(Equipment.EquipType), temp["equipType"].str); consum = new Equipment(id, name, itemType, quality, description, capacity, buyPrice, sellPrice, sprite, strength, intellect, agility, stamina, equipType); break; case Item.ItemType.Weapon: int damage = (int)temp["damage"].n; Weapon.WeaponType weaponType = (Weapon.WeaponType)System.Enum.Parse(typeof(Weapon.WeaponType), temp["weaponType"].str); consum = new Weapon(id, name, itemType, quality, description, capacity, buyPrice, sellPrice, sprite, damage, weaponType); break; case Item.ItemType.Material: consum = new Material(id, name, itemType, quality, description, capacity, buyPrice, sellPrice, sprite); break; } itemList.Add(consum); //print(consum); } }
/// <summary> /// 解析item的JSON文件 /// </summary> private void ParseItemInfo() { TextAsset ta = Resources.Load <TextAsset>("ItemJ"); JsonData items = JsonMapper.ToObject(ta.text); M_AllItem = new List <Item>(); Item m_item = null;//保存物品 if (items == null) { Debug.Log("没有解析到物品"); return; } foreach (JsonData item in items) { int id = (int)item["id"]; string name = item["name"].ToString(); ItemType itemType = (ItemType)System.Enum.Parse(typeof(ItemType), item["type"].ToString()); Item.Quality quality = (Item.Quality)System.Enum.Parse(typeof(Item.Quality), item["quality"].ToString()); string description = item["description"].ToString(); int capacity = (int)item["capacity"]; int buyprice = (int)item["buyprice"]; int sellprice = (int)item["sellprice"]; string spritePath = item["sprite"].ToString(); //图片地址 int hp, mp = 0; //hp和mp为Consumable和Equipment共有属性 //根据typee来区分不同类型装备的特有属性 switch (itemType) { case ItemType.Consumable: hp = (int)item["hp"]; mp = (int)item["mp"]; m_item = new Consumable(id, name, itemType, quality, description, capacity, buyprice, sellprice, spritePath, hp, mp); break; case ItemType.Equipment: int strength = (int)item["strength"]; //力量 int intellect = (int)item["intellect"]; //智力 int agility = (int)item["agility"]; //敏捷 int stamina = (int)item["stamina"]; //精力 hp = (int)item["hp"]; mp = (int)item["mp"]; Equipment.EquipmentType equipmentType = (Equipment.EquipmentType)System.Enum.Parse(typeof(Equipment.EquipmentType), item["EquipmentType"].ToString()); m_item = new Equipment(id, name, itemType, quality, description, capacity, buyprice, sellprice, spritePath, strength, intellect, agility, stamina, mp, hp, equipmentType); break; case ItemType.Weapon: Weapon.WeaponType weaponType = (Weapon.WeaponType)System.Enum.Parse(typeof(Weapon.WeaponType), item["weaponType"].ToString()); int damage = (int)item["damage"]; string weaponModel = item["weaponModel"] + ""; //武器模型位置 int attackDistance = (int)item["attackDistance"]; //Debug.Log(weaponModel); m_item = new Weapon(id, name, itemType, quality, description, capacity, buyprice, sellprice, spritePath, damage, weaponModel, attackDistance, weaponType); break; case ItemType.Materials: m_item = new Materials(id, name, itemType, quality, description, capacity, buyprice, sellprice, spritePath); break; } M_AllItem.Add(m_item); } }