public BagItemInfo(EquipListItem it, BagController ctl) { GameObject.Destroy(it.obj); Obj = ResManager.Instance.LoadPrefabFromRes("Prefab/Bagitem", true); Image = it.sprite; ItemCtl = Obj.GetComponent <BagItemController>(); ItemCtl.SetController(ctl); this.id = it.id; this.name = it.name; this.type = it.type; this.count = it.count; this.gain = it.gain; this.discribe = it.discribe; }
//添加item public bool AddItem(BaseItem it) { int id = it.id; int itemNum = m_bagModel.BagItemsDic.Count; int firstEmptySlot = -1; //背包中已经有这个id的物品 var enumerator = m_bagModel.BagItemsDic.GetEnumerator(); while (enumerator.MoveNext()) { if (m_bagModel.BagItemsDic[enumerator.Current.Key].id == id && (it.type == EquipType.bullet_556 || it.type == EquipType.medicine_s)) { //只有药品和子弹的数量可以叠加 if (it.type == EquipType.bullet_556) { enumerator.Current.Value.count += it.count; ChangeItemNum(enumerator.Current.Value, enumerator.Current.Value.count); Debug.Log(it.count); ChangeBulletCount(EquipType.bullet_556, it.count); return(true); } else if (it.type == EquipType.medicine_s) { enumerator.Current.Value.count += it.count; ChangeItemNum(enumerator.Current.Value, enumerator.Current.Value.count); ChangeMedicineCount(EquipType.medicine_s, it.count); return(true); } } } //背包中没有这个物品,遍历背包槽,找到第一个空槽 for (int i = 0; i < m_bagView.m_slots.Count; i++) { //Debug.Log(i + " : " + m_bagView.m_slots[i].isEmpty); if (m_bagView.m_slots[i].isEmpty == true) { firstEmptySlot = i; break; } } //背包还有空位 if (firstEmptySlot != -1) { //更新背包存放item的字典 //实例化一个item GameObject obj = ResManager.Instance.LoadPrefabFromRes("Prefab/BagItem", true); Sprite sprite = ResManager.Instance.LoadSpriteFromRes(it.imageName); //初始化item,加载名字,数量,图片 Transform numberObj = obj.transform.GetChild(0); Text number = numberObj.GetComponent <Text>(); number.text = it.count.ToString(); Image img = obj.transform.GetChild(1).GetComponent <Image>(); img.sprite = sprite; Text name = obj.transform.GetChild(2).GetComponent <Text>(); name.text = it.name; //赋值id,传递bagController BagItemController itemView = obj.GetComponent <BagItemController>(); itemView.SetController(this); itemView.SetDicKey(firstEmptySlot); //实例化一个BagItemInfo BagItemInfo itInfo = new BagItemInfo(id, it.name, sprite, it.count, it.type, obj, it.ScenePrefabPath, it.gain); //挂载item到空槽中 MountPrefabToSlot(itInfo, firstEmptySlot); } return(true); }