/// <summary> /// 解析物品信息 /// </summary> void ParseItemJson() { itemList = new List <Item>(); //文本为在Unity里面是 TextAsset类型 TextAsset itemText = Resources.Load <TextAsset>("Items"); string itemsJson = itemText.text;//物品信息的Json格式 JSONObject j = new JSONObject(itemsJson); foreach (JSONObject temp in j.list) { string typeStr = temp["type"].str; Item.ItemType type = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), typeStr); //下面的事解析这个对象里面的公有属性 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 sellPrice = (int)(temp["sellPrice"].n); string sprite = temp["sprite"].str; Item item = null; switch (type) { case Item.ItemType.Consumable: //int hp = (int)(temp["hp"].n); //int mp = (int)(temp["mp"].n); int exp = (int)(temp["exp"].n); item = new Consumable(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, exp); 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; int hp = (int)temp["hp"].n; Equipment.EquipmentType equipType = (Equipment.EquipmentType)System.Enum.Parse(typeof(Equipment.EquipmentType), temp["equipType"].str); item = new Equipment(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, hp, equipType); break; case Item.ItemType.Weapon: // int damage = (int)temp["damage"].n; //Weapon.WeaponType wpType = (Weapon.WeaponType)System.Enum.Parse(typeof(Weapon.WeaponType), temp["weaponType"].str); item = new Weapon(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, damage); break; case Item.ItemType.Material: // item = new Material(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite); break; } itemList.Add(item); //Debug.Log(item); } }
void ParseItemJson() { itemList = new List <Item>(); TextAsset itemText = Resources.Load <TextAsset>("Items"); string itemsJson = itemText.text; JSONObject j = new JSONObject(itemsJson); Debug.Log(string.Format("{0} {1} {2} {3}", j.IsArray, j.isContainer, j.Count, j.list.Count)); foreach (JSONObject temp in j.list) { string typeStr = temp["type"].str; Item.ItemType type = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), typeStr); 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 sellPrice = (int)(temp["sellPrice"].n); string sprite = temp["sprite"].str; Item item = null; switch (type) { case Item.ItemType.Consumable: int hp = (int)(temp["hp"].n); int mp = (int)(temp["mp"].n); item = new Consumable(id, name, type, 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.EquipmentType equipType = (Equipment.EquipmentType)System.Enum.Parse(typeof(Equipment.EquipmentType), temp["equipType"].str); item = new Equipment(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, strength, intellect, agility, stamina, equipType); break; case Item.ItemType.Weapon: int damage = (int)(temp["damage"].n); Weapon.WeaponType wpType = (Weapon.WeaponType)System.Enum.Parse(typeof(Weapon.WeaponType), temp["weaponType"].str); item = new Weapon(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, damage, wpType); break; case Item.ItemType.Material: item = new Material(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite); break; } itemList.Add(item); //Debug.Log(item); } }
void ParseItemJson() { itemList = new List <Item>(); TextAsset itemText = Resources.Load <TextAsset>("Items"); JsonData itemsData = JsonMapper.ToObject(itemText.text); Item item = null; foreach (JsonData itemData in itemsData) { int id = (int)itemData["id"]; string itemName = itemData["name"].ToString(); Item.ItemQuality quality = (Item.ItemQuality)Enum.Parse(typeof(Item.ItemQuality), itemData["quality"].ToString()); string des = itemData["description"].ToString(); int capacity = (int)itemData["capacity"]; int buyPrice = (int)itemData["buyPrice"]; int sellPrice = (int)itemData["sellPrice"]; string sprite = itemData["sprite"].ToString(); switch (itemData["type"].ToString()) { case "Consumable": int hp = (int)itemData["hp"]; int mp = (int)itemData["mp"]; item = new Consumable(id, itemName, Item.ItemType.Consumable, quality, des, capacity, buyPrice, sellPrice, sprite, hp, mp); break; case "Equipment": int strength = (int)itemData["strength"]; int intellect = (int)itemData["intellect"]; int agility = (int)itemData["agility"]; int stamina = (int)itemData["stamina"]; Equipment.EquipmentType equipmentType = (Equipment.EquipmentType)Enum.Parse(typeof(Equipment.EquipmentType), itemData["equipmentType"].ToString()); item = new Equipment(id, itemName, Item.ItemType.Consumable, quality, des, capacity, buyPrice, sellPrice, sprite, strength, intellect, agility, stamina, equipmentType); break; case "Weapon": int damage = (int)itemData["damage"]; Weapon.WeaponType weaponType = (Weapon.WeaponType)Enum.Parse(typeof(Weapon.WeaponType), itemData["weaponType"].ToString()); item = new Weapon(id, itemName, Item.ItemType.Consumable, quality, des, capacity, buyPrice, sellPrice, sprite, damage, weaponType); break; case "Material": item = new Material(id, itemName, Item.ItemType.Consumable, quality, des, capacity, buyPrice, sellPrice, sprite); break; } itemList.Add(item); //Debug.Log(item); } }
/// <summary> /// 解析JSON /// </summary> private void ParseItemsJSON() { itemList = new List <Item>(); TextAsset ta = Resources.Load <TextAsset>("items"); JSONObject j = new JSONObject(ta.text); foreach (JSONObject temp in j.list) { string itemType = temp["type"].str; Item.ItemType type = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), itemType); int id = (int)temp["id"].n; //公用物品解析 string name = temp["name"].str; //string Quality = temp["quality"].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 sellPrice = (int)(temp["sellPrice"].n); string sprite = temp["sprite"].str; Item item = null; switch (type) { case Item.ItemType.Consumable: int hp = (int)temp["hp"].n; int mp = (int)temp["mp"].n; item = new Consumable (id, name, type, 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.EquipmentType equipType = (Equipment.EquipmentType)System.Enum.Parse(typeof(Equipment.EquipmentType), temp["equipType"].str); item = new Equipment (id, name, type, 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); item = new Weapon(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, damage, weaponType); break; case Item.ItemType.Material: item = new Material(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite); break; } itemList.Add(item); //Debug.Log(item); } }
/// <summary> /// 解析物品Json /// </summary> public void ParseItemJson() { TextAsset textAsset = Resources.Load <TextAsset>("Items"); string itemsJson = textAsset.text; //List<Item> itemList = JsonConvert.DeserializeObject<List<Item>>(itemsJson); JSONObject j = new JSONObject(itemsJson); foreach (JSONObject temp in j.list) { int id = (int)temp["id"].n; string name = temp["name"].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; Item.ItemQuality quality = (Item.ItemQuality)System.Enum.Parse(typeof(Item.ItemQuality), temp["quality"].str); Item.ItemType type = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), temp["type"].str); Item item = null; switch (type) { case Item.ItemType.Consumable: int hp = (int)temp["hp"].n; int mp = (int)temp["mp"].n; item = new Consumable(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, hp, mp); break; case Item.ItemType.Equipment: int strength = (int)temp["strength"].n; int intelligence = (int)temp["intelligence"].n; int agility = (int)temp["agility"].n; int stamina = (int)temp["stamina"].n; Equipment.EquipmentType equipType = (Equipment.EquipmentType)System.Enum.Parse(typeof(Equipment.EquipmentType), temp["equipType"].str); item = new Equipment(id, name, type, quality, description, capacity, buyPrice, sellPrice, strength, intelligence, agility, stamina, equipType, sprite); 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); item = new Weapon(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, damage, weaponType); break; case Item.ItemType.Material: item = new Material(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite); break; default: break; } itemList.Add(item); } }
//解析物品信息 void ParseItemJson() { itemList = new List <Item>(); //文本在Unity里面是TextAsset类型 TextAsset itemText = Resources.Load <TextAsset>("Items"); string itemsJson = itemText.text;//物品信息的Json格式 JSONObject j = new JSONObject(itemsJson); foreach (JSONObject temp in j.list) { string typeStr = temp["type"].str; Item.ItemType type = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), typeStr); //下面的是解析这个对象里面的公有属性 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 des = temp["des"].str; int capacity = (int)(temp["capacity"].n); int buyPrice = (int)(temp["buyPrice"].n); int sellPrice = (int)(temp["sellPrice"].n); string sprite = temp["sprite"].str; Item item = null; switch (type) { case Item.ItemType.Xiaohaopin: int hp = (int)(temp["hp"].n); int mp = (int)(temp["mp"].n); item = new Xiaohaopin(id, name, type, quality, des, capacity, buyPrice, sellPrice, sprite, hp, mp); break; case Item.ItemType.Zhuangbei: int strength = (int)temp["strength"].n; int intellect = (int)temp["intellect"].n; int agility = (int)temp["agility"].n; int stamina = (int)temp["stamina"].n; Zhuangbei.ZhuangbeiType zbType = (Zhuangbei.ZhuangbeiType)System.Enum.Parse(typeof(Zhuangbei.ZhuangbeiType), temp["zbType"].str); item = new Zhuangbei(id, name, type, quality, des, capacity, buyPrice, sellPrice, sprite, strength, intellect, agility, stamina, zbType); break; case Item.ItemType.Wuqi: int damage = (int)temp["damage"].n; Wuqi.WuqiType wqType = (Wuqi.WuqiType)System.Enum.Parse(typeof(Wuqi.WuqiType), temp["wqType"].str); item = new Wuqi(id, name, type, quality, des, capacity, buyPrice, sellPrice, sprite, damage, wqType); break; case Item.ItemType.Cailiao: item = new Cailiao(id, name, type, quality, des, capacity, buyPrice, sellPrice, sprite); break; } itemList.Add(item); Debug.Log(item); } }
/// <summary> /// 解析Json文件 /// </summary> public void ParseItemJson() { itemList = new List <Item>(); //文本在unity里面是TextAsset类型 TextAsset itemText = Resources.Load <TextAsset>("Item"); //加载json文本 string itemJson = itemText.text; //得到json文本里面的内容 //bug.Log(itemJson); JSONObject j = new JSONObject(itemJson); foreach (var temp in j.list) { //物品类型字符串转化为枚举类型 Item.ItemType type = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), temp["type"].str); //print(type); //下面解析的是物品的共有属性:id,name,quality。。。注:temp["id"].n 的 .n,.str等是json插件的写法,用于解析 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 sellPrice = (int)(temp["sellPrice"].n); string sprite = temp["sprite"].str; Item item = null; switch (type) { case Item.ItemType.Consumable: int hp = (int)(temp["hp"].n); int mp = (int)(temp["mp"].n); item = new Consumable(id, name, type, 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.EquipmentType equiType = (Equipment.EquipmentType)System.Enum.Parse(typeof(Equipment.EquipmentType), temp["equipType"].str); item = new Equipment(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, strength, intellect, agility, stamina, equiType); 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); item = new Weapon(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, damage, weaponType); break; case Item.ItemType.Material: item = new Material(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite); break; } itemList.Add(item);//把解析到的物品信息加入物品列表里面 Debug.Log(item); } }
public void ParseItemJson() { Items = new Dictionary <int, Item>(); //文本为在Unity里面是 TextAsset类型 TextAsset itemText = Resources.Load <TextAsset>("Item"); string itemsJson = itemText.text;//物品信息的Json格式 JSONObject j = new JSONObject(itemsJson); foreach (JSONObject temp in j.list) { string typeStr = temp["type"].str; Item.ItemType type = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), typeStr); //下面的事解析这个对象里面的公有属性 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 sellPrice = (int)(temp["sellPrice"].n); string sprite = temp["sprite"].str; Item item = null; switch (type) { case Item.ItemType.Consumable: int function = (int)(temp["function"].n); int value = (int)(temp["value"].n); item = new Consumable(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, function, value); break; case Item.ItemType.Equipment: Equipment.EquipmentType equipType = (Equipment.EquipmentType)System.Enum.Parse(typeof(Equipment.EquipmentType), temp["equipType"].str); JSONObject equipProperty = temp["equipProperty"]; int[] property = new int[21]; int i = 0; foreach (JSONObject temp2 in equipProperty.list) { property[i] = (int)temp2.n; i++; } item = new Equipment(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, property, equipType); break; case Item.ItemType.Material: // item = new Material(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite); break; } Items.Add(id, item); } }
/// <summary> /// 解析物品信息 /// </summary> private void ParseItemJson() { //文本在Unity里是TextAsset类型 itemText = Resources.Load <TextAsset>("ItemJson"); //string itemContent = itemText.ToString(); string itemContent = itemText.text; JsonData json = JsonMapper.ToObject(itemContent); //下面的是解析这个对象里面的共有属性 for (int i = 0; i < json.Count; i++) { int id = int.Parse(json[i]["id"].ToString()); string name = json[i]["name"].ToString(); Item.ItemType type = (Item.ItemType)Enum.Parse(typeof(Item.ItemType), json[i]["type"].ToString()); Item.ItemQuality quality = (Item.ItemQuality)Enum.Parse(typeof(Item.ItemQuality), json[i]["quality"].ToString()); string description = json[i]["description"].ToString(); int capacity = int.Parse(json[i]["capacity"].ToString()); int buyPrice = int.Parse(json[i]["buyPrice"].ToString()); int sellPrice = int.Parse(json[i]["sellPrice"].ToString()); string sprite = json[i]["sprite"].ToString(); Item item = null; switch (type) { case Item.ItemType.Consumable: int hp = int.Parse(json[i]["hp"].ToString()); int mp = int.Parse(json[i]["mp"].ToString()); item = new Consumable(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, hp, mp); break; case Item.ItemType.Equipment: int strength = int.Parse(json[i]["strength"].ToString()); int intellect = int.Parse(json[i]["intellect"].ToString()); int agility = int.Parse(json[i]["agility"].ToString()); int stamina = int.Parse(json[i]["stamina"].ToString()); Equipment.EquipmentType equipType = (Equipment.EquipmentType)Enum.Parse(typeof(Equipment.EquipmentType), json[i]["equipType"].ToString()); item = new Equipment(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, strength, intellect, agility, stamina, equipType); break; case Item.ItemType.Weapon: int damage = int.Parse(json[i]["damage"].ToString()); Weapon.WeaponType wpType = (Weapon.WeaponType)Enum.Parse(typeof(Weapon.WeaponType), json[i]["weaponType"].ToString()); item = new Weapon(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, damage, wpType); break; case Item.ItemType.Material: item = new Material(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite); break; } itemList.Add(item); } }
/// <summary> /// 解析物品信息 - Json,添加到 list 里 /// </summary> private void ParseItemJson() { itemList = new List <Item>(); // 加载 Resources 下的 json 文件,文本为在 Unity 里面是 TextAsset 类型 TextAsset itemText = Resources.Load <TextAsset>("Items"); string itemsJson = itemText.text; // 物品信息的 Json 格式文本 JSONObject jo = new JSONObject(itemsJson); foreach (JSONObject temp in jo.list) { // 解析 Items 里面的公有属性 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 sellPrice = (int)(temp["sellPrice"].n); string sprite = temp["sprite"].str; string typeStr = temp["type"].str; // 解析 type,不同类型的物体拥有各自不同的属性 Item.ItemType type = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), typeStr); // 将 string 转 枚举 Item item = null; switch (type) { case Item.ItemType.Consumable: int hp = (int)(temp["hp"].n); int mp = (int)(temp["mp"].n); item = new Consumable(id, name, type, 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.EquipmentType equipType = (Equipment.EquipmentType)System.Enum.Parse(typeof(Equipment.EquipmentType), temp["equipType"].str); item = new Equipment(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, strength, intellect, agility, stamina, equipType); break; case Item.ItemType.Weapon: int damage = (int)temp["damage"].n; Weapon.WeaponType wpType = (Weapon.WeaponType)System.Enum.Parse(typeof(Weapon.WeaponType), temp["weaponType"].str); item = new Weapon(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, damage, wpType); break; case Item.ItemType.Material: item = new Material(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite); break; } itemList.Add(item); } }
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> /// 解析物品信息 /// </summary> void ParseItemJson() { itemList = new List <Item>(); //文本为在Unity里面是 TextAsset类型 TextAsset itemText = Resources.Load <TextAsset>("Items"); string itemsJson = itemText.text;//物品信息的Json格式 JsonData data = JsonMapper.ToObject(itemsJson); for (int i = 0; i < data.Count; i++) { string typeStr = data[i]["type"].ToString(); Item.ItemType type = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), typeStr); //下面的事解析这个对象里面的公有属性 int id = int.Parse((data[i]["id"]).ToString()); string name = data[i]["name"].ToString(); Item.ItemQuality quality = (Item.ItemQuality)System.Enum.Parse(typeof(Item.ItemQuality), data[i]["quality"].ToString()); string description = data[i]["description"].ToString(); int capacity = int.Parse((data[i]["capacity"]).ToString()); int buyPrice = int.Parse((data[i]["buyPrice"]).ToString()); int sellPrice = int.Parse((data[i]["sellPrice"]).ToString()); string sprite = data[i]["sprite"].ToString(); Item item = null; switch (type) { case Item.ItemType.Consumable: int hp = int.Parse(data[i]["hp"].ToString()); int mp = int.Parse(data[i]["mp"].ToString()); item = new Consumable(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, hp, mp); break; case Item.ItemType.Equipment: int strength = int.Parse(data[i]["strength"].ToString()); int intellect = int.Parse(data[i]["intellect"].ToString()); int agility = int.Parse(data[i]["agility"].ToString()); int stamina = int.Parse(data[i]["stamina"].ToString()); Equipment.EquipmentType equipType = (Equipment.EquipmentType)System.Enum.Parse(typeof(Equipment.EquipmentType), data[i]["equipType"].ToString()); item = new Equipment(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, strength, intellect, agility, stamina, equipType); break; case Item.ItemType.Weapon: int damage = int.Parse(data[i]["damage"].ToString()); Weapon.WeaponType wpType = (Weapon.WeaponType)System.Enum.Parse(typeof(Weapon.WeaponType), data[i]["weaponType"].ToString()); item = new Weapon(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, damage, wpType); break; } itemList.Add(item); //Debug.Log(item); } }
/// <summary> /// 解析物品信息 /// </summary> void ParseItemsJson() { itemList = new List <Item>(); TextAsset itemText = Resources.Load <TextAsset>("ResJson/Items"); string itemJson = itemText.text; JSONObject js = new JSONObject(itemJson); foreach (JSONObject temp in js.list) { //Debug.Log(temp["id"].ToString()+ temp["name"].ToString()); string typeStr = temp["type"].str; Item.ItemType type = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), typeStr); //下面的是解析这个独享的共有属性 int id = (int)(temp["id"].n); //Debug.Log("ParseItemsJson id=" + id); 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 sellPrice = (int)(temp["sellPrice"].n); string sprite = temp["sprite"].str; Item item = null; switch (type) { case Item.ItemType.Consumable: int hp = (int)(temp["hp"].n); int mp = (int)(temp["mp"].n); item = new Consumable(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, hp, mp); break; case Item.ItemType.Equipment: //TODO int strength = (int)temp["strength"].n; //Debug.Log("strength="+strength); int intellect = (int)temp["intellect"].n; //Debug.Log(intellect); int agility = (int)temp["agility"].n; //Debug.Log(agility); int stamina = (int)temp["stamina"].n; //Debug.Log(stamina); Equipment.EquipmentType equipmentType = (Equipment.EquipmentType)System.Enum.Parse(typeof(Equipment.EquipmentType), temp["equipmentTypes"].str); //Debug.Log(equipmentType); item = new Equipment(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, strength, intellect, agility, stamina, equipmentType); break; case Item.ItemType.Weapon: //TODO int damage = (int)temp["damage"].n; Weapon.WeaponType wpType = (Weapon.WeaponType)System.Enum.Parse(typeof(Weapon.WeaponType), temp["WeaponType"].str); item = new Weapon(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, damage, wpType); break; case Item.ItemType.Material: //TODO item = new Material(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite); break; } itemList.Add(item); //Debug.Log(item); } }
/// <summary> /// 解析物品信息 /// </summary> void ParseItemJson() { m_itemList = new List <Item>(); //加载文本 TextAsset TextAsset itemText = Resources.Load <TextAsset>("Items"); string itemsJson = itemText.text; JsonData jsonObject = JsonMapper.ToObject(itemsJson); for (int i = 0; i < jsonObject.Count; i++) { int id = (int)jsonObject[i]["id"]; string name = (string)jsonObject[i]["name"]; string description = (string)jsonObject[i]["description"]; int buyprice = (int)jsonObject[i]["buyprice"]; int capacity = (int)jsonObject[i]["capacity"]; int sellprice = (int)jsonObject[i]["sellprice"]; string sprite = (string)jsonObject[i]["sprite"]; string typeStr = (string)jsonObject[i]["type"]; Item.ItemType type = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), typeStr); string qualityStr = (string)jsonObject[i]["quality"]; Item.ItemQuality quality = (Item.ItemQuality)System.Enum.Parse(typeof(Item.ItemQuality), qualityStr); Item item = null; switch (type) { case Item.ItemType.Consumable: int hp = (int)jsonObject[i]["hp"]; int mp = (int)jsonObject[i]["mp"]; item = new Consumable(id, name, type, quality, description, capacity, buyprice, sellprice, sprite, hp, mp); break; case Item.ItemType.Equipment: int strength = (int)jsonObject[i]["strength"]; int intellect = (int)jsonObject[i]["intellect"]; int agility = (int)jsonObject[i]["agility"]; int stamina = (int)jsonObject[i]["stamina"]; string equipTypeStr = (string)jsonObject[i]["equipType"]; Equipment.EquipmentType equipType = (Equipment.EquipmentType)System.Enum.Parse(typeof(Equipment.EquipmentType), equipTypeStr); item = new Equipment(id, name, type, quality, description, capacity, buyprice, sellprice, sprite, strength, intellect, agility, stamina, equipType); break; case Item.ItemType.Material: item = new Material(id, name, type, quality, description, capacity, buyprice, sellprice, sprite); break; case Item.ItemType.Weapon: int damage = (int)jsonObject[i]["damage"]; string weaponTypeStr = (string)jsonObject[i]["weaponType"]; Weapon.WeaponType weaponType = (Weapon.WeaponType)System.Enum.Parse(typeof(Weapon.WeaponType), weaponTypeStr); item = new Weapon(id, name, type, quality, description, capacity, buyprice, sellprice, sprite, damage, weaponType); break; } m_itemList.Add(item); Debug.Log(item); } }