This limits the amount of items you can add to your inventory from a container. To use it:
- In the Container menu, select the ContainerItems element and set its Click behaviour property to Select Item Only.
- Select the PlayerInventory element and set its Inventory box type property to Custom Script.
- Select the TakeAllButton element and set its Click type to Custom Script.
- Copy/paste the script below into a new C# file named LimitItemsFromContainer.
- Attach the new Limit Items From Container component to a GameObject in the scene.
LimitItemsFromContainer.cs:
using UnityEngine;
using AC;
public class LimitItemsFromContainer : MonoBehaviour
{
public int maxItems = 5;
public bool includeMultipleItemsInSameSlot = true;
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 == "Container")
{
if (element.title == "PlayerInventory")
{
MenuInventoryBox inventoryBox = element as MenuInventoryBox;
InvInstance selectedInstance = KickStarter.runtimeInventory.SelectedInstance;
InvInstance clickedInstance = inventoryBox.GetInstance (slot);
if (InvInstance.IsValid (selectedInstance))
{
Container container = selectedInstance.GetSourceContainer ();
if (InvInstance.IsValid (clickedInstance) && container)
{
if (!clickedInstance.IsMatch (selectedInstance, false))
{
KickStarter.runtimeInventory.PlayerInvCollection.Insert (selectedInstance, slot + element.GetOffset (), OccupiedSlotBehaviour.SwapItems);
KickStarter.runtimeInventory.SetNull ();
return;
}
}
if (!IsFull ())
{
inventoryBox.inventoryBoxType = AC_InventoryBoxType.Default;
inventoryBox.HandleDefaultClick (MouseState.SingleClick, slot);
inventoryBox.inventoryBoxType = AC_InventoryBoxType.CustomScript;
}
}
else
{
if (InvInstance.IsValid (clickedInstance))
{
clickedInstance.Select ();
}
}
}
else if (element.title == "TakeAllButton")
{
Container container = (menu.GetElementWithName ("ContainerItems") as MenuInventoryBox).OverrideContainer;
if (container == null) container = KickStarter.playerInput.activeContainer;
if (container == null) return;
if (IsFull ()) return;
for (int i = 0; i < container.InvCollection.InvInstances.Count; i++)
{
InvInstance invInstance = container.InvCollection.InvInstances[i];
if (!InvInstance.IsValid (invInstance)) continue;
invInstance.TransferCount = 1;
InvInstance transferInstance = invInstance.CreateTransferInstance ();
KickStarter.runtimeInventory.PlayerInvCollection.Add (transferInstance);
KickStarter.runtimeInventory.SetNull ();
i--;
if (IsFull ()) return;
}
}
}
}
private bool IsFull ()
{
int numItems = KickStarter.runtimeInventory.PlayerInvCollection.GetCount (includeMultipleItemsInSameSlot);
return numItems >= maxItems;
}
}