public virtual void OnPointerDown(PointerEventData eventData) //为虚函数,方便子类EquipmentSlot重写 { if (eventData.button == PointerEventData.InputButton.Right) //鼠标右键点击直接实现穿戴,不经过拖拽 { if (transform.childCount > 0 && InventroyManager.Instance.IsPickedItem == false) //需要穿戴的物品得有,并且鼠标上要没有物品,否则就发生:当鼠标上有物品,在其他物品上点击鼠标右键也能穿上这种情况。 { ItemUI currentItemUI = transform.GetChild(0).GetComponent <ItemUI>(); if (currentItemUI.Item is Equipment || currentItemUI.Item is Weapon) //只有装备和物品才可以穿戴 { Item currentItem = currentItemUI.Item; //临时存储物品信息,防止物品UI销毁导致物品空指针 currentItemUI.RemoveItemAmount(1); //当前物品槽中的物品减少1个 if (currentItemUI.Amount <= 0) //物品槽中的物品没有了 { DestroyImmediate(currentItemUI.gameObject); //立即销毁物品槽中的物品 InventroyManager.Instance.HideToolTip(); //隐藏该物品的提示框 } CharacterPanel.Instance.PutOn(currentItem); //直接穿戴 } } } if (eventData.button != PointerEventData.InputButton.Left) { return; //只有鼠标左键能够点击物品拖拽 } //情况分析如下: //一:自身是空 ///1.pickedItem != null(即IsPickedItem == true),pickedItem放在这个位置 ////①按下Ctrl键,放置当前鼠标上的物品的一个 ////②没有按下Ctrl键,放置当前鼠标上物品的所有 ///2.pickedItem==null(即IsPickedItem == false),不做任何处理 //二:自身不是空 ///1.pickedItem != null(即 IsPickedItem == true) ////①自身的id == pickedItem.id //////A.按下Ctrl键,放置当前鼠标上的物品的一个 //////B.没有按下Ctrl键,放置当前鼠标上物品的所有 ///////a.可以完全放下 ///////b.只能放下其中一部分 ////②自身的id != pickedItem.id,pickedItem跟当前物品交换 ///2.pickedItem == null(即IsPickedItem == false) ////①按下Ctrl键,取得当前物品槽中物品的一半 ////②没有按下Ctrl键,把当前物品槽里面的物品放到鼠标上 if (transform.childCount > 0) //二:自身不是空 { ItemUI currentItem = transform.GetChild(0).GetComponent <ItemUI>(); //取得当前的物品 if (InventroyManager.Instance.IsPickedItem == false) ///2.pickedItem == null。如果当前没有选中任何物品,即当前鼠标上没有物品 { if (Input.GetKey(KeyCode.LeftControl)) ////①按下Ctrl键,取得当前物品槽中物品的一半 { int amountPicked = (currentItem.Amount + 1) / 2; //如果物品为偶数就拾取刚好一般,如果为奇数就拾取一半多一个 InventroyManager.Instance.PickUpItem(currentItem.Item, amountPicked); int amountRemained = currentItem.Amount - amountPicked; //拾取后剩余的物品个数 if (amountRemained <= 0) //如果物品槽中没有剩余的物品了,就销毁ItemUI { Destroy(currentItem.gameObject); } else//如果物品槽中还有剩余的物品,就让它的数量减去被拾取的数量 { currentItem.SetAmount(amountRemained); } } else ////②没有按下Ctrl键,把当前物品槽里面的物品放到鼠标上 { //InventroyManager.Instance.PickedItem.SetItemUI(currentItem);//把当前物品槽里的物品复制给PickedItem(跟随鼠标移动) //InventroyManager.Instance.IsPickedItem = true;//注释的这两句合并为下面的一句 InventroyManager.Instance.PickUpItem(currentItem.Item, currentItem.Amount); Destroy(currentItem.gameObject);//复制完之后销毁当前物品槽中的物品 } } else { ///1.pickedItem != null(即 IsPickedItem == true) ////①自身的id == pickedItem.id //////A.按下Ctrl键,放置当前鼠标上的物品的一个 //////B.没有按下Ctrl键,放置当前鼠标上物品的所有 ///////a.可以完全放下 ///////b.只能放下其中一部分 ////②自身的id != pickedItem.id,pickedItem跟当前物品交换 if (currentItem.Item.ID == InventroyManager.Instance.PickedItem.Item.ID) { if (Input.GetKey(KeyCode.LeftControl)) { if (currentItem.Item.Capacity > currentItem.Amount) //如果物品槽里的物品容量大于当前物品数量,那就说明物品槽里还能再放 { currentItem.AddItemAmount(); //增加物品槽中物品数量 InventroyManager.Instance.ReduceAmountItem(); //让鼠标上的物品减掉一个(默认移除一个) } else//如果物品槽里的物品容量小于当前物品数量,那就说明物品槽里不能再放了 { return; } } else { if (currentItem.Item.Capacity > currentItem.Amount) //如果物品槽里的物品容量大于当前物品数量,那就说明物品槽里还能再放 { int itemRemain = currentItem.Item.Capacity - currentItem.Amount; //物品槽还可以再放的数量 if (itemRemain >= InventroyManager.Instance.PickedItem.Amount) //如果物品槽还可以再放的数量大于等于鼠标上的数量, 那就可以完全放下 { currentItem.AddItemAmount(InventroyManager.Instance.PickedItem.Amount); InventroyManager.Instance.ReduceAmountItem(itemRemain); } else//只能放下其中一部分 { currentItem.AddItemAmount(itemRemain); InventroyManager.Instance.PickedItem.RemoveItemAmount(itemRemain); } } else//如果物品槽里的物品容量小于当前物品数量,那就说明物品槽里不能再放了 { return; } } } else//②自身的id != pickedItem.id,pickedItem跟当前物品交换 { //保存当前鼠标捡起物品,用于和物品槽中的物品交换 Item pickedItemTemp = InventroyManager.Instance.PickedItem.Item; int pickedItemAmountTemp = InventroyManager.Instance.PickedItem.Amount; //保存当前物品槽中的物品,用于和鼠标上的物品交换 Item currentItemTemp = currentItem.Item; int currentItemAmountTemp = currentItem.Amount; //两者交换 currentItem.SetItem(pickedItemTemp, pickedItemAmountTemp); //把当前鼠标上的物品放入物品槽 InventroyManager.Instance.PickedItem.SetItem(currentItemTemp, currentItemAmountTemp); //把当前物品槽中的物品放在鼠标上 } } } else//一:自身是空 { ///1.pickedItem != null(即IsPickedItem == true),pickedItem放在这个位置 ////①按下Ctrl键,放置当前鼠标上的物品的一个 ////②没有按下Ctrl键,放置当前鼠标上物品的所有 ///2.pickedItem==null(即IsPickedItem == false),不做任何处理 if (InventroyManager.Instance.IsPickedItem == true) { if (Input.GetKey(KeyCode.LeftControl)) { this.StoreItem(InventroyManager.Instance.PickedItem.Item); InventroyManager.Instance.ReduceAmountItem(); } else//②没有按下Ctrl键,放置当前鼠标上物品的所有 { for (int i = 0; i < InventroyManager.Instance.PickedItem.Amount; i++) { this.StoreItem(InventroyManager.Instance.PickedItem.Item); } InventroyManager.Instance.ReduceAmountItem(InventroyManager.Instance.PickedItem.Amount); } } else { return; } } }
public override void OnPointerDown(PointerEventData eventData) { if (eventData.button != PointerEventData.InputButton.Left) { return; } // 当前Slot存放不为空 if (transform.childCount > 0) { ItemUI currentItem = transform.GetChild(0).GetComponent <ItemUI>(); // 鼠标未拾取物品 if (InventroyManager.Instance.IsPickedItem == false) { // 按下Ctrl键取得当前物品槽中物品的一半 if (Input.GetKey(KeyCode.LeftControl)) { int amountPicked = (currentItem.Amount + 1) / 2; InventroyManager.Instance.PickUpItem(currentItem.ItemDetail, amountPicked); int amountRemained = currentItem.Amount - amountPicked; if (amountRemained <= 0) { Destroy(currentItem.gameObject); } else { currentItem.SetAmount(amountRemained); } } // 否则全部拾取 else { InventroyManager.Instance.PickUpItem(currentItem.ItemDetail, currentItem.Amount); Destroy(currentItem.gameObject); } } // 鼠标已拾取物品 else { // 当前Slot的物品与拾取物品相同 if (currentItem.ItemDetail.m_Id == InventroyManager.Instance.PickedItem.ItemDetail.m_Id) { // 若按下ctrl键,则单个增加 if (Input.GetKey(KeyCode.LeftControl)) { if (currentItem.ItemDetail.m_Capacity > currentItem.Amount) { currentItem.AddItemAmount(); InventroyManager.Instance.ReduceAmountItem(); } else { return; } } // 否则增加最大数量 else { if (currentItem.ItemDetail.m_Capacity > currentItem.Amount) { int itemRemain = currentItem.ItemDetail.m_Capacity - currentItem.Amount; if (itemRemain >= InventroyManager.Instance.PickedItem.Amount) { currentItem.AddItemAmount(InventroyManager.Instance.PickedItem.Amount); InventroyManager.Instance.ReduceAmountItem(itemRemain); } else { currentItem.AddItemAmount(itemRemain); InventroyManager.Instance.PickedItem.RemoveItemAmount(itemRemain); } } else { return; } } } // 当前Slot的物品与拾取物品不同,进行交换 else { ItemDetail pickedItemTemp = InventroyManager.Instance.PickedItem.ItemDetail; int pickedItemAmountTemp = InventroyManager.Instance.PickedItem.Amount; ItemDetail currentItemTemp = currentItem.ItemDetail; int currentItemAmountTemp = currentItem.Amount; currentItem.SetItem(pickedItemTemp, pickedItemAmountTemp); InventroyManager.Instance.PickedItem.SetItem(currentItemTemp, currentItemAmountTemp); } } } // 当前Slot存放为空 else { // 鼠标拾取有物品,则放置 if (InventroyManager.Instance.IsPickedItem == true) { // 按下ctrl键,单个放置 if (Input.GetKey(KeyCode.LeftControl)) { this.StoreItem(InventroyManager.Instance.PickedItem.ItemDetail); InventroyManager.Instance.ReduceAmountItem(); } // 否则全部放置 else { for (int i = 0; i < InventroyManager.Instance.PickedItem.Amount; i++) { this.StoreItem(InventroyManager.Instance.PickedItem.ItemDetail); } InventroyManager.Instance.ReduceAmountItem(InventroyManager.Instance.PickedItem.Amount); } } // 否则返回 else { return; } } }
/** * 监听鼠标点击事件 * */ public virtual void OnPointerDown(PointerEventData eventData) { if (eventData.button == PointerEventData.InputButton.Right) { if (transform.childCount > 0 && InventroyManager.Instance.IsPickedItem == false) { ItemUI currentItemUI = transform.GetChild(0).GetComponent <ItemUI>(); ItemDetail itemDetail = currentItemUI.ItemDetail; // 右键点击装备物品 if (itemDetail is Equipment) { EquipmentInventroy.Instance.PutOn(itemDetail); currentItemUI.RemoveItemAmount(1); if (currentItemUI.Amount <= 0) { DestroyImmediate(currentItemUI.gameObject); InventroyManager.Instance.HideToolTip(); } } // 右键点击使用消耗品 if (currentItemUI.ItemDetail.m_Type == (ItemDetail.ItemType)System.Enum.Parse(typeof(ItemDetail.ItemType), "Consumable")) { Consumable e = (Consumable)itemDetail; Player player = GameObject.Find("GM").GetComponent <GlobeManager>().GetPlayer("郭靖"); player.M_BattleProperty.M_CurrentHp += e.m_Add_Hp; player.M_BattleProperty.M_CurrentMp += e.m_Add_Mp; currentItemUI.RemoveItemAmount(1); if (currentItemUI.Amount <= 0) { DestroyImmediate(currentItemUI.gameObject); InventroyManager.Instance.HideToolTip(); } } } } if (eventData.button != PointerEventData.InputButton.Left) { return; } // 当前Slot存放不为空 if (transform.childCount > 0) { ItemUI currentItem = transform.GetChild(0).GetComponent <ItemUI>(); // 鼠标未拾取物品 if (InventroyManager.Instance.IsPickedItem == false) { // 按下Ctrl键取得当前物品槽中物品的一半 if (Input.GetKey(KeyCode.LeftControl)) { int amountPicked = (currentItem.Amount + 1) / 2; InventroyManager.Instance.PickUpItem(currentItem.ItemDetail, amountPicked); int amountRemained = currentItem.Amount - amountPicked; if (amountRemained <= 0) { Destroy(currentItem.gameObject); } else { currentItem.SetAmount(amountRemained); } } // 否则全部拾取 else { InventroyManager.Instance.PickUpItem(currentItem.ItemDetail, currentItem.Amount); Destroy(currentItem.gameObject); } } // 鼠标已拾取物品 else { // 当前Slot的物品与拾取物品相同 if (currentItem.ItemDetail.m_Id == InventroyManager.Instance.PickedItem.ItemDetail.m_Id) { // 若按下ctrl键,则单个增加 if (Input.GetKey(KeyCode.LeftControl)) { if (currentItem.ItemDetail.m_Capacity > currentItem.Amount) { currentItem.AddItemAmount(); InventroyManager.Instance.ReduceAmountItem(); } else { return; } } // 否则增加最大数量 else { if (currentItem.ItemDetail.m_Capacity > currentItem.Amount) { int itemRemain = currentItem.ItemDetail.m_Capacity - currentItem.Amount; if (itemRemain >= InventroyManager.Instance.PickedItem.Amount) { currentItem.AddItemAmount(InventroyManager.Instance.PickedItem.Amount); InventroyManager.Instance.ReduceAmountItem(itemRemain); } else { currentItem.AddItemAmount(itemRemain); InventroyManager.Instance.PickedItem.RemoveItemAmount(itemRemain); } } else { return; } } } // 当前Slot的物品与拾取物品不同,进行交换 else { ItemDetail pickedItemTemp = InventroyManager.Instance.PickedItem.ItemDetail; int pickedItemAmountTemp = InventroyManager.Instance.PickedItem.Amount; ItemDetail currentItemTemp = currentItem.ItemDetail; int currentItemAmountTemp = currentItem.Amount; currentItem.SetItem(pickedItemTemp, pickedItemAmountTemp); InventroyManager.Instance.PickedItem.SetItem(currentItemTemp, currentItemAmountTemp); } } } // 当前Slot存放为空 else { // 鼠标拾取有物品,则放置 if (InventroyManager.Instance.IsPickedItem == true) { // 按下ctrl键,单个放置 if (Input.GetKey(KeyCode.LeftControl)) { this.StoreItem(InventroyManager.Instance.PickedItem.ItemDetail); InventroyManager.Instance.ReduceAmountItem(); } // 否则全部放置 else { for (int i = 0; i < InventroyManager.Instance.PickedItem.Amount; i++) { this.StoreItem(InventroyManager.Instance.PickedItem.ItemDetail); } InventroyManager.Instance.ReduceAmountItem(InventroyManager.Instance.PickedItem.Amount); } } // 否则返回 else { return; } } }