/// <summary> /// 想背包中添加物品 /// </summary> public void Add(T item, ConstantDefine.BagItemType bagType, byte count = 1) { bool opned = m_canvas.activeSelf; m_canvas.SetActive(true); if (m_bagItems.ContainsKey(item.itemType)) { m_bagItems[item.itemType].count += count; //增加数量显示UI AddCountForIndexUI(m_bagItems[item.itemType].index, count); } else if (m_bagItems.Count < capacity) { //没有该物品,但背包不满 //创建物品UI GameObject itemUIGO = Object.Instantiate(Resources.Load("ui/item/item")) as GameObject; itemUIGO.GetComponent <Image>().sprite = Resources.Load <Sprite>("ui/item/" + item.itemInfo.name); BagItemUI bagItemUI = itemUIGO.GetComponent <BagItemUI>(); bagItemUI.bagType = bagType; bagItemUI.itemInfo = item.itemInfo; bagItemUI.type = item.itemType; //当选择此物品时,点击使用就会触发OnUsed bagItemUI.onUsedMethod = item.OnUsed; m_bagItems.Add(item.itemType, new BagItemData(count, (byte)m_bagItems.Count, itemUIGO)); //在背包UI中添加 Transform gridElementTF = m_bagGridTF.GetChild(m_bagItems.Count - 1); RectTransform itemUITF = itemUIGO.transform as RectTransform; itemUITF.SetParent(gridElementTF, false); //只在背景之上 itemUITF.SetSiblingIndex(1); //数量 Text countText = gridElementTF.GetChild(2).GetComponent <Text>(); countText.text = m_bagItems[item.itemType].count.ToString(); } else { //背包满,UI提示 } if (!opned) { m_canvas.SetActive(false); } }
public Bag(ConstantDefine.BagItemType type) { m_bagItems = new Dictionary <ConstantDefine.CollectionType, BagItemData>(capacity); bagType = type; }