Redirect to the Docs main page.Docs

Scripting

Integrating World Containers with UGI

Learn how to implement interactive containers in your game world that open inventory interfaces when players interact with them.

Creating Interactive Containers in Your Game World

One of the most common inventory features in games is the ability for players to interact with containers in the game world - like chests, backpacks, or storage lockers. This guide explains how to implement this functionality using Ultimate Grid Inventory's Environment Container Holder component.

What is an Environment Container Holder?

The Environment Container Holder is a specialized component that:

  • Connects physical objects in your game world to the inventory system
  • Manages the display of inventory UI when players interact with containers
  • Handles the transfer of items between player inventory and world containers
  • Maintains the state of containers (open/closed, locked/unlocked)

Setting Up Environment Containers

To create an interactive container in your game world:

  1. Select or create the GameObject that will represent your container (chest, locker, etc.)
  2. Add the Environment Container Holder component:
    • In the Inspector, click Add Component
    • Search for "Environment Container Holder"
    • Configure the component settings:
      • Container Data SO: Reference to the container item data
      • Container Inventory SO: The inventory that stores this container's contents
      • Container Display Anchor SO: Reference to the UI anchor where the container will display

Environment Container Holder ComponentLoading image

Key Methods for Interaction

The Environment Container Holder provides several methods to control container interaction:

MethodDescription
OpenContainer()Opens the container and displays its inventory UI
CloseContainer()Closes the container and hides the inventory UI
ToggleEnvironmentContainer()Toggles between open and closed states
IsOpenProperty that returns whether the container is currently open
Lock()Prevents the container from being opened (if you implement locking mechanics)
Unlock()Allows the container to be opened again

Implementation Example

Here's a complete example of how to implement container interaction in your game:

using UnityEngine;
using Inventory.Scripts.Core.MonoBehaviours;
 
public class ContainerInteraction : MonoBehaviour
{
    [SerializeField] private EnvironmentContainerHolder containerHolder;
    [SerializeField] private float interactionDistance = 2f;
 
    private Camera mainCamera;
    private bool isInRange = false;
 
    private void Start()
    {
        mainCamera = Camera.main;
 
        // Optional: Add a visual indicator that this object is interactive
        if (TryGetComponent<Renderer>(out var renderer))
        {
            // You could add an outline shader or highlight effect here
        }
    }
 
    private void Update()
    {
        // Check if player is in range of the container
        CheckPlayerDistance();
 
        // Handle interaction input
        if (isInRange && Input.GetKeyDown(KeyCode.E))
        {
            // Toggle the container open/closed
            containerHolder.ToggleEnvironmentContainer();
 
            // Optional: Play sound effect
            if (containerHolder.IsOpen)
            {
                // Play open sound
                Debug.Log("Container opened");
            }
            else
            {
                // Play close sound
                Debug.Log("Container closed");
            }
        }
    }
 
    private void CheckPlayerDistance()
    {
        // Simple distance check - you might want to use raycasting or other methods
        float distance = Vector3.Distance(transform.position, Camera.main.transform.position);
        isInRange = distance <= interactionDistance;
 
        // Optional: Show interaction prompt when in range
        if (isInRange)
        {
            // Display "Press E to open" UI prompt
        }
    }
}

UI Configuration

For the container UI to display correctly:

  1. Make sure you've set up a Container Display in your UI scene
  2. Configure the Container Display Anchor SO to reference this display
  3. Assign the Container Display Anchor SO to your Environment Container Holder

For detailed instructions on setting up the container UI, see the Creating Container Display guide.

Saving Container Contents

To persist container contents between game sessions:

  1. Create a unique identifier for each container in your game world
  2. When saving the game, store the contents of each container's inventory
  3. When loading, restore the items to the appropriate container inventories

For a comprehensive guide to saving and loading inventory data, see the Save System documentation.

// Example save method
public void SaveContainerContents(string containerId, EnvironmentContainerHolder container, SaveStrategySo saveStrategy)
{
    // Get the container item
    var containerItem = container.GetItemEquipped();
 
    if (containerItem != null && containerItem.InventoryMetadata is ContainerMetadata metadata)
    {
        // Get all items from the container
        var items = metadata.GetAllItems();
 
        // Convert items to JSON and save
        string jsonData = JsonInventoryUtility.GetJson(items);
        saveStrategy.Save(containerId, jsonData);
    }
}

Next Steps

After implementing basic container interaction, consider these enhancements:

  • Add visual feedback when containers are interactive (glowing outline, icon)
  • Implement container locking mechanics (keys, lockpicking)
  • Create container types with different appearances and capacities
  • Add sound effects for opening, closing, and locking containers
  • Implement trapped containers that trigger effects when opened

By following this guide, you'll create interactive containers that seamlessly integrate with the Ultimate Grid Inventory system, allowing players to store and retrieve items throughout your game world.

On this page