//拖拽子功能 /// <summary> /// 组合物品 /// </summary> private void GroupMaterials(InventoryItemController ctrl) { ctrl.InitNum(ctrl.Num + num); if (ctrl.Num > 64) { Debug.Log("hhh"); CreateAndSettleNewItem(ctrl.Num - 64); ctrl.InitNum(64); } GameObject.Destroy(gameObject); }
/// <summary> /// 从合成槽将物品放回背包 /// </summary> public void AddItems(List <GameObject> list) { for (int i = 0; i < list.Count; i++) { RectTransform tempTransform = list[i].GetComponent <RectTransform>(); for (int j = 0; j < slotLists.Count; j++) { Transform bagTemp = slotLists[j].GetComponent <Transform>().Find("InVentoryItem"); if (bagTemp != null) { InventoryItemController bagCtrl = bagTemp.gameObject.GetComponent <InventoryItemController>(); InventoryItemController craftCtrl = list[i].GetComponent <InventoryItemController>(); if (bagCtrl.Id == craftCtrl.Id) { bagCtrl.InitNum(craftCtrl.Num + bagCtrl.Num); GameObject.Destroy(list[i]); break; } } else { tempTransform.SetParent(slotLists[j].GetComponent <Transform>()); tempTransform.localPosition = Vector3.zero; tempTransform.GetComponent <InventoryItemController>().ResetSpriteSize(tempTransform, 85, 85); break; } } } }
/// <summary> /// 向槽里放置一个物品 /// </summary> private void PutOneInSlot(GameObject targetSlot) { //槽里有物品 if (targetSlot.tag == "InVentoryItem") { InventoryItemController tempCtrl = targetSlot.GetComponent <InventoryItemController>(); //槽里物品id与当前拖拽物品id一致 if (tempCtrl.Id == id) { tempCtrl.InitNum(tempCtrl.Num + 1); //槽内物品数量加一 InitNum(num - 1); //拖拽中物品数量减一 if (num == 0) { GameObject.Destroy(gameObject); } } } //槽里没有物品且可放置(合成槽有放置规则) else if (targetSlot.tag == "InVentorySlot" || (targetSlot.tag == "CraftingSlot" && targetSlot.GetComponent <CraftingSlotController>().IsOpen&& id == targetSlot.GetComponent <CraftingSlotController>().Id)) { if (targetSlot.GetComponent <Transform>().Find("InVentoryItem") == null) { GameObject go = GameObject.Instantiate(gameObject, targetSlot.GetComponent <RectTransform>()); //复制物品 InitNewItem(go, 1, targetSlot.tag); //创建新的数量为1的物品放进槽中 InitNum(num - 1); //拖拽中物品数量减一 InventoryPanelController.Instance.SendDargMaterialItem(go); } else if (targetSlot.GetComponent <Transform>().Find("InVentoryItem") != null) { InventoryItemController tempCtrl = targetSlot.GetComponent <Transform>().Find("InVentoryItem").GetComponent <InventoryItemController>(); //槽里物品id与当前拖拽物品id一致 if (tempCtrl.Id == id) { tempCtrl.InitNum(tempCtrl.Num + 1); //槽内物品数量加一 InitNum(num - 1); //拖拽中物品数量减一 if (num == 0) { GameObject.Destroy(gameObject); } } } } }
/// <summary> /// 初始化复制的物品 /// </summary> /// <param name="go"></param> private void InitNewItem(GameObject go, int num, string tag) { InventoryItemController tempCtrl = go.GetComponent <InventoryItemController>(); RectTransform rect = go.GetComponent <RectTransform>(); rect.localPosition = Vector3.zero; //位置初始化 tempCtrl.InitId(id); //id初始化 tempCtrl.InitName(); //名称初始化,去掉clone tempCtrl.InitNum(num); //数量文本初始化 tempCtrl.GetComponent <CanvasGroup>().blocksRaycasts = true; if (tag == "InVentorySlot") { tempCtrl.ResetSpriteSize(go.GetComponent <RectTransform>(), 85, 85); } else { tempCtrl.ResetSpriteSize(go.GetComponent <RectTransform>(), 70, 62); } }