By default, clicking items in the Player's inventory, inside the Container menu, will select them - allowing the Player to then click inside the ContainerItems element to transfer them.
This script automates the process, so that clicking such items moves them to the Container instantly.
To use it:
- Copy/paste the code below into a new C# script named AutoTransferToContainer.cs
- Attach the script as a new component in the scene (or, if using Unity UI, to the UI prefab's root)
- Fill in the Inspector's fields with details of the elements involved. Default values are set to match the default Container menu
- In the Menu Manager, select the PlayerInventory element of the Container menu, and check "Prevent selection?"
using UnityEngine;
using AC;
public class AutoTransferToContainer : MonoBehaviour
{
public string menuName = "Container";
public string inventoryBoxName = "PlayerInventory";
public string containerElementName = "ContainerItems";
private void OnEnable () { EventManager.OnMenuElementClick += OnMenuElementClick; }
private void OnDisable () { EventManager.OnMenuElementClick -= OnMenuElementClick; }
private void OnMenuElementClick (Menu menu, MenuElement element, int slot, int buttonPressed)
{
if (menu.title == menuName && element.title == inventoryBoxName)
{
MenuInventoryBox inventoryBox = element as MenuInventoryBox;
MenuInventoryBox containerBox = !string.IsNullOrEmpty (containerElementName) ? menu.GetElementWithName (containerElementName) as MenuInventoryBox : null;
Container container = (containerBox != null && containerBox.OverrideContainer) ? containerBox.OverrideContainer : KickStarter.playerInput.activeContainer;
if (container == null) return;
InvInstance clickedInstance = inventoryBox.GetInstance (slot);
container.Add (clickedInstance);
}
}
}