//解析物品信息 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); } }
//获取物品列表 // // // private void Load() { ItemList = new Dictionary <int, Item>(); Weapon w1 = new Weapon(1, "Excalibur", Color.blue, "1"); Weapon w2 = new Weapon(2, "Gaebolg", Color.green, "1"); Consumable co1 = new Consumable(3, "red", Color.green, "1"); Consumable co2 = new Consumable(4, "blue", Color.green, "1"); Cailiao c1 = new Cailiao(5, "IceEye", Color.blue, "1"); Cailiao c2 = new Cailiao(6, "hushou", Color.green, "1"); ItemList.Add(w1.ID, w1); ItemList.Add(w2.ID, w2); ItemList.Add(co1.ID, co1); ItemList.Add(co2.ID, co2); ItemList.Add(c1.ID, c1); ItemList.Add(c2.ID, c2); }