Mirror Networking
Mirror.NetworkIdentity Class Reference

NetworkIdentity identifies objects across the network. More...

Inheritance diagram for Mirror.NetworkIdentity:

Public Member Functions

delegate void ClientAuthorityCallback (NetworkConnectionToClient conn, NetworkIdentity identity, bool authorityState)
 The delegate type for the clientAuthorityCallback.
 
bool AssignClientAuthority (NetworkConnectionToClient conn)
 Assign control of an object to a client via the client's NetworkConnection. More...
 
void RemoveClientAuthority ()
 Removes ownership for an object. More...
 

Static Public Member Functions

static NetworkIdentity GetSceneIdentity (ulong id)
 Gets the NetworkIdentity from the sceneIds dictionary with the corresponding id
 
static void ResetNextNetworkId ()
 Resets nextNetworkId = 1
 

Public Attributes

Dictionary< int, NetworkConnectionToClientobservers
 The set of network connections (players) that can see this object. More...
 
ulong sceneId
 Unique identifier for NetworkIdentity objects within a scene, used for spawning scene objects. More...
 
bool serverOnly
 Make this object only exist when the game is running as a server (or host). More...
 
Visibility visible = Visibility.Default
 

Properties

bool isClient [get, set]
 Returns true if running as a client and this object was spawned by a server. More...
 
bool isServer [get, set]
 Returns true if NetworkServer.active and server is not stopped. More...
 
bool isLocalPlayer [get, set]
 Return true if this object represents the player on the local machine. More...
 
bool isServerOnly [get]
 True if this object only exists on the server More...
 
bool isClientOnly [get]
 True if this object exists on a client that is not also acting as a server. More...
 
bool hasAuthority [get, set]
 True on client if that component has been assigned to the client. E.g. player, pets, henchmen. More...
 
uint netId [get, set]
 The unique network Id of this object (unique at runtime). More...
 
NetworkConnection connectionToServer [get, set]
 Client's network connection to the server. This is only valid for player objects on the client. More...
 
NetworkConnectionToClient connectionToClient [get, set]
 Server's network connection to the client. This is only valid for client-owned objects (including the Player object) on the server. More...
 
static Dictionary< uint, NetworkIdentityspawned [get]
 All spawned NetworkIdentities by netId. Available on server and client. More...
 
NetworkBehaviour[] NetworkBehaviours [get]
 
Guid assetId [get, set]
 Prefab GUID used to spawn prefabs across the network. More...
 
bool SpawnedFromInstantiate [get]
 

Events

static ClientAuthorityCallback clientAuthorityCallback
 A callback that can be populated to be notified when the client-authority state of objects changes. More...
 

Detailed Description

NetworkIdentity identifies objects across the network.

Definition at line 40 of file NetworkIdentity.cs.

Member Function Documentation

◆ AssignClientAuthority()

bool Mirror.NetworkIdentity.AssignClientAuthority ( NetworkConnectionToClient  conn)

Assign control of an object to a client via the client's NetworkConnection.

Definition at line 1172 of file NetworkIdentity.cs.

1173 {
1174 if (!isServer)
1175 {
1176 Debug.LogError("AssignClientAuthority can only be called on the server for spawned objects.");
1177 return false;
1178 }
1179
1180 if (conn == null)
1181 {
1182 Debug.LogError($"AssignClientAuthority for {gameObject} owner cannot be null. Use RemoveClientAuthority() instead.");
1183 return false;
1184 }
1185
1186 if (connectionToClient != null && conn != connectionToClient)
1187 {
1188 Debug.LogError($"AssignClientAuthority for {gameObject} already has an owner. Use RemoveClientAuthority() first.");
1189 return false;
1190 }
1191
1192 SetClientOwner(conn);
1193
1194 // The client will match to the existing object
1195 NetworkServer.SendChangeOwnerMessage(this, conn);
1196
1197 clientAuthorityCallback?.Invoke(conn, this, true);
1198
1199 return true;
1200 }
bool isServer
Returns true if NetworkServer.active and server is not stopped.
NetworkConnectionToClient connectionToClient
Server's network connection to the client. This is only valid for client-owned objects (including the...
static ClientAuthorityCallback clientAuthorityCallback
A callback that can be populated to be notified when the client-authority state of objects changes.

◆ RemoveClientAuthority()

void Mirror.NetworkIdentity.RemoveClientAuthority ( )

Removes ownership for an object.

Definition at line 1206 of file NetworkIdentity.cs.

1207 {
1208 if (!isServer)
1209 {
1210 Debug.LogError("RemoveClientAuthority can only be called on the server for spawned objects.");
1211 return;
1212 }
1213
1214 if (connectionToClient?.identity == this)
1215 {
1216 Debug.LogError("RemoveClientAuthority cannot remove authority for a player object");
1217 return;
1218 }
1219
1220 if (connectionToClient != null)
1221 {
1222 clientAuthorityCallback?.Invoke(connectionToClient, this, false);
1223 NetworkConnectionToClient previousOwner = connectionToClient;
1224 connectionToClient = null;
1225 NetworkServer.SendChangeOwnerMessage(this, previousOwner);
1226 }
1227 }

Member Data Documentation

◆ observers

Dictionary<int, NetworkConnectionToClient> Mirror.NetworkIdentity.observers

The set of network connections (players) that can see this object.

Definition at line 97 of file NetworkIdentity.cs.

◆ sceneId

ulong Mirror.NetworkIdentity.sceneId

Unique identifier for NetworkIdentity objects within a scene, used for spawning scene objects.

Definition at line 105 of file NetworkIdentity.cs.

◆ serverOnly

bool Mirror.NetworkIdentity.serverOnly

Make this object only exist when the game is running as a server (or host).

