private IEnumerator SetupBag() { foreach (var obj in items) { Destroy(obj); } items.Clear(); foreach (var item in ItemManager.Instance.items.Values) { if (item == null || item.Count <= 0 || !DataManager.Instance.Items.ContainsKey(item.ID)) { continue; } ItemDefine define = DataManager.Instance.Items[item.ID]; GameObject obj = ResManager.Instance.Load <GameObject>(ResManager.ResourceType.Panel, "BagItemUI"); obj.transform.parent = content; obj.transform.localScale = Vector3.one; obj.transform.localPosition = Vector3.zero; BagItemUI bagItemUI = obj.GetComponent <BagItemUI>(); bagItemUI.Init(define, item.Count); items.Add(obj); } yield return(new WaitForEndOfFrame()); }
/// <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); } }