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

spinner

Spawn Flow

spinner

Predicted Spawning

The handler supports predicted spawning — the owning client creates a local object immediately and reconciles it when the server confirms the spawn:

Step
Owner Client
Remote Client

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:

  1. Adds the object to despawnQueue

  2. Waits 0.1 seconds (via Yield.WaitForSeconds)

  3. If still in queue after delay, calls factorable.Despawn()

  4. 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

Class
Description

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

Last updated