void ParseItemJson() { itemList = new List <Item>(); TextAsset itemText = Resources.Load <TextAsset>("ItemJson"); string itemStr = itemText.text; JSONObject j = new JSONObject(itemStr); foreach (JSONObject temp in j.list) { int id = (int)temp["Id"].n; string name = temp["Name"].str; Item.ItemQuality quality = (Item.ItemQuality)System.Enum.Parse(typeof(Item.ItemQuality), temp["Quality"].str); string description = temp["Description"].str; int capacity = (int)temp["Capacity"].n; int buyPrice = (int)temp["BuyPrice"].n; int salePrice = (int)temp["SalePrice"].n; string sprite = temp["Sprite"].str; string typeStr = temp["ItemType"].str; Item.ItemTypeEnum itemType = (Item.ItemTypeEnum)System.Enum.Parse(typeof(Item.ItemTypeEnum), typeStr); Item item = null; switch (itemType) { case Item.ItemTypeEnum.Consumable: int hp = int.Parse(temp["Hp"].ToString()); int mp = int.Parse(temp["Mp"].ToString()); item = new Consumable(hp, mp, id, name, itemType, quality, description, capacity, buyPrice, salePrice, sprite); break; case Item.ItemTypeEnum.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.EquipType equipmentType = (Equipment.EquipType)System.Enum.Parse(typeof(Equipment.EquipType), temp["EquipmentType"].str); item = new Equipment(strength, Intelligence, agility, stamina, equipmentType, id, name, itemType, quality, description, capacity, buyPrice, salePrice, sprite); break; case Item.ItemTypeEnum.Weapon: int damage = int.Parse(temp["Damage"].ToString()); Weapon.WeaponEnum weaponType = (Weapon.WeaponEnum)System.Enum.Parse(typeof(Weapon.WeaponEnum), temp["WeaponType"].str); item = new Weapon(damage, weaponType, id, name, itemType, quality, description, capacity, buyPrice, salePrice, sprite); break; case Item.ItemTypeEnum.Material: break; default: break; } itemList.Add(item); } }
/// <summary> /// 解析json /// </summary> private void DecodeJson() { for (int i = 0; i < _itemData.Count; i++) { int itemId = (int)_itemData[i]["ItemID"]; string itemName = _itemData[i]["ItemName"].ToString(); string itemDesc = _itemData[i]["ItemDescription"].ToString(); string itemIcon = _itemData[i]["ItemIcon"].ToString(); int itemCount = (int)_itemData[i]["ItemCount"]; int itemQuality = (int)_itemData[i]["ItemQuality"]; int itemOperation = (int)_itemData[i]["ItemOperation"]; Item.ItemType itemType = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), _itemData[i]["ItemType"].ToString()); Item item = null; int itemPrice = (int)_itemData[i]["ItemPrice"]; switch (itemType) { case Item.ItemType.Unknown: break; case Item.ItemType.Weapon: int damage = (int)_itemData[i]["damage"]; item = new Weapon(itemId, itemName, itemDesc, itemIcon, itemCount, itemQuality, itemOperation, itemType, damage, itemPrice); break; case Item.ItemType.Equipment: int strength = (int)_itemData[i]["strength"]; int intellect = (int)_itemData[i]["intellect"]; int agility = (int)_itemData[i]["agility"]; int stamina = (int)_itemData[i]["stamina"]; Equipment.EquipType equipType = (Equipment.EquipType)System.Enum.Parse(typeof(Item.ItemType), _itemData[i]["equipType"].ToString()); item = new Equipment(itemId, itemName, itemDesc, itemIcon, itemCount, itemQuality, itemOperation, itemType, strength, intellect, agility, stamina, equipType, itemPrice); break; case Item.ItemType.Potion: int hp = (int)_itemData[i]["hp"]; int mp = (int)_itemData[i]["mp"]; item = new Potion(itemId, itemName, itemDesc, itemIcon, itemCount, itemQuality, itemOperation, itemType, hp, mp, itemPrice); break; } Debug.Log(item.ToString()); Items.Add(item); } }
void ParseItemJson() { itemList = new List <Item>(); TextAsset itemText = Resources.Load <TextAsset>("Json/ItemsInfo"); string itemsJson = itemText.text; JSONObject j = new JSONObject(itemsJson); foreach (JSONObject temp in j.list) { Item item = null; int id = (int)temp["id"].n; string name = temp["name"].str; string des = temp["des"].str; string sprite = temp["sprite"].str; int buyprice = (int)temp["buyPrice"].n; int sellprice = (int)temp["sellPrice"].n; int capacity = (int)temp["capacity"].n; string strtype = temp["type"].str; Item.ItemType type = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), strtype); switch (type) { case Item.ItemType.Consumable: JSONObject j3 = temp["applyAttr"]; List <ApplyAttrEffect> applyAttrEffects2 = new List <ApplyAttrEffect>(); if (j3.type == JSONObject.Type.ARRAY) { foreach (JSONObject temp2 in j3.list) { ApplyAttrEffect applyAttrEffect = null; AttrType attrType = (AttrType)System.Enum.Parse(typeof(AttrType), temp2["attrTpye"].str); bool positive = temp2[1].b; int value = (int)temp2[2].n; applyAttrEffect = new ApplyAttrEffect(attrType, positive, value); applyAttrEffects2.Add(applyAttrEffect); } } else if (j3.type == JSONObject.Type.OBJECT) { ApplyAttrEffect applyAttrEffect = null; AttrType attrType = (AttrType)System.Enum.Parse(typeof(AttrType), j3["attrTpye"].str); bool positive = j3[1].b; int value = (int)j3[2].n; applyAttrEffect = new ApplyAttrEffect(attrType, positive, value); applyAttrEffects2.Add(applyAttrEffect); } item = new Consumable(id, name, des, sprite, buyprice, sellprice, capacity, type, applyAttrEffects2); break; case Item.ItemType.Equipment: string stret = temp["equipType"].str; Equipment.EquipType equipType = (Equipment.EquipType)System.Enum.Parse(typeof(Equipment.EquipType), stret); JSONObject j2 = temp["applyAttr"]; List <ApplyAttrEffect> applyAttrEffects = new List <ApplyAttrEffect>(); if (j2.type == JSONObject.Type.ARRAY) { foreach (JSONObject temp2 in j2.list) { ApplyAttrEffect applyAttrEffect = null; AttrType attrType = (AttrType)System.Enum.Parse(typeof(AttrType), temp2["attrTpye"].str); bool positive = temp2[1].b; int value = (int)temp2[2].n; applyAttrEffect = new ApplyAttrEffect(attrType, positive, value); applyAttrEffects.Add(applyAttrEffect); } } else if (j2.type == JSONObject.Type.OBJECT) { ApplyAttrEffect applyAttrEffect = null; AttrType attrType = (AttrType)System.Enum.Parse(typeof(AttrType), j2["attrTpye"].str); bool positive = j2[1].b; int value = (int)j2[2].n; applyAttrEffect = new ApplyAttrEffect(attrType, positive, value); applyAttrEffects.Add(applyAttrEffect); } item = new Equipment(id, name, des, sprite, buyprice, sellprice, capacity, type, equipType, applyAttrEffects); break; case Item.ItemType.Materials: item = new Materials(id, name, des, sprite, buyprice, sellprice, capacity, type); break; case Item.ItemType.OtherItem: OtherItem.OtherItemType otherItemType = (OtherItem.OtherItemType)System.Enum.Parse(typeof(OtherItem.OtherItemType), temp["othertype"].str); switch (otherItemType) { case OtherItem.OtherItemType.Seed: int maxgrow = (int)temp["maxgrow"].n; int daygrow = (int)temp["daygrow"].n; int productid = (int)temp["productid"].n; JSONObject j4 = temp["diffsprites"]; List <string> diffsprites = new List <string>(); for (int i = 0; i < j4.list.Count; i++) { string diffsprite = j4[i].str; diffsprites.Add(diffsprite); } item = new ItemSeed(id, name, des, sprite, buyprice, sellprice, capacity, type, otherItemType, maxgrow, daygrow, productid, diffsprites); break; case OtherItem.OtherItemType.Pet: int petid = (int)temp["petid"].n; item = new ItemPet(id, name, des, sprite, buyprice, sellprice, capacity, type, otherItemType, petid); break; } 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); } }