Class NetworkIdentity
The NetworkIdentity identifies objects across the network, between server and clients. Its primary data is a NetworkInstanceId which is allocated by the server and then set on clients. This is used in network communications to be able to lookup game objects on different machines.
The NetworkIdentity is used to synchronize information in the object with the network. Only the server should create instances of objects which have NetworkIdentity as otherwise they will not be properly connected to the system.
For complex objects with a hierarchy of subcomponents, the NetworkIdentity must be on the root of the hierarchy. It is not supported to have multiple NetworkIdentity components on subcomponents of a hierarchy.
NetworkBehaviour scripts require a NetworkIdentity on the game object to be able to function.
The NetworkIdentity manages the dirty state of the NetworkBehaviours of the object. When it discovers that NetworkBehaviours are dirty, it causes an update packet to be created and sent to clients.
The flow for serialization updates managed by the NetworkIdentity is:
* Each NetworkBehaviour has a dirty mask. This mask is available inside OnSerialize as syncVarDirtyBits
* Each SyncVar in a NetworkBehaviour script is assigned a bit in the dirty mask.
* Changing the value of SyncVars causes the bit for that SyncVar to be set in the dirty mask
* Alternatively, calling SetDirtyBit() writes directly to the dirty mask
* NetworkIdentity objects are checked on the server as part of it's update loop
* If any NetworkBehaviours on a NetworkIdentity are dirty, then an UpdateVars packet is created for that object
* The UpdateVars packet is populated by calling OnSerialize on each NetworkBehaviour on the object
* NetworkBehaviours that are NOT dirty write a zero to the packet for their dirty bits
* NetworkBehaviours that are dirty write their dirty mask, then the values for the SyncVars that have changed
* If OnSerialize returns true for a NetworkBehaviour, the dirty mask is reset for that NetworkBehaviour, so it will not send again until its value changes.
* The UpdateVars packet is sent to ready clients that are observing the object
On the client:
* an UpdateVars packet is received for an object
* The OnDeserialize function is called for each NetworkBehaviour script on the object
* Each NetworkBehaviour script on the object reads a dirty mask.
* If the dirty mask for a NetworkBehaviour is zero, the OnDeserialize functions returns without reading any more
* If the dirty mask is non-zero value, then the OnDeserialize function reads the values for the SyncVars that correspond to the dirty bits that are set
* If there are SyncVar hook functions, those are invoked with the value read from the stream.
Inherited Members
Namespace: UnityEngine.Networking
Syntax
[ExecuteInEditMode]
[DisallowMultipleComponent]
[AddComponentMenu("Network/NetworkIdentity")]
[Obsolete("The high level API classes are deprecated and will be removed in the future.")]
public sealed class NetworkIdentity : MonoBehaviour
Fields
clientAuthorityCallback
A callback that can be populated to be notified when the client-authority state of objects changes.
Whenever an object is spawned using SpawnWithClientAuthority, or the client authority status of an object is changed with AssignClientAuthority or RemoveClientAuthority, then this callback will be invoked.
This callback is used by the NetworkMigrationManager to distribute client authority state to peers for host migration. If the NetworkMigrationManager is not being used, this callback does not need to be populated.
Declaration
public static NetworkIdentity.ClientAuthorityCallback clientAuthorityCallback
Field Value
Type | Description |
---|---|
NetworkIdentity.ClientAuthorityCallback |
Properties
assetId
Unique identifier used to find the source assets when server spawns the on clients.
Declaration
public NetworkHash128 assetId { get; }
Property Value
Type | Description |
---|---|
NetworkHash128 |
clientAuthorityOwner
The client that has authority for this object. This will be null if no client has authority.
This is set for player objects with localPlayerAuthority, and for objects set with AssignClientAuthority, and spawned with SpawnWithClientAuthority.
Declaration
public NetworkConnection clientAuthorityOwner { get; }
Property Value
Type | Description |
---|---|
NetworkConnection |
connectionToClient
The connection associated with this NetworkIdentity This is only valid for player objects on the server.
Use it to return details such as the connection's identity, IP address and ready status.
//For this example to work, attach a NetworkIdentity component to your GameObject.
//Make sure your Scene has a NetworkManager and NetworkManagerHUD
//Attach this script to the GameObject, and it outputs the connection of your GameObject to the console.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class NetworkIdentityNetID : MonoBehaviour
{
NetworkIdentity m_Identity;
//This is a TextMesh component that you attach to the child of the NetworkIdentity GameObject
void Start()
{
//Fetch the NetworkIdentity component of the GameObject
m_Identity = GetComponent<NetworkIdentity>();
//Output to the console the connection associated with this NetworkIdentity
Debug.Log("Connection : " + m_Identity.connectionToClient);
}
}
Declaration
public NetworkConnection connectionToClient { get; }
Property Value
Type | Description |
---|---|
NetworkConnection |
connectionToServer
The UConnection associated with this NetworkIdentity. This is only valid for player objects on a local client.
Declaration
public NetworkConnection connectionToServer { get; }
Property Value
Type | Description |
---|---|
NetworkConnection |
hasAuthority
This returns true if this object is the authoritative version of the object in the distributed network application.
This value is determined at runtime, as opposed to localPlayerAuthority which is set on the prefab. For most objects, authority is held by the server / host. For objects with localPlayerAuthority set, authority is held by the client of that player.
For objects that had their authority set by AssignClientAuthority on the server, this will be true on the client that owns the object. NOT on other clients.
Declaration
public bool hasAuthority { get; }
Property Value
Type | Description |
---|---|
Boolean |
isClient
Returns true if running as a client and this object was spawned by a server.
Declaration
public bool isClient { get; }
Property Value
Type | Description |
---|---|
Boolean |
isLocalPlayer
This returns true if this object is the one that represents the player on the local machine.
This is set when the server has spawned an object for this particular client.
Declaration
public bool isLocalPlayer { get; }
Property Value
Type | Description |
---|---|
Boolean |
isServer
Returns true if running as a server, which spawned the object.
Declaration
public bool isServer { get; }
Property Value
Type | Description |
---|---|
Boolean |
localPlayerAuthority
localPlayerAuthority means that the client of the "owning" player has authority over their own player object.
Authority for this object will be on the player's client. So hasAuthority will be true on that client - and false on the server and on other clients.
Declaration
public bool localPlayerAuthority { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
netId
Unique identifier for this particular object instance, used for tracking objects between networked clients and the server.
This is a unique identifier for this particular GameObject instance. Use it to track GameObjects between networked clients and the server.
//For this example to work, attach a NetworkIdentity component to your GameObject.
//Then, create a new empty GameObject and drag it under your NetworkIdentity GameObject in the Hierarchy. This makes it the child of the GameObject. //Next, attach a TextMesh component to the child GameObject. You can then place this TextMesh GameObject to be above your GameObject in the Scene.
//Attach this script to the parent GameObject, and it changes the text of the TextMesh to the identity of your GameObject.
using UnityEngine;
using UnityEngine.Networking;
public class NetworkIdentityNetID : MonoBehaviour
{
NetworkIdentity m_Identity;
//This is a TextMesh component that you attach to the child of the NetworkIdentity GameObject
TextMesh m_TextMesh;
void Start()
{
//Fetch the NetworkIdentity component of the GameObject
m_Identity = GetComponent<NetworkIdentity>();
//Enter the child of your GameObject (the GameObject with the TextMesh you attach)
//Fetch the TextMesh component of it
m_TextMesh = GetComponentInChildren(typeof(TextMesh)) as TextMesh;
//Change the Text of the TextMesh to show the netId
m_TextMesh.text = "ID : " + m_Identity.netId;
}
}
Declaration
public NetworkInstanceId netId { get; }
Property Value
Type | Description |
---|---|
NetworkInstanceId |
observers
The set of network connections (players) that can see this object.
Declaration
public ReadOnlyCollection<NetworkConnection> observers { get; }
Property Value
Type | Description |
---|---|
ReadOnlyCollection<NetworkConnection> |
playerControllerId
The id of the player associated with this GameObject.
This is only valid if this GameObject is for a local player.
The HLAPI treats players and clients as separate GameObjects. In most cases, there is a single player for each client, but in some situations (for example, when there are multiple controllers connected to a console system) there might be multiple player GameObjects for a single connection. When there are multiple players for a single connection, use the playerControllerId property to tell them apart. This is an identifier that is scoped to the connection, so that it maps to the id of the controller associated with the player on that client.
Declaration
public short playerControllerId { get; }
Property Value
Type | Description |
---|---|
Int16 |
sceneId
A unique identifier for NetworkIdentity objects within a scene.
This is used for spawning scene objects on clients.
Declaration
public NetworkSceneId sceneId { get; }
Property Value
Type | Description |
---|---|
NetworkSceneId |
serverOnly
Flag to make this object only exist when the game is running as a server (or host).
Declaration
public bool serverOnly { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
Methods
AssignClientAuthority(NetworkConnection)
This assigns control of an object to a client via the client's NetworkConnection
This causes hasAuthority to be set on the client that owns the object, and NetworkBehaviour.OnStartAuthority will be called on that client. This object then will be in the NetworkConnection.clientOwnedObjects list for the connection.
Authority can be removed with RemoveClientAuthority. Only one client can own an object at any time. Only NetworkIdentities with localPlayerAuthority set can have client authority assigned. This does not need to be called for player objects, as their authority is setup automatically.
Declaration
public bool AssignClientAuthority(NetworkConnection conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn | The connection of the client to assign authority to. |
Returns
Type | Description |
---|---|
Boolean | True if authority was assigned. |
ForceSceneId(Int32)
Force the scene ID to a specific value.
This can be used to fix an invalid scene ID. If you process all the NetworkIdentity components in a scene you can assign them new values starting from 1.
Declaration
public void ForceSceneId(int newSceneId)
Parameters
Type | Name | Description |
---|---|---|
Int32 | newSceneId | The new scene ID. |
RebuildObservers(Boolean)
This causes the set of players that can see this object to be rebuild. The OnRebuildObservers callback function will be invoked on each NetworkBehaviour.
Declaration
public void RebuildObservers(bool initialize)
Parameters
Type | Name | Description |
---|---|---|
Boolean | initialize | True if this is the first time. |
RemoveClientAuthority(NetworkConnection)
Removes ownership for an object for a client by its conneciton.
This applies to objects that had authority set by AssignClientAuthority, or NetworkServer.SpawnWithClientAuthority. Authority cannot be removed for player objects.
Declaration
public bool RemoveClientAuthority(NetworkConnection conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn | The connection of the client to remove authority for. |
Returns
Type | Description |
---|---|
Boolean | True if authority is removed. |
UNetUpdate()
Declaration
public void UNetUpdate()