1、目標
當在道具欄中選中一個Item時,點擊地面就可以實現Item的drop操作,每點擊一次就drop一次,直到道具欄中Item數量不夠。
這樣的好處:避免每次Drop都從道具欄中拖拉Item,通過點擊這種操作可以更加高效。
方法:通過EventHandler腳本創建Event,在Player腳本中處理點擊并觸發Event,在UIInvenrotySlot腳本中訂閱Event進行處理。
2、優化EventHandler腳本
添加如下事件代碼:
3、優化Player腳本
添加如下變量:
private GridCursor gridCursor;
添加Start方法:
private void Start()
{gridCursor = FindObjectOfType<GridCursor>();
}
在Update中添加如下代碼:
其具體實現如下:
private void PlayerClickInput()
{if (Input.GetMouseButton(0)){if (gridCursor.CursorIsEnabled){ProcessPlayerClickInput();}}
}
相關的代碼實現如下:
private void ProcessPlayerClickInput(){ResetMovement();// Get Selected item detailsItemDetails itemDetails = InventoryManager.Instance.GetSelectedInventoryItemDetails(InventoryLocation.player);if(itemDetails != null){switch (itemDetails.itemType){case ItemType.Seed:if (Input.GetMouseButtonDown(0)){ProcessPlayerClickInputSeed(itemDetails);}break;case ItemType.Commodity:if (Input.GetMouseButtonDown(0)){ProcessPlayerClickInputCommodity(itemDetails);}break;case ItemType.none:break;case ItemType.count:break;default:break;}}}private void ProcessPlayerClickInputSeed(ItemDetails itemDetails){if(itemDetails.canBeDropped && gridCursor.CursorPositionIsValid){EventHandler.CallDropSelectedItemEvent();}}private void ProcessPlayerClickInputCommodity(ItemDetails itemDetails){if(itemDetails.canBeDropped && gridCursor.CursorPositionIsValid){EventHandler.CallDropSelectedItemEvent();}}
4、優化UIInventorySlot腳本
在OnEnable方法中增加如下代碼,訂閱事件并進行處理。