initialStateflag to differentiate between the first time a game object is serialized and when incremental updates can be sent. The first time a game object is sent to a client, it must include a full state snapshot, but subsequent updates can save on bandwidth by including only incremental changes.
OnSerializefunction should return true to indicate that an update should be sent. If it returns true, the dirty bits for that script are set to zero. If it returns false, the dirty bits are not changed. This allows multiple changes to a script to be accumulated over time and sent when the system is ready, instead of every frame.
OnSerializefunction is only called for
initialStateor when the
NetworkBehaviouris dirty. A
NetworkBehaviourwill only be dirty if a
SyncList) has changed since the last OnSerialize call. After data has been sent the
NetworkBehaviourwill not be dirty again until the next
syncInterval(set in the inspector). A
NetworkBehaviourcan also be marked as dirty by manually calling
SetDirtyBit(this does not bypass the syncInterval limit).
NetworkBehaviour. The flow for serializing these game objects is:
NetworkBehaviourhas a dirty mask. This mask is available inside
NetworkBehaviourscript is assigned a bit in the dirty mask.
SetDirtyBitwrites directly to the dirty mask
NetworkIdentityare dirty, then an
UpdateVarspacket is created for that game object
UpdateVarspacket is populated by calling
NetworkBehaviouron the game object
NetworkBehavioursthat are not dirty write a zero to the packet for their dirty bits
NetworkBehavioursthat are dirty write their dirty mask, then the values for the SyncVars that have changed
OnSerializereturns true for a
NetworkBehaviour, the dirty mask is reset for that
NetworkBehaviourso it does not send again until its value changes.
UpdateVarspacket is sent to ready clients that are observing the game object
UpdateVars packetis received for a game object
OnDeserializefunction is called for each
NetworkBehaviourscript on the game object
NetworkBehaviourscript on the game object reads a dirty mask.
NetworkBehaviouris zero, the
OnDeserializefunction returns without reading any more
OnDeserializefunction reads the values for the SyncVars that correspond to the dirty bits that are set
SerializeSyncVarsfunction which is called inside
DeserializeSyncVarsfunction which is called inside
NetworkBehaviourhas a base class that also has serialization functions, the base class functions should also be called.
UpdateVarpackets created for game object state updates may be aggregated in buffers before being sent to the client, so a single transport layer packet may contain updates for multiple game objects.