Hi @Extrys,
You can send meta data via the OnNetworkBehaviour.OnSynchronize method which is invoked on the server/authority side prior to serializing a NetworkObject.
The spawn process on the client side flows as such:
- CreateObjectMessage:
FastBufferReadercontains serialized information.- NetworkObject.AddScenObject:
SceneObjectdeserialized fromFastBufferReader.- NetworkSpawnManager.CreateLocalNetworkObject: Network prefab is instantiated =or= an instance is pulled from an object pool. No stream buffer is used during this phase.
- All NetworkBehaviour.OnNetworkPreSpawn methods are invoked.
- NetworkObject.SynchronizeNetworkBehaviours:
- NetworkVariables are deserialized and assigned their current value(s).
- NetworkBehaviour.Synchronize: If you have used
NetworkBehaviour.OnSynchronizethen this will be invoked on eachNetworkBehaviourcomponent where your instance specific meta data would reside (unique to eachNetworkBehaviourcomponent type on the network prefab instance).
- NetworkSpawnManager.SpawnNetworkObjectLocally: This is where OnNetworkSpawn and OnNetworkPostSpawn is invoked on each
NetworkBehaviourcomponent and where you could use the deserialized meta data you obtained during theOnSynchronizeinvocation(s) to uniquely setup your network prefab instance.
- NetworkObject.AddScenObject:
So the flow I think you are looking for is this:
Server Side:
- Network prefab is pulled from a pool and spawned locally on the server
- The
CreateObjectMessageis created and sent.- During the serialization of the network prefab instance, one or more
NetworkBehaviourcomponents will have theirOnSynchronizemethod invoked and will write serialized meta data specific to theNetworkBehaviourand/or the network prefab. This information is included in theCreateObjectMessage’s serialization data.
- During the serialization of the network prefab instance, one or more
- The
*Note: when you are spawning the object on the server side you are just directly pulling from the object pool (or directly creating an instance of the network prefab), the INetworkPrefabInstanceHandler implementation is not being invoked on the server side to obtain an instance for you. Including any form of meta data would require the INetworkPrefabInstanceHandler implementation to be invoked on the server side and would also require synchronizing the specific instance ids (i.e. NetworkObjectId and NetworkBehaviourId) in order to assure that specific meta data was associated with the spawned instance. However, since at this point the NetworkObject is not spawned there is no way to have that information.
Client Side:
CreateObjectMessageis received, deserialized, and then itsHandlemethod invoked.- The
SceneObjectstructure is used to create the network prefab instance.- If registered using a
INetworkPrefabInstanceHandlerimplementation, then the handler provides an instance from the pool.
- If registered using a
- Prior to spawning, each
NetworkBehaviourthat providedOnSynchronizemeta data will have theirOnSynchronizemethod invoked where you can deserialize the unique meta data for your network prefab instance. - The network prefab’s
NetworkObjectis spawned.- Each
NetworkBehaviour’sOnNetworkSpawnis invoked.- Any deserialized data obtained from
OnSynchronizecan be used to further define the unique traits of the instance.
- Any deserialized data obtained from
- Each
- The
This is the recommended way to handle adding custom serialized data to your network prefab instance when spawning it. It assures that when you spawn a new network prefab instance there is custom serialized data associated with the specific NetworkObject and will assure that custom information is always associated with that specific NetworkObject for late joining clients.
Let me know if this works for your project’s needs or what aspects of the above flow doesn’t work with your project’s needs and why adding the serialization data within one or more NetworkBehaviours doesn’t work and perhaps more of a description as to why you need to add all of your serialization data when getting a network prefab instance from a pool?