Is there a way to utilize something like right clicking an equipable item in the inventory and it acting like I’m dragging and dropping it into the equipment slot while utilizing the inventory system?
like, say I have a sword equipped, and a sword in my inventory. I want to right click the sword in my inventory, and it swaps places with the sword equipped (or just sets itself to the equipped sword slot if there’s nothing in there)
Might be an advanced answer, but the best way is to use the Command pattern.
The command pattern allows you to encapsulate an instruction - or command - and then pass that command to something to act up. So in your case, when you drag the item, then drop it, the act of dropping would create the command and send it to be ran. Then, when right clicking, again, the right click would also create the same command, resulting in similar results whatever approach you take.
With the command pattern, you also get the added bonus of being able to implement an undo method for example, where the piece of code that runs your command could keep a list of the last ran command, and just undo it when called upon.
There are plenty of Unity command pattern on Youtube if you are interested…
While that is an option, let’s explore a quicker option, in the InventorySlotUI
First, you need to add a Button component to the prefab. I don’t mean a Button child GameObject, in this case I mean adding a component in the inspector called Button.
Now in the InventoryUI, let’s add this method:
public void ItemClicked()
{
if (GetItem() == null || GetNumber() <1) return;
if (GetItem() is EquipableItem equipableItem)
{
Equipment equipment = inventory.GetComponent<Equipment>();
EquipableItem equippedItem = equipment.GetItemInSlot(equipableItem.GetAllowedEquipLocation());
equipment.AddItem(equipableItem.GetAllowedEquipLocation(), equipableItem);
RemoveItems(1);
if(equippedItem!=null) AddItems(equippedItem, 1);
}
}
Finally, in the button component you’ve added to your InventorySlotUI component, add a reference to the InventorySlotUI script → ItemClicked() to the OnClick event.
Yes, I missed that you were specifically looking for left click. You’re quite right, Unity buttons don’t have a built in event for right clicking, we have to roll our own for that. Your solution should do the trick.