Right clicking an item in the inventory

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)

1 Like

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.

1 Like

so this does left clicking, does unity allow right clicking for something like this to work? Or is that something that I’ll need to hard code myself?

Instead of adding a button script I added a new script to the inventory slot to handle left click, right click, and middle click as Unity Events.

public class UIClickHandler : MonoBehaviour, IPointerClickHandler
    {
        public UnityEvent onLeftClick;
        public UnityEvent onRightClick;
        public UnityEvent onMiddleClick;

        public void OnPointerClick(PointerEventData eventData)
        {
            if(eventData.pointerId == -1) { onLeftClick.Invoke(); }
            if(eventData.pointerId == -2) { onRightClick.Invoke(); }
            if(eventData.pointerId == -3) { onMiddleClick.Invoke(); }
        }
    }
1 Like

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.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms