The behaviour of inventory menus can be altered by hooking into the OnMenuElementClick event, which is run whenever the user clicks inside a menu.
This script demonstrates how to build custom inventory behaviour through script. It shows how the standard behaviour of left-clicking to select/combine, right-click to examine, can be moved to a custom script.
To use it:
- Set your InventoryBox element's "Inventory box type" property to "Custom Script"
- Paste the code below into a C# file named OverrideInventory.cs
- Attach the new Override Inventory component to a GameObject in the scene
OverrideInventory.cs:
using UnityEngine;
using AC;
public class OverrideInventory : MonoBehaviour
{
private void OnEnable ()
{
EventManager.OnMenuElementClick += ElementClick;
}
private void OnDisable ()
{
EventManager.OnMenuElementClick -= ElementClick;
}
private void ElementClick (Menu menu, MenuElement element, int slot, int buttonPressed)
{
MenuInventoryBox menuInventoryBox = (MenuInventoryBox) element;
if (menuInventoryBox != null && menuInventoryBox.inventoryBoxType == AC_InventoryBoxType.CustomScript)
{
// Clicked inside a custom InventoryBox element
InvInstance clickedInstance = menuInventoryBox.GetInstance (slot);
if (InvInstance.IsValid (clickedInstance))
{
// Clicked on a valid item slot
if (buttonPressed == 1)
{
// Left click
InvInstance currentSelectedInstance = KickStarter.runtimeInventory.SelectedInstance;
if (InvInstance.IsValid (currentSelectedInstance))
{
// Combine item with selected
clickedInstance.Combine (currentSelectedInstance);
}
else
{
// Select item
clickedInstance.Select ();
}
}
else if (buttonPressed == 2)
{
// Right click examine
clickedInstance.Examine ();
}
}
}
}
}