Prefabs

Reconstructing an object that no longer exists — only through your factory, only with a recorded key, with the identity adopted from the save.

Unity NexusForge Persistence 0.2.0 Updated

Restoring never instantiates anything by itself#

Four conditions must all hold, and each one is something you opted into:

  1. Capture recorded a prefabKey — only if a component on the object implements IPersistencePrefabSource.
  2. The missing-object policy is ReconstructThroughFactory. The default, Report, does not build anything.
  3. The game installed an IPersistencePrefabFactory via NexusForgePersistence.PrefabFactory.
  4. The factory — and nothing else in the package — calls Instantiate.
csharp
public sealed class MyPrefabFactory : IPersistencePrefabFactory
{
    public bool TryCreate(string prefabKey, out GameObject instance)
    {
class="tok-com">        // Your catalogue, your pooling, your addressables. Return false when the key is unknown,
class="tok-com">        // and the restore will report exactly that instead of leaving a half-built object behind.
        return m_Catalogue.TryCreate(prefabKey, out instance);
    }
}

After a successful reconstruction the instance adopts the identity from the save, so the next capture still matches the same object rather than creating a second identity for the same thing.

When it does not happen, you are told which part is missing#

The report distinguishes the three failures, because they have three different fixes:

MissingWhat it meansFix
No keyCapture never recorded oneImplement IPersistencePrefabSource on a component of the object
No factoryThe game installed noneSet NexusForgePersistence.PrefabFactory during initialisation
Unknown keyThe factory refused itAdd the key to your catalogue, or keep Report for that case

MissingPrefabPolicy chooses whether an unsatisfiable key is a report, a warning or a failure.

Next#

Something wrong on this page? Every page here describes behaviour that is checked in the repository. If a page and the package disagree, the package wins.

Report a documentation problem · Frequently asked questions · Troubleshooting