Network Identity⚓️
The NetworkIdentity
class is a fundamental component of the Omni Networking framework for Unity, playing a role similar to that of NetworkIdentity
in frameworks like Mirror and Photon Unity Networking (PUN).
It uniquely identifies a GameObject across the network and links it to the networking system, enabling synchronization between clients and servers. Every networked GameObject must have a NetworkIdentity
to participate in the networking system.
Key responsibilities of the NetworkIdentity
include:
- Assigning and maintaining a unique network ID for each object.
- Managing network authority (server-authoritative or client-authoritative).
- Enabling the replication of state and data between clients and the server.
- Supporting remote procedure calls (RPCs) and network variables.
- Handling spawning, destruction, and visibility of objects across the network.
This class acts as the foundation for any GameObject that needs to be synchronized or controlled in a multiplayer environment. Without it, objects cannot be managed by the networking system.
⚠️ Note: Only one
NetworkIdentity
should exist per networked GameObject.
Methods⚓️
Get⚓️
Retrieves a service instance by its name from the service locator.
- Signature:
public T Get<T>(string serviceName) where T : class
public T Get<T>() where T : class
Description
The Get
method retrieves a service instance from the service locator. It provides two overloads: one that accepts a service name and another that uses the type name as the service name. If the service is not found or cannot be cast to the specified type, an exception is thrown.
Parameters
Parameter | Type | Description |
---|---|---|
serviceName |
string |
The name of the service to retrieve (optional). |
Returns
T
: The service instance cast to the specified type.
Exceptions
- Throws an
Exception
if the service is not found. - Throws an
Exception
if the service cannot be cast to the specified type.
Example
TryGet⚓️
Attempts to retrieve a service instance from the service locator.
- Signature:
public bool TryGet<T>(string serviceName, out T service) where T : class
public bool TryGet<T>(out T service) where T : class
Description
The TryGet
method attempts to retrieve a service instance from the service locator. It provides a safe way to access services without throwing exceptions if the service is not found or cannot be cast to the specified type.
Parameters
Parameter | Type | Description |
---|---|---|
serviceName |
string |
The name of the service to retrieve (optional). |
service |
T |
The retrieved service instance (output parameter). |
Returns
bool
:true
if the service is found and successfully cast to the specified type; otherwise,false
.
Example
C# | |
---|---|
Register⚓️
Registers a new service instance in the service locator.
- Signature:
public void Register<T>(T service) where T : class
public void Register<T>(T service, string serviceName) where T : class
Description
The Register
method adds a new service instance to the service locator. It provides two overloads: one that uses the type name as the service name and another that accepts a custom service name.
Parameters
Parameter | Type | Description |
---|---|---|
service |
T |
The service instance to register. |
serviceName |
string |
The name to associate with the service (optional). |
Exceptions
- Throws an
Exception
if a service with the same name already exists.
Example
TryRegister⚓️
Attempts to register a new service instance in the service locator.
- Signature:
public bool TryRegister<T>(T service) where T : class
public bool TryRegister<T>(T service, string serviceName) where T : class
Description
The TryRegister
method attempts to add a new service instance to the service locator. Unlike Register
, this method returns a boolean indicating success or failure instead of throwing an exception.
Parameters
Parameter | Type | Description |
---|---|---|
service |
T |
The service instance to register. |
serviceName |
string |
The name to associate with the service (optional). |
Returns
bool
:true
if the service was successfully registered; otherwise,false
.
Example
UpdateService⚓️
Updates an existing service instance in the service locator.
- Signature:
public void UpdateService<T>(T service) where T : class
public void UpdateService<T>(T service, string serviceName) where T : class
Description
The UpdateService
method updates an existing service instance in the service locator. If the service doesn't exist, an exception is thrown.
Parameters
Parameter | Type | Description |
---|---|---|
service |
T |
The new service instance. |
serviceName |
string |
The name of the service to update (optional). |
Exceptions
- Throws an
Exception
if the service does not exist.
Example
TryUpdateService⚓️
Attempts to update an existing service instance in the service locator.
- Signature:
public bool TryUpdateService<T>(T service) where T : class
public bool TryUpdateService<T>(T service, string serviceName) where T : class
Description
The TryUpdateService
method attempts to update an existing service instance in the service locator. Returns a boolean indicating success or failure instead of throwing an exception.
Parameters
Parameter | Type | Description |
---|---|---|
service |
T |
The new service instance. |
serviceName |
string |
The name of the service to update (optional). |
Returns
bool
:true
if the service was successfully updated; otherwise,false
.
Example
Unregister⚓️
Removes a service from the service locator.
- Signature:
public bool Unregister<T>() where T : class
public bool Unregister(string serviceName)
Description
The Unregister
method removes a service from the service locator. It can be called using either the type name or a custom service name.
Parameters
Parameter | Type | Description |
---|---|---|
serviceName |
string |
The name of the service to remove (optional). |
Returns
bool
:true
if the service was successfully removed; otherwise,false
.
Example
Exists⚓️
Checks if a service exists in the service locator.
- Signature:
public bool Exists<T>() where T : class
public bool Exists(string serviceName)
Description
The Exists
method checks whether a service exists in the service locator. It can be called using either the type name or a custom service name.
Parameters
Parameter | Type | Description |
---|---|---|
serviceName |
string |
The name of the service to check (optional). |
Returns
bool
:true
if the service exists; otherwise,false
.
Example
GetAsComponent⚓️
Retrieves a Unity MonoBehaviour component that was assigned to a NetworkComponentService in the inspector.
- Signature:
public void GetAsComponent<T>(out T component) where T : class
public void GetAsComponent<T>(string componentName, out T component) where T : class
public T GetAsComponent<T>() where T : class
public T GetAsComponent<T>(string componentName) where T : class
Description
The GetAsComponent
method retrieves a Unity MonoBehaviour component that was previously assigned to a NetworkComponentService in the inspector. This allows for network-aware access to Unity components like Rigidbody, Transform, or custom MonoBehaviours.
Parameters
Parameter | Type | Description |
---|---|---|
componentName |
string |
The name of the NetworkComponentService (optional, defaults to T name). |
component |
T |
The retrieved Unity component (output parameter). |
Returns
T
: The Unity component instance (for non-void overloads).
Example
TryGetAsComponent⚓️
Attempts to retrieve a Unity MonoBehaviour component that was assigned to a NetworkComponentService in the inspector.
- Signature:
public bool TryGetAsComponent<T>(out T component) where T : class
public bool TryGetAsComponent<T>(string componentName, out T component) where T : class
Description
The TryGetAsComponent
method attempts to retrieve a Unity MonoBehaviour component that was previously assigned to a NetworkComponentService in the inspector. It provides a safe way to access components without throwing exceptions if the component is not found or not properly assigned.
Parameters
Parameter | Type | Description |
---|---|---|
componentName |
string |
The name of the NetworkComponentService (optional, defaults to T name). |
component |
T |
The retrieved Unity component (output parameter). |
Returns
bool
:true
if the component was successfully retrieved; otherwise,false
.
Example
GetAsGameObject⚓️
Retrieves the GameObject that contains a component assigned to a NetworkComponentService.
- Signature:
public GameObject GetAsGameObject<T>()
public GameObject GetAsGameObject(string gameObjectName)
Description
The GetAsGameObject
method retrieves the GameObject that contains a component that was previously assigned to a NetworkComponentService in the inspector. This is useful when you need to access the GameObject that holds a specific networked component.
Parameters
Parameter | Type | Description |
---|---|---|
gameObjectName |
string |
The name of the NetworkComponentService (optional, defaults to T name). |
Returns
GameObject
: The GameObject containing the specified component.
Example
C# | |
---|---|
TryGetAsGameObject⚓️
Attempts to retrieve the GameObject that contains a component assigned to a NetworkComponentService.
- Signature:
public bool TryGetAsGameObject<T>(out GameObject gameObject)
public bool TryGetAsGameObject(string gameObjectName, out GameObject gameObject)
Description
The TryGetAsGameObject
method attempts to retrieve the GameObject that contains a component that was previously assigned to a NetworkComponentService in the inspector. It provides a safe way to access GameObjects without throwing exceptions if the component or GameObject is not found.
Parameters
Parameter | Type | Description |
---|---|---|
gameObjectName |
string |
The name of the NetworkComponentService (optional, defaults to T name). |
gameObject |
GameObject |
The retrieved GameObject (output parameter). |
Returns
bool
:true
if the GameObject was successfully retrieved; otherwise,false
.
Example
SpawnOnClient⚓️
Spawns the network identity on the client.
- Signature:
public void SpawnOnClient(ServerOptions options)
public void SpawnOnClient(Target target = Target.Auto, DeliveryMode deliveryMode = DeliveryMode.ReliableOrdered, int groupId = 0, DataCache dataCache = default, byte sequenceChannel = 0)
Description
The SpawnOnClient
method spawns the network identity on connected clients. It can be called with either ServerOptions
or individual parameters for customization.
Parameters
Parameter | Type | Description |
---|---|---|
options |
ServerOptions |
Options for spawning the network identity. |
target |
Target |
The target clients for spawning (default is Auto). |
deliveryMode |
DeliveryMode |
The delivery mode for the spawn message. |
groupId |
int |
The group ID for the spawn message. |
dataCache |
DataCache |
The data cache options for the spawn message. |
sequenceChannel |
byte |
The sequence channel for message ordering. |
Example
C# | |
---|---|
Despawn⚓️
Despawns the network identity from clients.
- Signature:
public void Despawn(ServerOptions options)
public void Despawn(Target target = Target.Auto, DeliveryMode deliveryMode = DeliveryMode.ReliableOrdered, int groupId = 0, DataCache dataCache = default, byte sequenceChannel = 0)
Description
The Despawn
method removes the network identity from connected clients. It can be called with either ServerOptions
or individual parameters for customization.
Parameters
Parameter | Type | Description |
---|---|---|
options |
ServerOptions |
Options for despawning the network identity. |
target |
Target |
The target clients for despawning (default is Auto). |
deliveryMode |
DeliveryMode |
The delivery mode for the despawn message. |
groupId |
int |
The group ID for the despawn message. |
dataCache |
DataCache |
The data cache options for the despawn message. |
sequenceChannel |
byte |
The sequence channel for message ordering. |
Example
C# | |
---|---|
DespawnToPeer⚓️
Despawns the network identity for a specific peer.
- Signature:
public void DespawnToPeer(NetworkPeer peer, ServerOptions options)
public void DespawnToPeer(NetworkPeer peer, DeliveryMode deliveryMode = DeliveryMode.ReliableOrdered, DataCache dataCache = default, byte sequenceChannel = 0)
Description
The DespawnToPeer
method removes the network identity for a specific peer. It can be called with either ServerOptions
or individual parameters for customization.
Parameters
Parameter | Type | Description |
---|---|---|
peer |
NetworkPeer |
The peer to despawn the network identity for. |
options |
ServerOptions |
Options for despawning the network identity. |
deliveryMode |
DeliveryMode |
The delivery mode for the despawn message. |
dataCache |
DataCache |
The data cache options for the despawn message. |
sequenceChannel |
byte |
The sequence channel for message ordering. |
Example
C# | |
---|---|
DestroyOnClient⚓️
Destroys the network identity on the client only.
- Signature:
public void DestroyOnClient()
Description
The DestroyOnClient
method destroys the network identity locally on the client side only. This action does not affect other clients or the server.
Example
DestroyOnServer⚓️
Destroys the network identity on the server only.
- Signature:
public void DestroyOnServer()
Description
The DestroyOnServer
method destroys the network identity locally on the server side only. This action does not affect clients.
Example
Destroy⚓️
Destroys the network identity based on the current context (client or server).
- Signature:
public void Destroy()
Description
The Destroy
method destroys the network identity either on the client or server, depending on where it is called. It automatically determines the appropriate destroy method to call based on the current context.
AddNetworkComponent⚓️
Adds a network component to the network identity.
- Signature:
public T AddNetworkComponent<T>(string serviceName = null) where T : NetworkBehaviour
Description
The AddNetworkComponent
method adds a new network component to the network identity and initializes it with the necessary network settings.
Parameters
Parameter | Type | Description |
---|---|---|
serviceName |
string |
Optional name for the service (defaults to type name). |
Returns
T
: The newly added network component.
Example
SetOwner⚓️
Sets the owner of the network identity.
- Signature:
public void SetOwner(NetworkPeer peer, Target target = Target.Auto, DeliveryMode deliveryMode = DeliveryMode.ReliableOrdered, int groupId = 0, DataCache dataCache = default, byte sequenceChannel = 0)
Description
The SetOwner
method changes the ownership of the network identity to the specified peer.
Parameters
Parameter | Type | Description |
---|---|---|
peer |
NetworkPeer |
The new owner of the network identity. |
target |
Target |
The target for the ownership change message. |
deliveryMode |
DeliveryMode |
The delivery mode for the ownership change message. |
groupId |
int |
The group ID for the ownership change message. |
dataCache |
DataCache |
The data cache options for the ownership change message. |
sequenceChannel |
byte |
The sequence channel for message ordering. |
Example
RequestAction⚓️
Requests an action to be performed on the server-side entity.
- Signature:
public void RequestAction(DataBuffer data = default, DeliveryMode deliveryMode = DeliveryMode.ReliableOrdered, byte sequenceChannel = 0)
Description
The RequestAction
method sends a request from the client to perform an action on the server-side entity.
Parameters
Parameter | Type | Description |
---|---|---|
data |
DataBuffer |
The data buffer containing action parameters. |
deliveryMode |
DeliveryMode |
The delivery mode for the action request. |
sequenceChannel |
byte |
The sequence channel for message ordering. |
Example
Properties⚓️
Id⚓️
Gets or sets the unique identifier for this network identity.
- Signature:
public int Id { get; internal set; }
Description
The Id
property represents a unique identifier assigned to each NetworkIdentity
instance in the network. This ID is used to track and reference specific network objects across the server and clients.
Returns
int
: The unique identifier of this network identity.
Example
LocalPlayer⚓️
Gets or sets the local player instance. This property is only set on the client side.
- Signature:
public static NetworkIdentity LocalPlayer { get; internal set; }
Description
The LocalPlayer
property provides access to the NetworkIdentity
instance representing the local player in the game. This static property is only set on the client side and represents the player's own character or entity.
Returns
NetworkIdentity
: The network identity instance representing the local player.
Example
Owner⚓️
Gets or sets the owner of this object, available on both server and client side.
- Signature:
public NetworkPeer Owner { get; internal set; }
Description
The Owner
property represents the NetworkPeer
that owns this network identity. On the client side, only a few properties of the owner are accessible, such as SharedData
. This property is crucial for determining authority and permissions over the network object.
Returns
NetworkPeer
: The peer that owns this network identity.
Example
IsServer⚓️
Gets or sets whether this object is obtained from the server side or checked on the client side.
- Signature:
public bool IsServer { get; internal set; }
Description
The IsServer
property indicates whether this network identity is operating on the server side. When true
, the object has server authority and can perform server-specific operations. When false
, the object is operating on the client side.
Returns
bool
:true
if the object is on the server side; otherwise,false
.
Example
IsClient⚓️
Gets a value indicating whether this instance is on the client side.
- Signature:
public bool IsClient => !IsServer
Description
The IsClient
property indicates whether this network identity is operating on the client side. This is the inverse of IsServer
and is used to determine client-side behavior and permissions.
Returns
bool
:true
if this instance is on the client side; otherwise,false
.
Example
IsLocalPlayer⚓️
Gets or sets whether this object is owned by the local player.
- Signature:
public bool IsLocalPlayer { get; internal set; }
Description
The IsLocalPlayer
property indicates whether this network identity represents the local player's object. This is useful for determining if the object should respond to local input or have special rendering/behavior for the local player.
Returns
bool
:true
if this object is owned by the local player; otherwise,false
.
Example
IsRegistered⚓️
Gets whether this NetworkIdentity is registered in the network system.
- Signature:
public bool IsRegistered => Owner != null && Id != 0
Description
The IsRegistered
property indicates whether this network identity has been properly registered in the network system. An object is considered registered when it has both a valid owner and a non-zero ID assigned to it.
Returns
bool
:true
if the object is registered (has an owner and valid ID); otherwise,false
.
Example
IsServerOwner⚓️
Gets or sets whether this identity is owned by the server.
- Signature:
public bool IsServerOwner { get; internal set; }
Description
The IsServerOwner
property indicates whether this network identity is owned by the server itself rather than a client. This is useful for objects that should be controlled exclusively by the server.
Returns
bool
:true
if this identity is owned by the server; otherwise,false
.