Skip to content

🧩 InventorySizeSync

Unity Integration Component — Scene-Driven Container Size Sync

InventorySizeSync is a small, optional glue component that applies the scene’s container size policy to a specific inventory container (e.g. Backpack) on a chosen owner object when the scene starts.

This allows designers to override container capacities per scene without modifying character or object prefabs.


✔ When to Use It

Use this component when you want:

  • The scene to determine the size of an inventory container
  • Characters, NPCs, vendors, or interactables to adopt scene rules automatically
  • Different levels to impose different backpack or storage capacities
  • Container sizes applied before UI or gameplay logic runs

Typical rules enforced with this component:

  • “All backpacks in this level have 20 slots”
  • “Vendors in this hub area have expanded storage”
  • “This challenge zone restricts capacity to encourage resource management”

🛠 How It Works

When the scene starts:

  1. Resolves a SceneInventoryService (if not explicitly assigned).
  2. Converts the Unity-facing container name (string) into a canonical ContainerId.
  3. Queries the scene’s ContainerSizePolicy for that container.
  4. If a policy entry exists, calls the service-level resize API:
invSvc.ResizeContainer(owner, (ContainerId)container, size, allowTruncate: false, out _);

This uses the same typed service boundary as all other inventory mutations, ensuring consistent behaviour.


🎮 Component Fields

Field Description
invSvc Optional reference to SceneInventoryService. Auto-resolved if left blank.
owner GameObject that owns the inventory container (player, NPC, vendor, chest).
container Unity-facing container name (default: "Backpack"). Converted to ContainerId at runtime.

🔀 Execution Order

[DefaultExecutionOrder(-350)]

This ensures container sizing is applied early, before:

  • Inventory UI bridges
  • Player setup logic
  • Post-initialisation scripts
  • Other Unity integration helpers

📦 Example Use Cases

Player Character

Attach to the player so each scene enforces its own backpack rules.

NPC Merchant

Different hubs can give merchants larger or smaller storage.

Loot Chest

Scene designers can change chest capacity without touching prefabs.

Procedural Spawns

Spawned characters inherit scene rules automatically by adding this component at spawn time.


🧪 Safety Notes

  • If no SceneInventoryService exists, the component safely does nothing.
  • If no owner is assigned, a warning is logged in the Editor only.
  • If no matching policy exists, nothing happens (no truncation risk).

It is always safe to include this component in a scene.


📜 Full Source (Reference)

using UnityEngine;

using RevGaming.RevFramework.Inventory.Abstractions;
using RevGaming.RevFramework.Inventory.Services;

namespace RevGaming.RevFramework.Inventory
{
    [DefaultExecutionOrder(-350)]
    public class InventorySizeSync : MonoBehaviour
    {
        public SceneInventoryService invSvc;
        public GameObject owner;
        public string container = "Backpack";

        private void Start()
        {
            // Resolve service if not assigned
            invSvc ??= InventoryResolve.ServiceFrom(this);

            if (!invSvc || !owner)
            {
#if UNITY_EDITOR
                if (!invSvc) Debug.LogWarning("[InventorySizeSync] No SceneInventoryService available.");
                if (!owner) Debug.LogWarning("[InventorySizeSync] No owner assigned.");
#endif
                return;
            }

            // Unity-facing string → typed ContainerId
            var cid = (ContainerId)container;

            var p = invSvc.sizePolicy?.TryGet(cid.Value);
            if (p.HasValue)
            {
                invSvc.ResizeContainer(owner, cid, p.Value, allowTruncate: false, out _);
            }
        }
    }
}

📝 Summary

InventorySizeSync is a safe, optional, scene-driven integration component used to:

  • Apply container size rules at scene start
  • Enforce per-level inventory constraints
  • Keep prefab data clean and decoupled
  • Give designers control without touching code

It lives in the Unity Integration layer — never required, but extremely useful when scene rules should trump prefab defaults.