Skip to main content

Network Identity

NetworkIdentity is a MonoBehaviour that lets the SDK runtime knows that it’s a networked GameObject. This component is used in many different systems in the Networking SDK so it’s vital to understand what it is and what it’s capable of doing. A GameObject that has this component will be replicated on all connected clients.

Data

NetworkIdentity contains 3 values:

Value

This is a UInt32 that will be unique for every NetworkIdentity instance. However, this value will be the same in the replicated instance on every connected clients. This is the most important data, as it basically acts as the identifier for a particular GameObject within a game session.

Prefab Id

This is a UInt16 value. When a GameObject is instantiated in the network from a prefab, this value will be set by the SDK. More details can be read on section 4.4. Instantiating a Network Prefab

Owner

Every connected client is represented by a unique UInt32 value. This value can be set if the owner is specified when instantiating a Network Object. Developers can use this field to check whether this NetworkIdentity is owned by a particular client.

Behavior

By adding a NetworkIdentity to a GameObject there are some features that developers get automatically:

  1. When a client is connected to a game server, all GameObject with a NetworkIdentity will be instantiated in that client with the latest available Position and Rotation value from the host.

  2. For a particular GameObject , all connected players within a game session shares the sameNetworkIdentity value

Network Objects & Network Prefab

There are two types of Network Objects. The only difference is in how the NetworkIdentity's value is initialized. After this first initialization, both will act exactly the same.

  1. Scene-based Network Objects: Network Objects that exists in Scene originally

  2. Instantiated Network Objects: Network Objects that are created from users in runtime

In order to make a Network Prefab, developers need to add a NetworkIdentity component to a prefab, and registers it to a RegisterNetworkPrefab component. RegisterNetworkPrefab is a MonoBehaviour that is provided by the SDK.

This is required to specify a unique prefab identifier for each prefabs.

Instantiating a Network Prefab

It is important to note that in order to properly instantiate a Network Prefab, we need to use the Instantiate function from the NetworkSpawner class. NetworkSpawner can be accessed from an instance of IGameSession.

These are the function signature for instantiating a Network Prefab. Instantiating a network prefab with Unity’s own Instantiate function will not have the replication behavior explained above.

public void Instantiate(NetworkIdentity prefab, Vector3 position, Quaternion rotation, Player owner);
public void Instantiate(NetworkIdentity prefab, Vector3 position, Quaternion rotation, NetworkIdentity parent, Player owner);
public Task<NetworkIdentity> InstantiateAsync(NetworkIdentity prefab, Vector3 position, Quaternion rotation, Player owner, CancellationToken cancellationToken = default);
public Task<NetworkIdentity> InstantiateAsync(NetworkIdentity prefab, Vector3 position, Quaternion rotation, NetworkIdentity parent, Player owner, CancellationToken cancellationToken = default)

IdentityManager

Often developers will want to send a reference of an object over the network so that the receiving end can do some actions on that object. For example when there is a bullet hit, we want to reduce the health of the hit character.

The common pattern here is to send a message containing UInt32 of the NetworkIdentity's Value field. This value can then be consumed on the receiving end by using the IdentityManager class. Note that this means any object that can or may be interacted by multiple clients will need to have a NetworkIdentity component attached to it.

IdenitityManager can be accessed from an instance of IGameSession. There are three important functions that developers need to know about this class.

// Get a NetworkIdentity instance of the specified networkId.
public NetworkIdentity Get(uint networkId);

// Get all NetworkIdentity.
public IEnumerable<NetworkIdentity> GetAll();

// Check whether there is a NetworkIdentity of type networkId.
public bool Contains(uint networkId);