Inventory Context Menu Support
Right click --> Unequip, right click --> Drop... Wait, I wanted to consume you!
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).
Prerequisites
Install Unity
Install IDE (I use Visual Studio 2019)
Import Game Creator
Import and enable the Inventory module
General
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!
Adding Mouse Right Click Support
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.
Using statements
Add the following two using statements (above the Unity Editor directive):
Add right click button event
Add the following content after public Actions actions = null;
and before public ButtonActionsEvent onClick...
in the PROPERTIES section:
Modify the OnPointerClick() method
Modify the method OnPointerClick(PointerEventData eventData)
in the INTERFACES section:
Add the PressRight() method
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
Change the look of the Button Inspector
Add a serialized property
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:
Assign the added serialized property
Add this line of code to the OnEnable()
method after base.OnEnable()
and before SerializedProperty spActions...
:
Modify OnInspectorGUI()
Modify the OnInspectorGUI()
method to look like this:
Replace the button component
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.
Import ContextMenu Prefab And Script
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.
Add Context Menu Functionality
Add a property to the InventoryManager
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:
Add Properties to the ItemUI script
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 Methods to the ItemUI script
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.
Don't forget to assign the OnClickRight()
method to the right click event field of the GC UI Button in the item prefab!
Assign Variables 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 the functionality
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.
Last updated