About NetworkUpdateLoop
Often there is a need to update netcode systems like RPC queue, transport IO, and others outside the standard MonoBehaviour
event cycle.
The Network Update Loop infrastructure utilizes Unity's low-level Player Loop API allowing for registering INetworkUpdateSystems
with NetworkUpdate()
methods to be executed at specific NetworkUpdateStages
which may be either before or after MonoBehaviour
-driven game logic execution.
Typically you will interact with NetworkUpdateLoop
for registration and INetworkUpdateSystem
for implementation. Systems such as network tick and future features (such as network variable snapshotting) will rely on this pipeline.
Registration
NetworkUpdateLoop
exposes four methods for registration:
Method | Registers |
---|---|
void RegisterNetworkUpdate(INetworkUpdateSystem updateSystem, NetworkUpdateStage updateStage) | Registers an INetworkUpdateSystem to be executed on the specified NetworkUpdateStage |
void RegisterAllNetworkUpdates(INetworkUpdateSystem updateSystem) | Registers an INetworkUpdateSystem to be executed on all NetworkUpdateStage s |
void UnregisterNetworkUpdate(INetworkUpdateSystem updateSystem, NetworkUpdateStage updateStage) | Unregisters an INetworkUpdateSystem from the specified NetworkUpdateStage |
void UnregisterAllNetworkUpdates(INetworkUpdateSystem updateSystem) | Unregisters an INetworkUpdateSystem from all NetworkUpdateStage s |
Update Stages
After injection, the player loops follows these stages. The player loop executes the Initialization
stage and that invokes NetworkUpdateLoop
's RunNetworkInitialization
method which iterates over registered INetworkUpdateSystems
in m_Initialization_Array
and calls INetworkUpdateSystem.NetworkUpdate(UpdateStage)
on them.
In all NetworkUpdateStages
, it iterates over an array and calls the NetworkUpdate
method over INetworkUpdateSystem
interface, and the pattern is repeated.
Stage | Method |
---|---|
Initialization | RunNetworkInitialization |
EarlyUpdate | RunNetworkEarlyUpdate ScriptRunDelayedStartupFrame Other systems |
FixedUpdate | RunNetworkFixedUpdate ScriptRunBehaviourFixedUpdate Other systems |
PreUpdate | RunNetworkPreUpdate PhysicsUpdate Physics2DUpdate Other systems |
Update | RunNetworkUpdate ScriptRunBehaviourUpdate Other systems |
PreLateUpdate | RunNetworkPreLateUpdate ScriptRunBehaviourLateUpdate |
PostLateUpdate | PlayerSendFrameComplete RunNetworkPostLateUpdate Other systems |
References
See Network Update Loop Reference for process flow diagrams and code.