5-12 Click To Put Item Into Slot
本章要來完成背包系統點擊物品的功能,按住Z鍵點擊物品時僅會取得一半的物品,按住Z鍵放置物品時則會一個一個的放進去。若直接點擊物品則能取得所有物品,也能直接放置物品,同時必須顧慮到物品的容量上限。
本次修改只有動到Slot.cs,而且程式碼解說已經寫在註解中了,請大家自己看看吧!如果不懂這次修改的內容,請記得將前幾篇相關的文章都看一下唷!
5-10 Click To Pickup Item
https://rpgcorecombat.blogspot.tw/2018/01/5-10-click-to-pickup-item.html
5-11 Play Scale Animation When Getting Item
也可以隨自己的喜好隨便放置物品。
本次修改只有動到Slot.cs,而且程式碼解說已經寫在註解中了,請大家自己看看吧!如果不懂這次修改的內容,請記得將前幾篇相關的文章都看一下唷!
5-10 Click To Pickup Item
https://rpgcorecombat.blogspot.tw/2018/01/5-10-click-to-pickup-item.html
5-11 Play Scale Animation When Getting Item
Slot.cs:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; namespace RPG.Inventory{ public class Slot : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler { [SerializeField] GameObject itemPrefab; public void StoreItem(Item item){ // 若Slot底下沒有Item,則Instantiate一個 if (transform.childCount == 0) { GameObject itemObject = Instantiate (itemPrefab, transform); itemObject.GetComponentInChildren().SetItem (item); } else { // 已存在該Item,所以增加數量即可 transform.GetComponentInChildren ().AddAmount (); } } public int GetItemID(){ return transform.GetComponentInChildren ().Item.ID; } public bool IsFilled(){ ItemUI itemUI = transform.GetComponentInChildren (); return itemUI.Amount >= itemUI.Item.Capacity; } public void OnPointerEnter(PointerEventData eventData){ if (transform.childCount > 0) { Item item = GetComponentInChildren ().Item; GetComponentInParent ().ShowToolTip (item.GetToolTipText ()); } } public void OnPointerExit(PointerEventData eventData){ GetComponentInParent ().HideToolTip (); } public void OnPointerDown(PointerEventData eventData){ InventorySystem invetorySystem = GetComponentInParent (); // Slot內是否已經有物體 if (transform.childCount > 0) { // 取得目前滑鼠點擊Slot的ItemUI ItemUI clickedItemUI = GetComponentInChildren (); // 當滑鼠沒有Item時 if (invetorySystem.isPickedItem == false) { if (Input.GetKey (KeyCode.Z)) { // 壓住Z鍵時,只會取出一半的數量 PickupHalfOfSlotItem(clickedItemUI, invetorySystem); } else { // 取出全部 PickupAllSlotItem (clickedItemUI, invetorySystem); } // 當滑鼠有Item時 } else if (invetorySystem.isPickedItem == true) { if (clickedItemUI.Item.ID == invetorySystem.GetPickedItem ().Item.ID) { if (Input.GetKey (KeyCode.Z)) { // 壓住Z鍵時,只會一個一個的放入物體 PutInOneItem (invetorySystem); } else { // 放入所有物體 PutInAllItem (clickedItemUI, invetorySystem); } } } } else { if (invetorySystem.isPickedItem == true) { if (Input.GetKey (KeyCode.Z)) { // 壓住Z鍵時,只會一個一個的放入物體 PutInOneItem (invetorySystem); } else { // 放入所有物體 PutInAllItemToEmptySlot (invetorySystem); } } } } private void PickupHalfOfSlotItem(ItemUI clickedItemUI, InventorySystem invetorySystem){ int amountToPick = (clickedItemUI.Amount + 1) / 2; int amountRemained = clickedItemUI.Amount - amountToPick; // 將PickedItem的ItemUI設置為目前滑鼠點中的ItemUI invetorySystem.PickupItem (clickedItemUI.Item, amountToPick); if (amountRemained == 0) { Destroy (clickedItemUI.gameObject); } else { clickedItemUI.SetAmount (amountRemained); } } private void PickupAllSlotItem(ItemUI clickedItemUI, InventorySystem invetorySystem){ // 將PickedItem的ItemUI設置為目前滑鼠點中的ItemUI invetorySystem.PickupItem (clickedItemUI.Item, clickedItemUI.Amount); // 因為已經選中物體了,要將Slot內的ItemUI刪除 Destroy (clickedItemUI.gameObject); } private void PutInOneItem(InventorySystem invetorySystem){ this.StoreItem (invetorySystem.GetPickedItem ().Item); invetorySystem.ReducePickedItem (1); } private void PutInAllItem(ItemUI clickedItemUI, InventorySystem invetorySystem){ if (clickedItemUI.Item.Capacity > clickedItemUI.Amount) { int slotRemained = clickedItemUI.Item.Capacity - clickedItemUI.Amount; if (slotRemained >= invetorySystem.GetPickedItem ().Amount) { // 若Slot內的空間足以放下滑鼠拿著的全部物體 clickedItemUI.SetAmount (clickedItemUI.Amount + invetorySystem.GetPickedItem ().Amount); invetorySystem.ReducePickedItem (invetorySystem.GetPickedItem ().Amount); } else { // Slot內的空間不夠放,只能放下部分滑鼠拿著的物體 clickedItemUI.SetAmount (clickedItemUI.Amount + slotRemained); invetorySystem.ReducePickedItem (slotRemained); } } } private void PutInAllItemToEmptySlot(InventorySystem invetorySystem){ for (int i = 0; i < invetorySystem.GetPickedItem ().Amount; i++) { this.StoreItem (invetorySystem.GetPickedItem().Item); } invetorySystem.ReducePickedItem (invetorySystem.GetPickedItem().Amount); } } }
撰寫完程式碼後,來執行遊戲看看吧,壓住Z鍵就可以取出部分物品了,而且可以在每個Slot各放一個物品。
也可以隨自己的喜好隨便放置物品。
留言
張貼留言