Definition at line 110 of file NetworkIdentity.cs.

◆ visible

Visibility Mirror.NetworkIdentity.visible = Visibility.Default

Definition at line 171 of file NetworkIdentity.cs.

Property Documentation

◆ assetId

Guid Mirror.NetworkIdentity.assetId
getset

Prefab GUID used to spawn prefabs across the network.

Definition at line 200 of file NetworkIdentity.cs.

201 {
202 get
203 {
204#if UNITY_EDITOR
205 // This is important because sometimes OnValidate does not run (like when adding view to prefab with no child links)
206 if (string.IsNullOrWhiteSpace(m_AssetId))
207 SetupIDs();
208#endif
209 // convert string to Guid and use .Empty to avoid exception if
210 // we would use 'new Guid("")'
211 return string.IsNullOrWhiteSpace(m_AssetId) ? Guid.Empty : new Guid(m_AssetId);
212 }
213 internal set
214 {
215 string newAssetIdString = value == Guid.Empty ? string.Empty : value.ToString("N");
216 string oldAssetIdString = m_AssetId;
217
218 // they are the same, do nothing
219 if (oldAssetIdString == newAssetIdString)
220 {
221 return;
222 }
223
224 // new is empty
225 if (string.IsNullOrWhiteSpace(newAssetIdString))
226 {
227 Debug.LogError($"Can not set AssetId to empty guid on NetworkIdentity '{name}', old assetId '{oldAssetIdString}'");
228 return;
229 }
230
231 // old not empty
232 if (!string.IsNullOrWhiteSpace(oldAssetIdString))
233 {
234 Debug.LogError($"Can not Set AssetId on NetworkIdentity '{name}' because it already had an assetId, current assetId '{oldAssetIdString}', attempted new assetId '{newAssetIdString}'");
235 return;
236 }
237
238 // old is empty
239 m_AssetId = newAssetIdString;
240 // Debug.Log($"Settings AssetId on NetworkIdentity '{name}', new assetId '{newAssetIdString}'");
241 }
242 }

◆ connectionToClient

NetworkConnectionToClient Mirror.NetworkIdentity.connectionToClient
getset

Server's network connection to the client. This is only valid for client-owned objects (including the Player object) on the server.

Definition at line 121 of file NetworkIdentity.cs.

122 {
123 get => _connectionToClient;
124 internal set
125 {
126 _connectionToClient?.RemoveOwnedObject(this);
127 _connectionToClient = value;
128 _connectionToClient?.AddOwnedObject(this);
129 }
130 }

◆ connectionToServer

NetworkConnection Mirror.NetworkIdentity.connectionToServer
getset

Client's network connection to the server. This is only valid for player objects on the client.

Definition at line 118 of file NetworkIdentity.cs.

118{ get; internal set; }

◆ hasAuthority

bool Mirror.NetworkIdentity.hasAuthority
getset

True on client if that component has been assigned to the client. E.g. player, pets, henchmen.

Definition at line 92 of file NetworkIdentity.cs.

92{ get; internal set; }

◆ isClient

bool Mirror.NetworkIdentity.isClient
getset

Returns true if running as a client and this object was spawned by a server.

Definition at line 54 of file NetworkIdentity.cs.

54{ get; internal set; }

◆ isClientOnly

bool Mirror.NetworkIdentity.isClientOnly
get

True if this object exists on a client that is not also acting as a server.

Definition at line 89 of file NetworkIdentity.cs.

◆ isLocalPlayer

bool Mirror.NetworkIdentity.isLocalPlayer
getset

Return true if this object represents the player on the local machine.

Definition at line 83 of file NetworkIdentity.cs.

83{ get; internal set; }

◆ isServer

bool Mirror.NetworkIdentity.isServer
getset

Returns true if NetworkServer.active and server is not stopped.

Definition at line 69 of file NetworkIdentity.cs.

69{ get; internal set; }

◆ isServerOnly

bool Mirror.NetworkIdentity.isServerOnly
get

True if this object only exists on the server

Definition at line 86 of file NetworkIdentity.cs.

◆ netId

uint Mirror.NetworkIdentity.netId
getset

The unique network Id of this object (unique at runtime).

Definition at line 100 of file NetworkIdentity.cs.

100{ get; internal set; }

◆ NetworkBehaviours

NetworkBehaviour [] Mirror.NetworkIdentity.NetworkBehaviours
get

Definition at line 160 of file NetworkIdentity.cs.

160{ get; private set; }

◆ spawned

Dictionary<uint, NetworkIdentity> Mirror.NetworkIdentity.spawned
staticget

All spawned NetworkIdentities by netId. Available on server and client.

Definition at line 140 of file NetworkIdentity.cs.

141 {
142 get
143 {
144 // server / host mode: use the one from server.
145 // host mode has access to all spawned.
146 if (NetworkServer.active) return NetworkServer.spawned;
147
148 // client
149 if (NetworkClient.active) return NetworkClient.spawned;
150
151 // neither: then we are testing.
152 // we could default to NetworkServer.spawned.
153 // but from the outside, that's not obvious.
154 // better to throw an exception to make it obvious.
155 throw new Exception("NetworkIdentity.spawned was accessed before NetworkServer/NetworkClient were active.");
156 }
157 }

◆ SpawnedFromInstantiate

bool Mirror.NetworkIdentity.SpawnedFromInstantiate
get

Definition at line 304 of file NetworkIdentity.cs.

304{ get; private set; }

Event Documentation

◆ clientAuthorityCallback

ClientAuthorityCallback Mirror.NetworkIdentity.clientAuthorityCallback
static

A callback that can be populated to be notified when the client-authority state of objects changes.

Definition at line 300 of file NetworkIdentity.cs.