Saving generated items
Don't serialize the item — serialize the recipe. A snapshot is six plain fields that regenerate the identical item on load.
Why not just JSON the ItemInstance?
Because it holds live asset references — Rarity, ItemType, Icon, Prefab, the source affixes. JsonUtility can't round-trip those; you'd get hollow items on load. Determinism gives you a better deal: since the same seed always rolls the same item, you only need to store the seed and the names of the definitions involved.
Capture & restore
using ItemGeneratorSystem; using UnityEngine; // SAVE — six plain fields, safe for JsonUtility / Newtonsoft / PlayerPrefs ItemInstanceSnapshot snap = ItemInstancePersistence.Capture(item); string json = JsonUtility.ToJson(snap); // LOAD — resolve names through GeneratorSettings, re-roll from the seed var restored = ItemInstancePersistence.Restore( JsonUtility.FromJson<ItemInstanceSnapshot>(json), settings); if (restored == null) { /* a definition was renamed or missing — handle it */ }
| Snapshot field | Type | Description |
|---|---|---|
| Seed | int | The generation seed — the whole item in one number. |
| GeneratorVersion | int | Version stamp; Restore warns when it differs from the current generator. |
| TemplateName / ItemTypeName / SubTypeName / RarityName | string | Definition asset names, resolved through GeneratorSettings on restore. |
The contract you're signing
| Rule | Description |
|---|---|
| Registry must be populated | Restore resolves names via GeneratorSettings — run Auto-Discover Content after adding definitions, or restored items come back null. |
| Don’t rename shipped definitions | Resolution is by asset name. Renaming Rarity_Epic after players have saves breaks their snapshots. Add new assets instead of renaming old ones. |
| Version drift warns, not breaks | A snapshot from an older generator version restores best-effort with a console warning — the item may roll differently. Decide whether to migrate or accept. |
| Callbacks re-run on restore | OnPostGenerate fires during Restore too. Keep handlers deterministic or your restored items won’t match what was saved. |
customName/metadata, if you're running both.Design-time assets instead
The editor window's GENERATE button takes the other path: it writes ItemDefinition ScriptableObjects (default folder Assets/GeneratedItems/, name format {Name}_{Rarity}_{Seed}). Those are permanent project assets — hand-pick the great rolls, ship them as quest rewards, or feed them to the Grid Inventory System's import bridge. Their inspector renders the full item card plus a read-only metadata foldout (seed, version, timestamp, source template).