/// <summary> /// 添加物品到箱子里 /// </summary> /// <param name="itemData">物品的数据</param> /// <returns>返回没有添加进去的物品数量</returns> public int AddItem(ItemData itemData) { if (itemData.number == 0) { return(0); } // 先判断箱子里是否已经有重复的物品,如果有就叠加在一起 for (int i = 0; i < box.Count; i++) { // 找到重复物品并且还有叠加的位置 if (box[i].itemData.name == itemData.name && box[i].itemData.number < box[i].maxNumber) { // 如果物品的叠加数量超过最大限制就进行叠加后将多余的物品继续进行下一轮查找 if (box[i].itemData.number + itemData.number > box[i].maxNumber) { itemData.number = box[i].itemData.number + itemData.number - box[i].maxNumber; box[i].itemData.number = box[i].maxNumber; box[i].UpdateNumberText(); } // 否则就能够填满 else { box[i].itemData.number = box[i].itemData.number + itemData.number; box[i].UpdateNumberText(); return(0); } } } // 查找空的格子进行装填 for (int i = 0; i < boxGrids.transform.childCount; i++) { GridUI gridUI = boxGrids.transform.GetChild(i).GetComponent <GridUI>(); if (gridUI.Article == null) { GameObject temp = Instantiate(ResourcePath.Single.articleUIs[itemData.name]); ArticleUI articleUI = temp.GetComponent <ArticleUI>(); articleUI.ChangeGrid(gridUI); box.Add(articleUI); // 如果可以叠加完的话 if (itemData.number <= articleUI.maxNumber) { articleUI.itemData = itemData.Clone(); articleUI.UpdateNumberText(); return(0); } // 不能叠加还得继续找空格子创建物品 else { articleUI.itemData = itemData.Clone(); articleUI.itemData.number = articleUI.maxNumber; itemData.number -= articleUI.maxNumber; articleUI.UpdateNumberText(); } } } // 找不到空格子后,返回还剩下的物品数量 return(itemData.number); }
protected override void Awake() { base.Awake(); WS = GameObject.Find("Store").GetComponent <websockets>(); articleUI = GameObject.Find("ArticleUI").GetComponent <ArticleUI>(); cart = GameObject.Find("ArticleUI/Canvas/AddToCartBtn/AddToCart").GetComponent <cartHandler>(); buyHandler = GameObject.Find("Store/Checkout/CheckoutCanvas/Buy").GetComponent <BuyHandler>(); dot = m_Camera.GetComponentInChildren <Image>(); m_Data = new PointerEventData(eventSystem); }
public void OnDrop(PointerEventData eventData) { // 如果拖动的是“可拖动UI”就什么也不做 if (eventData.pointerDrag.tag == "DragUI") { return; } // 如果当前格子上有物品 if (Article != null) { ArticleUI temp = eventData.pointerDrag.GetComponent <ArticleUI>(); // 将一样的物品进行堆叠 if (Article.itemData.name == temp.itemData.name) { // 将拖拽的物品的格子对该物品的引用置空 temp.ArticleGrid.Article = null; Box box = transform.parent.parent.GetComponent <Box>(); // 清除箱子对该物品的引用 box.box.Remove(temp); // 进行物品添加 box.AddItem(temp.itemData.Clone()); // 删除拖拽的该物品 Destroy(temp.gameObject); } // 否则将这个物品移动到用户拖拽过来的物品之前的格子(交换位置) else { Article.ChangeGrid(temp.ArticleGrid); } } eventData.pointerDrag.transform.SetParent(transform); // 新来的物品设置为自身的子物品 Article = eventData.pointerDrag.GetComponent <ArticleUI>(); // 拿到引用物品 Article.ArticleGrid = this; // 将物品对格子的引用设为自己 }
private void Update() { articleUI = GameObject.Find("ArticleUI").GetComponent <ArticleUI>(); }
private void Awake() { WS = GameObject.Find("Store").GetComponent <websockets>(); articleUI = GameObject.Find("ArticleUI").GetComponent <ArticleUI>(); }
private void Start() { rectTransform = GetComponent <RectTransform>(); Article = transform.childCount >= 1 ? transform.GetChild(0).GetComponent <ArticleUI>() : null; }