PrefabHandler
Internal subsystem that integrates Paragon's Scriptable Factory pooling with Unity Netcode's INetworkPrefabInstanceHandler. The prefab handler hierarchy intercepts Netcode's prefab instantiation and destruction callbacks, routing them through the factory pool system instead of using default Object.Instantiate / Object.Destroy. This enables object pooling, predicted spawning, and spawn message coordination for networked factory objects.
Architecture
Spawn Flow
Predicted Spawning
The handler supports predicted spawning — the owning client creates a local object immediately and reconciles it when the server confirms the spawn:
1
Spawn(ownerClientId, predicted: true)
—
2
factory.Spawn() creates local instance
—
3
NetworkObject.Spawn() sends to server
Server replicates to remotes
4
—
OnInstantiateNetworkObject() called
5
—
Checks predictedSpawnQueue
6
—
If predicted object exists: reuse it; otherwise: factory.Spawn() + queue for owner
Despawn Flow
When Netcode calls INetworkPrefabInstanceHandler.Destroy(), the handler uses a deferred despawn pattern:
Adds the object to
despawnQueueWaits 0.1 seconds (via
Yield.WaitForSeconds)If still in queue after delay, calls
factorable.Despawn()DespawnFactorable()can pre-empt by removing from the queue before the delay expires
This delay prevents race conditions where the object is despawned locally before the network destroy message is processed.
Classes
Abstract base — implements INetworkPrefabInstanceHandler, manages Netcode registration, and provides prefab instantiation with hash override
Generic handler with spawn queues, predicted spawning, deferred despawn, and spawn message coordination
Sealed handler adding variant-aware spawning via variantId in NetworkFactorySpawnMessage
See Also
Network Factory — parent subsystem overview
INetworkScriptableFactory — base network factory interface
INetworkScriptableFactory<TObject> — generic network factory interface
INetworkScriptableFactory<TObject, TData> — data-driven network factory interface
Scriptable Factory — base factory/pool system
Last updated