The Save System in Ultimate Grid Inventory (UGI) provides a flexible and extensible way to persist inventory data between game sessions. It allows you to:
using Inventory.Scripts.Core.ScriptableObjects;using UnityEngine;public class InventorySaveManager : MonoBehaviour{ [SerializeField] private InventorySo playerInventory; // Save the inventory (e.g., when exiting the game) public void SaveInventory() { playerInventory.Save(); Debug.Log("Inventory saved successfully!"); } // Load the inventory (e.g., when starting the game) public void LoadInventory() { bool success = playerInventory.Load(); Debug.Log(success ? "Inventory loaded successfully!" : "No saved inventory found."); }}
You can implement auto-saving at key moments in your game:
// Auto-save when the player picks up an itempublic void OnItemPickedUp(ItemTable item){ // Add item to inventory... // Auto-save after important changes playerInventory.Save();}
The default LocalSaveStrategySo saves data to local files, but you can create custom save strategies for different storage methods (cloud saves, databases, etc.).
Create a new class that inherits from SaveStrategySo
Implement the Save and Load methods
Add a CreateAssetMenu attribute to make it available in the Unity menu
Here's an example of a custom save strategy that uses PlayerPrefs:
using Inventory.Scripts.Core.Controllers.Save;using UnityEngine;[CreateAssetMenu(menuName = "Inventory/Configuration/Save/PlayerPrefs Save Strategy")]public class PlayerPrefsSaveStrategySo : SaveStrategySo{ public override void Save(string key, string jsonEquippedItems) { PlayerPrefs.SetString($"UGI_{key}", jsonEquippedItems); PlayerPrefs.Save(); Debug.Log($"Saved inventory data to PlayerPrefs with key: UGI_{key}"); } public override string Load(string key) { string fullKey = $"UGI_{key}"; if (PlayerPrefs.HasKey(fullKey)) { return PlayerPrefs.GetString(fullKey); } Debug.LogWarning($"No save data found in PlayerPrefs with key: {fullKey}"); return null; }}
The save system automatically handles item metadata like stacks and container contents. For example, to access container contents after loading:
using Inventory.Scripts.Core.Items;using Inventory.Scripts.Core.Items.Metadata;using UnityEngine;public class ContainerContentExample : MonoBehaviour{ public void DisplayContainerContents(ItemTable containerItem) { if (!containerItem.IsContainer()) return; var containerMetadata = containerItem.GetMetadata<ContainerMetadata>(); if (containerMetadata == null) return; // Get all items from the container (including nested containers) var allItems = containerMetadata.GetAllItems(true); Debug.Log($"Container {containerItem.ItemDataSo.DisplayName} contains {allItems.Count} items:"); foreach (var item in allItems) { Debug.Log($"- {item.ItemDataSo.DisplayName}"); } }}
Implement different ways saving and loading in your game
Create custom save strategies for your specific needs
Integrate the save system with your game's UI
Add save/load functionality to your game's menu system
By leveraging the UGI save system, you can provide a seamless experience for players, ensuring their inventory progress is preserved between game sessions.