Spawning
Introduction
NetworkObjects cannot be instantiated with the regular GameObject.Instantiate functions instead they have to be spawned.
To spawn a NetworkObject at runtime use the Runner.Spawn() method. Scene objects do not need to be spawned they are automatically spawned when the scene is loaded.
The NetworkObject is used by Fusion to assign a network-wide unique identifier so all clients can agree on which instance is which and correctly synchronize each networked object's state. The Runner.Spawn() method tells Fusion when and how to add a new object instance to the collective network state.
IMPORTANT!
Do NOT use Unity's built in GameObject.Instantiate() method for networked object , this will just create a local game object which is completely detached from the Fusion simulation loop with a broken network state.
Runner.Spawn
The Runner.Spawn() method on the Fusion NetworkRunner instance mimics the GameObject.Instantiate() method in Unity. The parameters that can be provided to the method are:
- a prefab of type
NetworkObject. - a position
- a rotation
- a
PlayerRefto identify the client with input authority over the object. (Not relevant for Shared Mode) - a delegate of type
NetworkRunner.OnBeforeSpawnedto run before replicating the object on the other instances
Only the prefab is a mandatory parameter, all others are optional.
Note: if you pass a position and/or rotation to the Spawn method it will only affect the object's transform on the peer that spawns it. To see this position and rotation reflected on other peers you can attach a NetworkTransform, any other component which inherits from NetworkTRSP or a fully custom component which synchronizes the transform data.
C#
var obj = Runner.Spawn(prefab, Vector3.zero, Quaternion.identity, Runner.LocalPlayer, MyOnBeforeSpawnDelegate);
Any client can spawn a NetworkObject. Spawning a NetworkObject automatically assigns StateAuthority over it to the client.
Spawned
ISpawned.Spawned() is called immediately after the NetworkRunner attaches a NetworkObject (either as a result of a NetworkRunner.Spawn() call, or loading a Scene which contained a NetworkObject). The Spawned() callback can be considered Fusion's alternative to Awake(), and is called AFTER the object has been attached and is valid.
NetworkBehaviour implements the ISpawned interface with an empty virtual Spawned() method. To implement Spawned() for a custom NetworkBehaviour, simply override Spawned().
C#
public class Example : NetworkBehaviour
{
public override void Spawned()
{
// Use this instead of Start / Awake for NetworkObjects
}
}
Any Networked Property values changes made on the State Authority in Spawned() will be the initial values for all spawned instances of that NetworkObject on all other peers. HOWEVER, remote peers may not always spawn objects on the same tick as the State Authority (due to Area of Interest, Interest Management, late joining, and Prioritization). When a NetworkObject spawns remotely on a later tick than the State Authority, it will spawn with the values that the State Authority had for that later tick.
- For spawns on a peer with State Authority,
Spawned()is called immediately afterRunner.Spawn(). - For non-State Authority peers, The
NetworkObjectis spawned when theNetworkRunnerreceives a network state for anNetworkObjectthat does not exist locally.
Despawn
To remove a network object, the peer with State Authority over the object may call Runner.Despawn().
Despawned
Similar to how Runner.Spawn() triggers the Spawned() method call for classes implementing ISpawned such as NetworkBehaviour, Despawn() will result in the Despawned() method to be called for classes implementing IDespawned.
C#
public class Example : NetworkBehaviour
{
public override void Despawned()
{
// Use this instead of Destroy for NetworkObjects
}
}
Back to top