Inventory Context Menu Support
Right click --> Unequip, right click --> Drop... Wait, I wanted to consume you!
Last updated
Right click --> Unequip, right click --> Drop... Wait, I wanted to consume you!
Last updated
The documentation has moved to: https://mitschmr-studios.io/documentation/api-guides/inventorycontextmenusupport.html
This version will no longer be updated and maintained.
In this tutorial, I want to show you how to modify the Game Creator inventory to support a context menu when clicking the right mouse button and do the most common things ((Un-)Equip, Consume, Drop).
Install Unity
Install IDE (I use Visual Studio 2019)
Import Game Creator
Import and enable the Inventory module
Many games (like Zelda: Breath of the Wilds) use a context menu with buttons to do stuff, like equipping weapons, dropping items etc. The Game Creator Inventory doesn't support this out of the box. There are several changes that need to be done in order to support a context menu:
Modify the GC UI Button script to differentiate between right and left mouse clicks
Replace the Button component in the item prefab with the GC UI Button
Add a prefab for the context menu with buttons
Add a script to the prefab which stores references to the buttons (prefab and script attached in zip)
Modify the GC ItemUI script to add the necessary functionality for a context menu
Assign the variables in the modified ItemUI script
Make sure to have a backup of your project before doing anything of this, so you can revert changes made to the scripts!
There is currently no differentation between a left and a right mouse click on a button in Unity. Game Creator has its own UI button with additional functionality to the basic Unity UI Button. Open the ButtonActions
script, you can find it under Assets --> Plugins --> GameCreator --> Core --> Mono --> UI --> ButtonActions.cs.
Add the following two using statements (above the Unity Editor directive):
Add the following content after public Actions actions = null;
and before public ButtonActionsEvent onClick...
in the PROPERTIES section:
Modify the method OnPointerClick(PointerEventData eventData)
in the INTERFACES section:
Add the method PressRight()
in the PRIVATE METHODS section:
The || you see in the first if statement is a so called conditional logical OR operator: Reference
The GC UI Button layout is being replaced by a custom one. I have a tutorial on this too (Custom Editor Windows - Part 2). Open Assets --> Plugins --> GameCreator --> Core --> Editor --> UI --> ButtonActionsEditor.cs.
Add a Serialized Property for the OnClickRight event:
Add this line of code to the OnEnable()
method after base.OnEnable()
and before SerializedProperty spActions...
:
Modify the OnInspectorGUI()
method to look like this:
Now open an item prefab like the ItemRPG or ItemAdventure (depending on what skin you want to use), delete the button component on the root object and add the GC UI Button that we just modified. Add the action Call Method to the left click and add a new entry to right click. It should then look like this:
You don't have ItemUI.OnClickRight()
as of now, that changes in one of the next sections. Don't forget to set this when we add it.
I have prepared a script and a prefab for this. Download this zip file, extract it, import first the script then the prefab package into your project and you should be good:
The prefab is fully customizable, the script is necessary to hold a reference to the buttons. Make sure that the buttons are assigned to the variables:
In order to make sure that the references are correct and not somehow corrupted, clear them all (set to none) and readd them.
In order for the context menu to work correctly, we need to add a variable to hold a reference of it to the InventoryManager script. Open Assets --> Plugins --> GameCreator --> Inventory --> Mono --> Utilities --> InventoryManager.cs and add the following line to the PROPERTIES section:
Now let's add the functionality for the context menu. Open the item prefab you chose above and edit the ItemUI script. Add the following code below public GameObject equipped;
in the PROPERTIES section:
Add the System namespace as a using statement: using System;
Add the following line below InventoryManager.Instance.ConsumeItem(this.item.uuid);
in public virtual void OnClick()
to disable the context menu when you click on an item:
Then add all of the following code after public virtual void OnClick()
and before public virtual void OnDragBegin()
. The explanation to every method and its functionality is below the code block.
Method
Functionality
OnClickRight()
Gets executed when a right click on an item is registered.
ShowItemContextMenu()
Checks if a context menu is already added to the item and if a context menu was already opened, if not, then it instantiates one and adds it as a reference to the InventoryManager. This is necessary to close the context menus when you click on any item or open a context menu on another item.
HideItemContextMenu()
Tries to set the current context menu to inactive. This results in an error when you remove the last item of an item from the inventory. If this happens, it then sets the variable to null.
AddButtonListeners()
This adds a non-persistent listener for each button in the context menu of an item. The strange way of writing a listener <variableName>.AddListener(() => Method(argument1, argument2))
allows you to add arguments to a method that gets executed when the event is called. Otherwise you just write <variableName>.AddListener(MethodName)
. Note that you don't use the () brackets after the method name in this way.
SetButtonVisibility()
Gets the current selected item and compares different properties of it to display the suitable buttons. You don't want the button Equip when you already have equipped this item.
itemEquip()
Gets called when the button Equip is pressed. Equips the item.
itemUnequip()
Gets called when the button Unequip is pressed. Unequips the item.
itemConsume()
Gets called when the button Consume is pressed. Consumes the item.
itemDrop()
Gets called when the button Drop is pressed. Instantiates the item in front of the player with a distance of 1 and subtracts one. The distance value is hardcoded, but you can change it to any value you want.
Don't forget to assign the OnClickRight()
method to the right click event field of the GC UI Button in the item prefab!
The only thing left to do is add a value for the variables itemPrefab
and prefabContextMenu
in your chosen item prefab:
Test if everything is working as intended:
Context Menu appears when clicking the right mouse button on an item
Context Menu disappears when clicking the right mouse button on another item and a new context menu appears on the other item
Context Menu disappears when clicking the left mouse button on any item
(Un-)Equipping, consuming and dropping is working
It is not perfect, i.e. the context menu remains active when you close and reopen the inventory window. In order to iron out this behavior, further customization is needed, which I won't cover in this tutorial. It is possible that there will be a tutorial on how to do this in the future, but not now.