Network Manager⚓️
The NetworkManager class is the core component of the Omni Networking library for Unity. It provides a framework for managing network operations, including client-server communication, synchronization, and data transmission.
The NetworkManager class is responsible for handling the initialization, configuration, and management of the network environment. It acts as a central hub for network-related operations, such as connecting to a server, handling client-server communication, and managing network events.
Methods⚓️
ClearCaches⚓️
Clears all global caches, removing all entries from both append and overwrite cache collections.
- Signature:
public static void ClearCaches()
Description
The ClearCaches method removes all entries in the global append and overwrite cache collections. This method is useful for freeing up resources by clearing all cached data across the network, ensuring a clean state.
Example
DestroyAllCaches⚓️
Destroys all caches associated with the specified NetworkPeer, removing both global and overwrite caches marked for auto-destruction.
- Signature:
public static void DestroyAllCaches(NetworkPeer peer)
Description
The DestroyAllCaches method removes all cache entries associated with the given NetworkPeer that are marked for auto-destruction (AutoDestroyCache). This includes caches from both global append and global overwrite collections. If any cache entry fails to be removed, an error is logged.
Parameters
| Parameter | Type | Description |
|---|---|---|
peer |
NetworkPeer |
The network peer whose associated caches are to be destroyed. |
Example
DeleteCache⚓️
Deletes a cache entry based on the provided DataCache and optional groupId, or a DataCache associated with a specific NetworkPeer.
- Signature:
public static void DeleteCache(DataCache dataCache, int groupId = 0)public static void DeleteCache(DataCache dataCache, NetworkPeer peer, int groupId = 0)
Description
The DeleteCache methods remove cache entries based on the provided DataCache details. Depending on the cache's Mode, this can involve removing global, group-specific, or peer-specific caches, with additional conditions to check if the DataCache.Id and DataCache.Mode are set correctly. These methods ensure the removal of specified caches, either by targeting a particular group (groupId) or by associating it with a specific peer (NetworkPeer).
Parameters
| Parameter | Type | Description |
|---|---|---|
dataCache |
DataCache |
The data cache to delete, which includes mode and ID for the targeted cache entry. |
groupId |
int |
The ID of the group to which the cache belongs (optional, default is 0). |
peer |
NetworkPeer |
The peer associated with the cache, for peer-specific cache deletion. |
Overloads
Deletes a cache entry in a specific group or globally based on the provided DataCache and groupId.
| Parameter | Type | Description |
|---|---|---|
dataCache |
DataCache |
The data cache to delete. |
groupId |
int |
The ID of the group to which the cache belongs. |
Deletes a cache entry for a specific peer or group based on the provided DataCache, NetworkPeer, and optional groupId.
| Parameter | Type | Description |
|---|---|---|
dataCache |
DataCache |
The data cache to delete. |
peer |
NetworkPeer |
The peer associated with the cache. |
groupId |
int |
The ID of the group to which the cache belongs. |
Invoke(Server) by Instance⚓️
Invokes a Remote Procedure Call (RPC) on clients, targeting a specific network identity and script instance by their IDs, with customizable options for target, delivery mode, grouping, and sequencing.
- Signature:
public static void Invoke(byte msgId, NetworkPeer peer, int identityId, byte instanceId, SyncOptions options)public static void Invoke(byte msgId, NetworkPeer peer, int identityId, byte instanceId, DataBuffer buffer = null, Target target = Target.Auto, DeliveryMode deliveryMode = DeliveryMode.ReliableOrdered, int groupId = 0, DataCache dataCache = default, byte sequenceChannel = 0)
Description
The Invoke method allows the server to execute a specific RPC on clients by targeting a NetworkIdentity and a specific script instance associated with that identity. The identityId identifies the object, while instanceId specifies the exact script instance on that object that should receive the RPC. This function provides options for message delivery such as buffer, target recipients, delivery reliability, grouping, and sequencing.
Parameters
| Parameter | Type | Description |
|---|---|---|
msgId |
byte |
The ID of the RPC message to invoke. |
peer |
NetworkPeer |
The peer for whom the RPC is invoked. |
identityId |
int |
The unique ID of the NetworkIdentity that will receive the RPC. |
instanceId |
byte |
The ID of the script instance on the NetworkIdentity that will receive the RPC. |
options |
SyncOptions |
A set of options for synchronization (used in the first overload). |
buffer |
DataBuffer |
Optional data buffer to send. |
target |
Target |
Specifies the target for the RPC, such as all clients or a specific peer (default is All). |
deliveryMode |
DeliveryMode |
Defines the delivery mode, such as ReliableOrdered (default), Unreliable, etc. |
groupId |
int |
ID for grouping related messages together (default is 0). |
dataCache |
DataCache |
Cache option for the data being sent (default is DataCache.None). |
sequenceChannel |
byte |
Channel for message sequencing to manage order consistency across channels (default is 0). |
Overloads
Invokes an RPC using SyncOptions to specify buffer, target, delivery mode, group ID, data cache, and sequence channel, targeting both a NetworkIdentity and script instance.
| Parameter | Type | Description |
|---|---|---|
msgId |
byte |
The ID of the RPC message to invoke. |
peer |
NetworkPeer |
The peer for whom the RPC is invoked. |
identityId |
int |
The ID of the NetworkIdentity to target. |
instanceId |
byte |
The ID of the script instance to target. |
options |
SyncOptions |
Configuration options for synchronization. |
Example
Invokes an RPC with detailed parameters for buffer, target, delivery mode, grouping, and sequencing, targeting both a NetworkIdentity and script instance.
| Parameter | Type | Description |
|---|---|---|
msgId |
byte |
The ID of the RPC message to invoke. |
peer |
NetworkPeer |
The peer for whom the RPC is invoked. |
identityId |
int |
The ID of the NetworkIdentity to target. |
instanceId |
byte |
The ID of the script instance to target. |
buffer |
DataBuffer |
Optional data buffer containing the message content. |
target |
Target |
The target for the RPC, such as all clients or a specific peer. |
deliveryMode |
DeliveryMode |
Defines the delivery mode, such as ReliableOrdered. |
groupId |
int |
ID for grouping related messages (default is 0). |
dataCache |
DataCache |
Specifies how the message data is cached (default is None). |
sequenceChannel |
byte |
Channel for message sequencing (default is 0). |
Example
Remarks
- Identity and Instance-Specific Invocation: Targets a specific
NetworkIdentityusingidentityIdand a specific script instance usinginstanceId, ensuring precise correspondence with the intended script or object. - Flexible Targeting: Supports targeting all clients, server-only, or specific peers, providing control over who receives the RPC.
- Reliability Options: Provides reliable, ordered delivery or lightweight, unordered options depending on the use case.
- Sequencing and Grouping: Allows message organization with group IDs and sequence channels, ensuring consistency across channels.
- Usage: Ideal for invoking RPCs on specific entities and script instances, particularly for commands or updates that are relevant to targeted objects or specific components.
Note
This function is also available on the client side, but does not load the peer parameter.
Invoke(Server)⚓️
Invokes a Remote Procedure Call (RPC) on clients, targeting a specific network identity by its unique ID, with options for target, delivery mode, grouping, and sequencing.
- Signature:
public static void Invoke(byte msgId, NetworkPeer peer, int identityId, SyncOptions options)public static void Invoke(byte msgId, NetworkPeer peer, int identityId, DataBuffer buffer = null, Target target = Target.Auto, DeliveryMode deliveryMode = DeliveryMode.ReliableOrdered, int groupId = 0, DataCache dataCache = default, byte sequenceChannel = 0)
Description
The Invoke method allows the server to execute a specific RPC on clients by using the unique identityId of a network identity. Unlike global RPCs, this method requires the identity ID to establish correspondence, ensuring that only scripts or objects tied to that identity will respond. This method offers flexibility for customizing message delivery with options such as buffer, target recipients, delivery reliability, grouping, and message sequencing.
Parameters
| Parameter | Type | Description |
|---|---|---|
msgId |
byte |
The ID of the RPC message to invoke. |
peer |
NetworkPeer |
The peer for whom the RPC is invoked. |
identityId |
int |
The unique ID of the NetworkIdentity that will receive the RPC. |
options |
SyncOptions |
A set of options for synchronization (used in the first overload). |
buffer |
DataBuffer |
Optional data buffer to send. |
target |
Target |
Specifies the target for the RPC, such as all clients or a specific peer (default is All). |
deliveryMode |
DeliveryMode |
Defines the delivery mode, such as ReliableOrdered (default), Unreliable, etc. |
groupId |
int |
ID for grouping related messages together (default is 0). |
dataCache |
DataCache |
Cache option for the data being sent (default is DataCache.None). |
sequenceChannel |
byte |
Channel for message sequencing to manage order consistency across channels (default is 0). |
Overloads
Invokes an RPC using a SyncOptions instance to specify buffer, target, delivery mode, group ID, data cache, and sequence channel.
| Parameter | Type | Description |
|---|---|---|
msgId |
byte |
The ID of the RPC message to invoke. |
peer |
NetworkPeer |
The peer for whom the RPC is invoked. |
identityId |
int |
The ID of the NetworkIdentity to target. |
options |
SyncOptions |
Configuration options for synchronization. |
Example
Invokes an RPC with detailed parameters for buffer, target, delivery mode, grouping, and sequencing.
| Parameter | Type | Description |
|---|---|---|
msgId |
byte |
The ID of the RPC message to invoke. |
peer |
NetworkPeer |
The peer for whom the RPC is invoked. |
identityId |
int |
The ID of the NetworkIdentity to target. |
buffer |
DataBuffer |
Optional data buffer containing the message content. |
target |
Target |
The target for the RPC, such as all clients or a specific peer. |
deliveryMode |
DeliveryMode |
Defines the delivery mode, such as ReliableOrdered. |
groupId |
int |
ID for grouping related messages (default is 0). |
dataCache |
DataCache |
Specifies how the message data is cached (default is None). |
sequenceChannel |
byte |
Channel for message sequencing (default is 0). |
Example
| C# | |
|---|---|
Remarks
- Identity-Specific Invocation: Targets a specific
NetworkIdentityusingidentityId, ensuring the RPC is directed only to matching identities. - Flexible Targeting: Supports targeting all clients, server-only, or specific peers, allowing control over who receives the RPC.
- Reliability Options: Provides reliable, ordered delivery or lightweight, unordered options depending on the use case.
- Sequencing and Grouping: Allows message organization with group IDs and sequence channels, ensuring consistency across channels.
- Usage: Ideal for invoking RPCs on specific entities across clients, especially for commands that are only relevant to targeted objects or scripts.
Note
This function is also available on the client side, but does not load the peer parameter.
GlobalInvoke(Server)⚓️
Invokes a global Remote Procedure Call (RPC) on clients, independent of script instance or identity, using customizable options for target, delivery mode, grouping, and sequencing.
- Signature:
public static void GlobalInvoke(byte msgId, NetworkPeer peer, SyncOptions options)public static void GlobalInvoke(byte msgId, NetworkPeer peer, DataBuffer buffer = null, Target target = Target.Auto, DeliveryMode deliveryMode = DeliveryMode.ReliableOrdered, int groupId = 0, DataCache dataCache = default, byte sequenceChannel = 0)
Description
The GlobalInvoke method allows the server to invoke a global RPC on connected clients. Unlike instance-based RPCs, GlobalInvoke does not require a specific script instance or identity, allowing any client-side script with the matching RPC to be called directly. This is ideal for general-purpose commands and broadcasts. The method offers flexible parameters for controlling the target recipients, delivery reliability, grouping, and message sequencing.
Parameters
| Parameter | Type | Description |
|---|---|---|
msgId |
byte |
The ID of the RPC message to invoke. |
peer |
NetworkPeer |
The peer for whom the RPC is invoked. |
options |
SyncOptions |
A set of options for synchronization (used in the first overload). |
buffer |
DataBuffer |
Optional data buffer to send. |
target |
Target |
Specifies the target for the RPC, such as all clients or a specific peer (default is All). |
deliveryMode |
DeliveryMode |
Defines the delivery mode, such as ReliableOrdered (default), Unreliable, etc. |
groupId |
int |
ID for grouping related messages together (default is 0). |
dataCache |
DataCache |
Cache option for the data being sent (default is DataCache.None). |
sequenceChannel |
byte |
Channel for message sequencing to manage order consistency across channels (default is 0). |
Overloads
Invokes an RPC on clients using a SyncOptions instance to specify buffer, target, delivery mode, group ID, data cache, and sequence channel.
| Parameter | Type | Description |
|---|---|---|
msgId |
byte |
The ID of the RPC message to invoke. |
peer |
NetworkPeer |
The peer to whom the RPC is invoked. |
options |
SyncOptions |
Configuration options for synchronization. |
Example
Invokes an RPC on clients with detailed parameters for buffer, target, delivery mode, grouping, and sequencing.
| Parameter | Type | Description |
|---|---|---|
msgId |
byte |
The ID of the RPC message to invoke. |
peer |
NetworkPeer |
The peer to whom the RPC is invoked. |
buffer |
DataBuffer |
Optional data buffer containing the message content. |
target |
Target |
The target for the RPC, such as all clients or a specific peer. |
deliveryMode |
DeliveryMode |
Defines the delivery mode, such as ReliableOrdered. |
groupId |
int |
ID for grouping related messages (default is 0). |
dataCache |
DataCache |
Specifies how the message data is cached (default is None). |
sequenceChannel |
byte |
Channel for message sequencing (default is 0). |
Example
Remarks
- Global Invocation: Unlike instance-specific RPCs,
GlobalInvoketargets clients globally, without needing script identity or instance details. - Flexible Targeting: Supports targeting all clients, server-only, or specific peers, allowing flexible control over who receives the RPC.
- Reliability Options: Provides reliable, ordered delivery or lightweight, unordered options depending on the use case.
- Sequencing and Grouping: Allows message organization with group IDs and sequence channels, ensuring consistency across channels.
- Usage: Ideal for broadcasting general-purpose commands or notifications across clients without binding to specific instances.
Note
This function is also available on the client side, but does not load the peer parameter.
GetIdentity⚓️
Retrieves a NetworkIdentity instance by its unique identity ID.
- Signature:
public static NetworkIdentity GetIdentity(int identityId)
Description
The GetIdentity method attempts to retrieve a NetworkIdentity instance using its unique identifier. If the identity is found, it returns the corresponding NetworkIdentity instance; otherwise, it logs an error message and returns null. This method is available on both server and client sides.
Parameters
| Parameter | Type | Description |
|---|---|---|
identityId |
int |
The unique ID of the NetworkIdentity to retrieve. |
Returns
NetworkIdentity: The network identity instance if found;nullotherwise.
Remarks
- If the identity is not found, an error message is logged using
NetworkLogger. - For safer access to network identities, consider using
TryGetIdentitywhich provides a boolean return value indicating success. - The method is available on both server and client sides, but will only return identities that are known to the respective side.
Note
When using this method, it's recommended to check for null return values to handle cases where the identity might not exist.
TryGetIdentity⚓️
Attempts to retrieve a NetworkIdentity instance by its unique identity ID.
- Signature:
public static bool TryGetIdentity(int identityId, out NetworkIdentity identity)
Description
The TryGetIdentity method searches for a NetworkIdentity in the networked identities collection using a unique identityId. If the NetworkIdentity is found, it is returned via the out parameter; otherwise, null is returned. This method provides a safe and efficient way to check if an identity exists before attempting to interact with it.
Parameters
| Parameter | Type | Description |
|---|---|---|
identityId |
int |
The unique ID of the NetworkIdentity to retrieve. |
identity |
NetworkIdentity |
The retrieved NetworkIdentity instance, or null if not found. |
Returns
bool: Returnstrueif theNetworkIdentitywas found; otherwise,false.
Example
AddPrefab⚓️
Adds a prefab to the NetworkManager's registration list if it hasn’t been registered already.
- Signature:
public static void AddPrefab(NetworkIdentity prefab)
Parameters
| Parameter | Type | Description |
|---|---|---|
prefab |
NetworkIdentity |
The prefab to add to the registration list. |
Description
Registers a NetworkIdentity prefab for network spawning. This method checks if a prefab with the same name already exists in the list, and if so, it will not add it again. This ensures that each prefab is unique in the registration list.
Example
GetPrefab⚓️
Retrieves a prefab from the NetworkManager's registered list by either its name or index.
- Signature:
public static NetworkIdentity GetPrefab(string prefabName)public static NetworkIdentity GetPrefab(int index)
Retrieves a prefab by its name.
| Parameter | Type | Description |
|---|---|---|
prefabName |
string |
The name of the prefab to retrieve. |
- Returns:
NetworkIdentity— The prefab with the specified name. - Exceptions: Throws an
Exceptionif the prefab with the specified name is not found.
Retrieves a prefab by its index in the registered list.
| Parameter | Type | Description |
|---|---|---|
index |
int |
The index of the prefab to retrieve in the list. |
- Returns:
NetworkIdentity— The prefab at the specified index. - Exceptions: Throws an
IndexOutOfRangeExceptionif the index is out of bounds.
Description
This method provides two overloads for retrieving a NetworkIdentity prefab from the NetworkManager’s registration list. You can either search by the name of the prefab or by its index within the list. If a matching prefab is found, it is returned; otherwise, an appropriate exception is thrown, ensuring that only registered prefabs are used for network spawning.
Remarks
- By Name: Throws an
Exceptionif the prefab with the specified name is not found in the registration list. - By Index: Throws an
IndexOutOfRangeExceptionif the index is out of bounds. - Useful for scenarios where dynamic instantiation of networked objects is required, either by specific name or by predefined order in the list.
Connect⚓️
Establishes a connection to a specified server address and port. There are two overloads for this method, allowing you to specify a client listening port if needed.
- Signature:
public static void Connect(string address, int port)public static void Connect(string address, int port, int listenPort)
Connects to the server using the specified address and port. The client will use the default listening port defined in Manager.m_ClientListenPort.
| Parameter | Type | Description |
|---|---|---|
address |
string |
The IP address of the server. |
port |
int |
The port number on the server. |
- Exceptions: Throws an
Exceptionif the client is already active, instructing to stop the client before reconnecting.
Connects to the server using the specified address and port, with the client listening on the specified listenPort.
| Parameter | Type | Description |
|---|---|---|
address |
string |
The IP address of the server. |
port |
int |
The port number on the server. |
listenPort |
int |
The port number on which the client listens. |
- Exceptions: Throws an
Exceptionif the client is already active, instructing to stop the client before reconnecting.
Description
The Connect method initiates a connection to the server at a given IP address and port. If the client is already active, an exception is thrown to prevent multiple connections. In the server build configuration (UNITY_SERVER), client connections are disabled.
Remarks
- Server Build: In a server build (
UNITY_SERVER), client connections are not permitted and will log a message instead. - Exception Handling: Ensure to call
StopClient()before reconnecting if the client is already active to avoid exceptions. - Listening Port: Use the overload with
listenPortif a custom listening port is required for the client.
DisconnectPeer⚓️
Disconnects a specified peer from the server.
- Signature:
public static void DisconnectPeer(NetworkPeer peer)
Parameters
| Parameter | Type | Description |
|---|---|---|
peer |
NetworkPeer |
The network peer to disconnect. |
Description
The DisconnectPeer method removes the specified NetworkPeer from the server if the server is currently active. If the server has not been initialized, an exception is thrown to prompt server startup before disconnection.
Example
Disconnect⚓️
Disconnects the local client from the server.
- Signature:
public static void Disconnect()
Description
The Disconnect method terminates the connection between the local client and the server, if the client is currently active. If the client has not been initialized, an exception is thrown to prompt a connection attempt before disconnection.
Example
StopClient⚓️
Stops the local client and ends its connection to the server.
- Signature:
public static void StopClient()
Description
The StopClient method halts the local client’s network operations, fully disconnecting it from the server if it is currently active. If the client has not been initialized, an exception is thrown to prompt a connection attempt before stopping.
Example
FastWrite⚓️
Writes one or more primitive values to a DataBuffer, utilizing stackalloc to avoid allocations and ensure high performance. This method is available in multiple overloads, allowing for writing up to six primitive values in a single call.
- Signature:
public static DataBuffer FastWrite<T1>(T1 t1) where T1 : unmanagedpublic static DataBuffer FastWrite<T1, T2>(T1 t1, T2 t2) where T1 : unmanaged where T2 : unmanagedpublic static DataBuffer FastWrite<T1, T2, T3>(T1 t1, T2 t2, T3 t3) where T1 : unmanaged where T2 : unmanaged where T3 : unmanagedpublic static DataBuffer FastWrite<T1, T2, T3, T4>(T1 t1, T2 t2, T3 t3, T4 t4) where T1 : unmanaged where T2 : unmanaged where T3 : unmanaged where T4 : unmanagedpublic static DataBuffer FastWrite<T1, T2, T3, T4, T5>(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) where T1 : unmanaged where T2 : unmanaged where T3 : unmanaged where T4 : unmanaged where T5 : unmanagedpublic static DataBuffer FastWrite<T1, T2, T3, T4, T5, T6>(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6) where T1 : unmanaged where T2 : unmanaged where T3 : unmanaged where T4 : unmanaged where T5 : unmanaged where T6 : unmanaged
Description
Each overload of FastWrite allows for writing up to six unmanaged primitive values to a DataBuffer. By using stackalloc, these methods avoid memory allocations, making them highly efficient for network messaging. The returned DataBuffer must be disposed or used within a using statement to ensure proper memory management.
Returns
DataBuffer: The network message buffer containing the written values.
Writes a single unmanaged value to the buffer.
| Parameter | Type | Description |
|---|---|---|
t1 |
T1 |
The first value to write to the buffer. |
Writes two unmanaged values to the buffer.
| Parameter | Type | Description |
|---|---|---|
t1 |
T1 |
The first value to write to the buffer. |
t2 |
T2 |
The second value to write to the buffer. |
Writes three unmanaged values to the buffer.
| Parameter | Type | Description |
|---|---|---|
t1 |
T1 |
The first value to write to the buffer. |
t2 |
T2 |
The second value to write to the buffer. |
t3 |
T3 |
The third value to write to the buffer. |
Writes four unmanaged values to the buffer.
| Parameter | Type | Description |
|---|---|---|
t1 |
T1 |
The first value to write to the buffer. |
t2 |
T2 |
The second value to write to the buffer. |
t3 |
T3 |
The third value to write to the buffer. |
t4 |
T4 |
The fourth value to write to the buffer. |
Writes five unmanaged values to the buffer.
| Parameter | Type | Description |
|---|---|---|
t1 |
T1 |
The first value to write to the buffer. |
t2 |
T2 |
The second value to write to the buffer. |
t3 |
T3 |
The third value to write to the buffer. |
t4 |
T4 |
The fourth value to write to the buffer. |
t5 |
T5 |
The fifth value to write to the buffer. |
Writes six unmanaged values to the buffer.
| Parameter | Type | Description |
|---|---|---|
t1 |
T1 |
The first value to write to the buffer. |
t2 |
T2 |
The second value to write to the buffer. |
t3 |
T3 |
The third value to write to the buffer. |
t4 |
T4 |
The fourth value to write to the buffer. |
t5 |
T5 |
The fifth value to write to the buffer. |
t6 |
T6 |
The sixth value to write to the buffer. |
Remarks
- Disposal: The caller must ensure that the returned
DataBufferis disposed or used within ausingstatement to prevent memory leaks. - Performance: By using
stackalloc, this method avoids heap allocations, offering high performance for network messaging. - Generic Constraints: Only unmanaged types are allowed, ensuring compatibility with
DataBufferfor efficient serialization.
FromBinary⚓️
Deserializes an object from binary format using MemoryPackSerializer.
- Signature:
public static T FromBinary<T>(byte[] data, MemoryPackSerializerOptions settings = null)
Type Parameters
| Type Parameter | Description |
|---|---|
T |
The type of the object to deserialize. |
Parameters
| Parameter | Type | Description |
|---|---|---|
data |
byte[] |
The byte array containing the binary data to deserialize. |
settings |
MemoryPackSerializerOptions |
Optional settings for deserialization (default is null). If null, default settings are used. |
Description
The FromBinary method deserializes an object of type T from a binary byte array using the MemoryPackSerializer. It allows specifying optional deserialization settings through MemoryPackSerializerOptions. If no settings are provided, the method uses the default settings defined in BufferWriterExtensions.DefaultMemoryPackSettings.
Returns
T: The deserialized object of typeT.
Example
ToBinary⚓️
Converts an object to binary format using MemoryPackSerializer.
- Signature:
public static byte[] ToBinary<T>(T obj, MemoryPackSerializerOptions settings = null)
Type Parameters
| Type Parameter | Description |
|---|---|
T |
The type of the object to serialize. |
Parameters
| Parameter | Type | Description |
|---|---|---|
obj |
T |
The object to be converted to binary format. |
settings |
MemoryPackSerializerOptions |
Optional settings for serialization (default is null). If null, default settings are used. |
Description
The ToBinary method serializes an object of type T into a binary byte array using MemoryPackSerializer. It allows specifying optional serialization settings via MemoryPackSerializerOptions. If no settings are provided, the method uses the default settings from BufferWriterExtensions.DefaultMemoryPackSettings.
Returns
byte[]: A byte array representing the binary serialization of the object.
Example
FromJson⚓️
Deserializes an object from JSON format.
- Signature:
public static T FromJson<T>(string json, JsonSerializerSettings settings = null)
Type Parameters
| Type Parameter | Description |
|---|---|
T |
The type of the object to deserialize. |
Parameters
| Parameter | Type | Description |
|---|---|---|
json |
string |
The JSON string to deserialize. |
settings |
JsonSerializerSettings |
Optional settings for JSON deserialization (default is null). If null, default settings are used. |
Description
The FromJson method deserializes an object of type T from a JSON string. This method allows specifying optional deserialization settings via JsonSerializerSettings. If no settings are provided, the default settings from BufferWriterExtensions.DefaultJsonSettings are used.
Returns
T: The deserialized object of typeT.
Example
ToJson⚓️
Converts an object to JSON format.
- Signature:
public static string ToJson<T>(T obj, JsonSerializerSettings settings = null)
Type Parameters
| Type Parameter | Description |
|---|---|
T |
The type of the object to serialize. |
Parameters
| Parameter | Type | Description |
|---|---|---|
obj |
T |
The object to be converted to JSON format. |
settings |
JsonSerializerSettings |
Optional settings for JSON serialization (default is null). If null, default settings are used. |
Description
The ToJson method serializes an object of type T into a JSON string using JsonConvert. This method allows for optional JSON serialization settings via JsonSerializerSettings. If no settings are specified, it uses the default settings defined in BufferWriterExtensions.DefaultJsonSettings.
Returns
string: A JSON string representation of the serialized object.
Example
InitializeModule⚓️
Initializes a specified network module based on the provided Module type.
- Signature:
public static void InitializeModule(Module module)
Parameters
| Parameter | Type | Description |
|---|---|---|
module |
Module |
The type of module to initialize. |
Description
The InitializeModule method initializes a network module based on the specified Module type. This includes setting up components for ticking, network time, console, connections, and matchmaking as required. The method ensures that the initialization occurs on the main thread and applies appropriate configurations based on each module’s unique requirements.
Module Types
TickSystem: Initializes a network tick system if one doesn’t already exist. Sets up the tick rate usingManager.m_TickRate.NtpClock: Initializes an SNTP clock based onNetworkClocksettings, configuring interval and tick timing.Console: Initializes aNetworkConsoleinstance.Connection: Sets up the client and server transporters, checks for a transporter component, and configures network connection. Includes logic for auto-starting the server and client based on specific address conditions and the current build configuration.Matchmaking: Initializes the network matchmaking module.
Example
Remarks
- Thread Safety: The method enforces that initialization occurs on the main thread to prevent multithreading issues.
- Connection Transporter: Throws an Exception if no transporter is found on NetworkManager when initializing the Connection module.
- Auto-Start Logic: In the Connection module, auto-start behavior is configured based on the client’s address. The server will only auto-start if the address is recognized as localhost or a public IP address.
- Build Configuration: Auto-starting behavior may vary based on build configuration. For instance, in OMNI_RELEASE builds, both server and client auto-start are enabled by default.
LoadScene / LoadSceneAsync⚓️
Loads a scene by name or index, with options for synchronous or asynchronous loading. These methods also provide optional parameters to destroy the current scene before loading a new one.
- Signature:
public static void LoadScene(string sceneName, LoadSceneMode mode = LoadSceneMode.Single)public static AsyncOperation LoadSceneAsync(string sceneName, LoadSceneMode mode = LoadSceneMode.Single)public static void LoadScene(int index, LoadSceneMode mode = LoadSceneMode.Single)public static AsyncOperation LoadSceneAsync(int index, LoadSceneMode mode = LoadSceneMode.Single)
Parameters
| Parameter | Type | Description |
|---|---|---|
sceneName |
string |
The name of the scene to load. |
index |
int |
The build index of the scene to load. |
mode |
LoadSceneMode |
Specifies whether to load the scene in Single or Additive mode (default is Single). |
Description
Each overload of LoadScene and LoadSceneAsync allows loading scenes either by name or by build index, with the option to specify loading in single or additive mode. The methods also include a call to DestroyScene to clear the current scene before loading the new one, supporting smooth transitions and memory management.
Overloads
Loads a scene by its name in synchronous mode.
| Parameter | Type | Description |
|---|---|---|
sceneName |
string |
The name of the scene to load. |
mode |
LoadSceneMode |
Load mode (Single or Additive). Defaults to Single. |
Loads a scene by its name in asynchronous mode, returning an AsyncOperation.
| Parameter | Type | Description |
|---|---|---|
sceneName |
string |
The name of the scene to load. |
mode |
LoadSceneMode |
Load mode (Single or Additive). Defaults to Single. |
Loads a scene by its build index in synchronous mode.
| Parameter | Type | Description |
|---|---|---|
index |
int |
The build index of the scene to load. |
mode |
LoadSceneMode |
Load mode (Single or Additive). Defaults to Single. |
Loads a scene by its build index in asynchronous mode, returning an AsyncOperation.
| Parameter | Type | Description |
|---|---|---|
index |
int |
The build index of the scene to load. |
mode |
LoadSceneMode |
Load mode (Single or Additive). Defaults to Single. |
Remarks
- DestroyScene: Each method calls
DestroyScenebefore loading to remove the current scene and free resources. - LoadSceneMode: Allows loading in
Singlemode, which replaces the current scene, orAdditivemode, which loads the new scene on top of the existing one. - AsyncOperation: In asynchronous methods, an
AsyncOperationis returned, which can be used to track the progress of scene loading. - Error Handling: Ensure the specified scene name or index is valid to avoid loading errors.
UnloadSceneAsync⚓️
Asynchronously unloads a scene by its name or index with options for unloading behavior.
- Signature:
public static AsyncOperation UnloadSceneAsync(string sceneName, UnloadSceneOptions options = UnloadSceneOptions.None)public static AsyncOperation UnloadSceneAsync(int index, bool useBuildIndex = false, UnloadSceneOptions options = UnloadSceneOptions.None)
Parameters
| Parameter | Type | Description |
|---|---|---|
sceneName |
string |
The name of the scene to unload. |
index |
int |
The index of the scene to unload, which can refer to the build index or scene load order. |
useBuildIndex |
bool |
Indicates if the index parameter should be interpreted as the build index (default is false). |
options |
UnloadSceneOptions |
Options for unloading the scene (default is None). |
Description
The UnloadSceneAsync method provides asynchronous scene unloading capabilities. You can specify the scene to unload either by its name or index, with the option to interpret the index as the build index. Each method first calls DestroyScene to handle any necessary scene cleanup before initiating the asynchronous unloading operation using SceneManager.UnloadSceneAsync.
Overloads
Unloads a scene asynchronously by its name.
| Parameter | Type | Description |
|---|---|---|
sceneName |
string |
The name of the scene to unload. |
options |
UnloadSceneOptions |
Options for unloading the scene (default is None). |
Unloads a scene asynchronously by its index, with the option to interpret the index as the build index.
| Parameter | Type | Description |
|---|---|---|
index |
int |
The index of the scene to unload. |
useBuildIndex |
bool |
Whether the index refers to the build index (default is false). |
options |
UnloadSceneOptions |
Options for unloading the scene (default is None). |
Remarks
- DestroyScene: Each method calls
DestroyScenebefore unloading to handle any necessary cleanup operations and prevent memory leaks. - UnloadSceneOptions: Options can be provided for customizing the unloading process, such as removing dependencies.
- AsyncOperation: Returns an
AsyncOperationthat can be used to track the progress of the unloading process. - Usage: Ideal for offloading scenes when they are no longer needed, helping to manage memory and resources efficiently.
SpawnOnClient⚓️
Instantiates a network identity on the client.
- Signature:
public static NetworkIdentity SpawnOnClient(NetworkIdentity prefab, int peerId, int identityId)
Parameters
| Parameter | Type | Description |
|---|---|---|
prefab |
NetworkIdentity |
The prefab to instantiate. |
peerId |
int |
The ID of the peer who owns the instantiated object. |
identityId |
int |
The ID of the instantiated object. |
Description
The SpawnOnClient method instantiates a NetworkIdentity object on the client. It checks if the instantiated object is owned by the local player and updates the LocalPlayer reference if appropriate. After instantiation, it notifies the server that this identity has been spawned on the client side.
Returns
NetworkIdentity: The instantiated network identity object.
Example
SpawnOnServer⚓️
Instantiates a network identity on the server for a specific peer or with a specified ID.
- Signature:
public static NetworkIdentity SpawnOnServer(NetworkIdentity prefab, NetworkPeer peer)public static NetworkIdentity SpawnOnServer(NetworkIdentity prefab, NetworkPeer peer, int identityId)public static NetworkIdentity SpawnOnServer(NetworkIdentity prefab, int peerId, int identityId = 0)
Parameters
| Parameter | Type | Description |
|---|---|---|
prefab |
NetworkIdentity |
The prefab to instantiate. |
peer |
NetworkPeer |
The peer who will receive the instantiated object. |
peerId |
int |
The ID of the peer who will receive the instantiated object (for overloads without peer). |
identityId |
int |
The ID of the instantiated object. If not provided, a unique ID will be dynamically generated. |
Description
The SpawnOnServer method provides multiple overloads to instantiate a NetworkIdentity object on the server. The instantiation can be performed for a specific peer, identified either by a NetworkPeer object or a peerId. If an identityId is not provided, a unique ID is generated dynamically.
Overloads
Instantiates a network identity on the server for a specific peer.
| Parameter | Type | Description |
|---|---|---|
prefab |
NetworkIdentity |
The prefab to instantiate. |
peer |
NetworkPeer |
The peer who will receive the object. |
Instantiates a network identity on the server for a specific peer with a specified identity ID.
| Parameter | Type | Description |
|---|---|---|
prefab |
NetworkIdentity |
The prefab to instantiate. |
peer |
NetworkPeer |
The peer who will receive the object. |
identityId |
int |
The ID of the instantiated object. |
Instantiates a network identity on the server using a peer ID and an optional identity ID. If identityId is not provided, a unique ID is generated dynamically.
| Parameter | Type | Description |
|---|---|---|
prefab |
NetworkIdentity |
The prefab to instantiate. |
peerId |
int |
The ID of the peer who will receive the object. |
identityId |
int |
The ID of the instantiated object, or 0 for a unique ID. |
Remarks
- Dynamic ID Generation: If
identityIdis0, the method generates a unique ID for the instantiated object usingNetworkHelper.GenerateDynamicUniqueId(). - Peer Association: The instantiated object is associated with the specified peer, allowing for ownership and network synchronization based on peer ID.
- Usage: Suitable for spawning networked objects on the server side, with flexibility for assigning specific identity IDs or generating them dynamically.
Split⚓️
Splits binary data into blocks of a specified size, ensuring efficient data handling and transmission.
- Signature:
public static List<byte[]> Split(ReadOnlySpan<byte> data, int blockSize = 128)
Parameters
| Parameter | Type | Description |
|---|---|---|
data |
ReadOnlySpan<byte> |
The binary data to be split into blocks. |
blockSize |
int |
The size of each block in bytes. Must be a power of 2. Defaults to 128. |
Description
The Split method divides a large binary data array into smaller blocks of equal size. This is particularly useful for network transmission when dealing with large data sets that need to be sent in smaller chunks. The method ensures that the block size is a power of 2 for optimal performance.
Returns
List<byte[]>: A list containing the data blocks, each as a byte array.
Exceptions
- Throws an
Exceptionif the input data length is less than or equal to the block size. - Throws an
Exceptionif the block size is not a power of 2.
Example
| C# | |
|---|---|
StartServer⚓️
Starts the network server with optional port configuration.
- Signature:
public static void StartServer()public static void StartServer(int port)
Parameters
| Parameter | Type | Description |
|---|---|---|
port |
int |
The port number on which the server will listen. |
Description
The StartServer method initializes and starts the network server. When called without parameters, it uses the default port configured in the NetworkManager. The server generates RSA keys for secure communication and begins listening for incoming connections.
Remarks
- Only available in debug mode, editor, or server builds.
- Throws an exception if the server is already running.
- In release mode on client builds, server functionality is disabled.
Example
StopServer⚓️
Stops the network server and cleans up all associated resources.
- Signature:
public static void StopServer()
Description
The StopServer method terminates the server if it is currently active. It performs cleanup operations including:
- Clearing all peer references
- Removing all group references
- Clearing RPC handlers
- Releasing network identities
- Shutting down the server transport
Exceptions
- Throws an
Exceptionif attempting to stop the server when it's not running.
Example
Remarks
- Ensure all clients are properly disconnected before stopping the server.
- After stopping, the server must be restarted using
StartServer()to accept new connections.
SendMessage(Client)⚓️
Sends a message from the client to the server, with options for specifying message content, delivery mode, and sequence channel.
- Signature:
public static void SendMessage(byte msgId, SyncOptions options)public static void SendMessage(byte msgId, DataBuffer buffer = null, DeliveryMode deliveryMode = DeliveryMode.ReliableOrdered, byte sequenceChannel = 0)
Description
The SendMessage method allows the client to send a message to the server, either by using SyncOptions or by specifying detailed parameters such as DataBuffer, DeliveryMode, and SequenceChannel. This function provides flexible control over how messages are delivered, including options for reliable, ordered delivery and custom sequencing to maintain message order.
Parameters
| Parameter | Type | Description |
|---|---|---|
msgId |
byte |
The ID of the message to send. |
options |
SyncOptions |
Configuration options for synchronization, including buffer, delivery mode, and channel. |
buffer |
DataBuffer |
Optional data buffer containing the message content. |
deliveryMode |
DeliveryMode |
Specifies the delivery mode, such as ReliableOrdered or Unreliable (default is ReliableOrdered). |
sequenceChannel |
byte |
Channel for message sequencing, to control message order consistency across channels (default is 0). |
Overloads
Sends a message to the server using SyncOptions to define the buffer, delivery mode, and sequence channel.
| Parameter | Type | Description |
|---|---|---|
msgId |
byte |
The ID of the message to send. |
options |
SyncOptions |
Configuration options for synchronization. |
Sends a message to the server with detailed parameters for buffer, delivery mode, and sequencing.
| Parameter | Type | Description |
|---|---|---|
msgId |
byte |
The ID of the message to send. |
buffer |
DataBuffer |
Optional data buffer containing the message content. |
deliveryMode |
DeliveryMode |
Defines the delivery mode, such as ReliableOrdered. |
sequenceChannel |
byte |
Channel for message sequencing (default is 0). |
Remarks
- Client-to-Server Communication: Allows the client to send messages to the server, ideal for client-initiated requests or updates.
- Flexible Delivery Options: Supports both reliable and unreliable delivery modes, as well as ordered or unordered options, depending on the use case.
- Sequencing Control: Use the
sequenceChannelto maintain message order across multiple channels, ensuring consistency in message flow. - Usage: Commonly used for sending custom data, requests, or status updates from the client to the server.
SendMessage(Server)⚓️
Sends a message from the server to a client or from a client to other networked peers on the server side, with optional configurable options for target, delivery mode, grouping, and sequencing.
- Signature:
public static void SendMessage(byte msgId, NetworkPeer peer, SyncOptions options)public static void SendMessage(byte msgId, NetworkPeer peer, DataBuffer buffer = null, Target target = Target.Auto, DeliveryMode deliveryMode = DeliveryMode.ReliableOrdered, int groupId = 0, DataCache dataCache = default, byte sequenceChannel = 0)
Description
The SendMessage method provides multiple overloads to send a message through the network with flexible settings for various parameters. The method allows specifying the message ID, target peer, and delivery options like delivery mode, group ID, and sequence channel. This enables fine-grained control over how messages are delivered, such as targeting specific peers, ensuring message ordering, and managing data cache. SendMessage is typically used to send data from the server to clients or between clients.
Parameters
| Parameter | Type | Description |
|---|---|---|
msgId |
byte |
The ID of the message to send. |
peer |
NetworkPeer |
The peer to send the message to. |
options |
SyncOptions |
A set of options for message synchronization (used in the first overload). |
buffer |
DataBuffer |
Optional data buffer to send. |
target |
Target |
Specifies the target for the message (default is All). |
deliveryMode |
DeliveryMode |
Defines the delivery mode, such as ReliableOrdered (default), Unreliable, etc. |
groupId |
int |
ID for grouping related messages together (default is 0). |
dataCache |
DataCache |
Cache option for the data being sent (default is DataCache.None). |
sequenceChannel |
byte |
Channel for message sequencing to manage order consistency across channels (default is 0). |
Overloads
Sends a message using a SyncOptions instance to specify buffer, target, delivery mode, group ID, data cache, and sequence channel.
| Parameter | Type | Description |
|---|---|---|
msgId |
byte |
The ID of the message to send. |
peer |
NetworkPeer |
The peer to send the message to. |
options |
SyncOptions |
Configuration options for synchronization. |
Example
Sends a message with detailed parameters for target, delivery mode, grouping, and sequence channel.
| Parameter | Type | Description |
|---|---|---|
msgId |
byte |
The ID of the message to send. |
peer |
NetworkPeer |
The peer to send the message to. |
buffer |
DataBuffer |
Optional data buffer containing the message content. |
target |
Target |
The target for the message, such as All. |
deliveryMode |
DeliveryMode |
Defines the delivery mode, such as ReliableOrdered. |
groupId |
int |
ID for grouping related messages (default is 0). |
dataCache |
DataCache |
Specifies how the message data is cached (default is None). |
sequenceChannel |
byte |
Channel for message sequencing (default is 0). |
Remarks
- Targeting: Allows flexible message targeting, such as sending to all peers, server-only, or specific groups.
- Delivery Mode: Supports reliable and ordered delivery, or unreliable options for lightweight
Properties⚓️
ReceivedBandwidth⚓️
Gets the BandwidthMonitor for monitoring the server's or client incoming bandwidth.
- Signature:
public static BandwidthMonitor ReceivedBandwidth => Connection.Server.ReceivedBandwidth
Description
The ReceivedBandwidth property provides access to the BandwidthMonitor instance that tracks the server’s or client incoming bandwidth usage. This monitor measures the rate of data being received by the server from clients and vice versa, allowing developers to analyze and optimize network performance.
Returns
BandwidthMonitor: An instance that tracks and measures the server's or client incoming bandwidth usage.
Example
SentBandwidth⚓️
Gets the BandwidthMonitor for monitoring the server's or client outgoing bandwidth.
- Signature:
public static BandwidthMonitor SentBandwidth => Connection.Server.SentBandwidth
Description
The SentBandwidth property provides access to the BandwidthMonitor instance that tracks the server’s or client outgoing bandwidth usage. This monitor measures the rate of data being sent from the server to clients and vice versa, allowing developers to observe and manage network performance.
Returns
BandwidthMonitor: An instance that tracks and measures the server's or client outgoing bandwidth usage.
Example
ServerPeer⚓️
Gets the server peer, which represents the server within the network as a special NetworkPeer instance.
- Signature:
public static NetworkPeer ServerPeer { get; }
Description
The ServerPeer property provides a NetworkPeer instance specifically designated to represent the server. This instance can be used to identify the server in network operations, facilitating communication, control, and synchronization between the server and clients.
Remarks
- Server Representation:
ServerPeeracts as a unique identifier for the server in the network, making it distinct from other peers. - Usage: Useful for operations where the server needs to be addressed specifically, such as broadcasting messages to all clients or handling server-specific logic.
- Initialization: The
ServerPeeris instantiated with a default IP endpoint (IPAddress.None) and a port of0, signifying that it is used solely for internal identification rather than active communication.
Example
Remarks
Client-Side: When NetworkManager.Client.ServerPeer is used, it represents the server peer on the client side, exclusively for encryption keys.
Peers⚓️
A dictionary that maps peer IDs to NetworkPeer instances, providing access to all connected peers by their unique IDs.
- Signature:
public static Dictionary<int, NetworkPeer> Peers => PeersById
Description
The Peers property provides a dictionary of all active NetworkPeer instances, keyed by their unique integer IDs. This dictionary allows efficient management and retrieval of network peers, making it easy to access specific clients or other networked entities by their ID.
Returns
Dictionary<int, NetworkPeer>: A dictionary containing all activeNetworkPeerinstances, indexed by peer IDs.
Example
Identities⚓️
A dictionary that stores all NetworkIdentity instances, mapped by their unique identity IDs.
- Signature:
public static Dictionary<int, NetworkIdentity> Identities { get; }
Description
The Identities property provides access to a dictionary containing all NetworkIdentity instances, where each entry is keyed by a unique integer ID (identityId). This dictionary allows efficient retrieval and management of networked identities, enabling easy access to any networked object within the application.
Returns
Dictionary<int, NetworkIdentity>: A dictionary ofNetworkIdentityinstances, keyed by unique identity IDs.
Example
ClientListenPort⚓️
Gets the port on which the client listens for incoming connections.
- Signature:
public static int ClientListenPort
Description
The ClientListenPort property provides access to the client’s listening port, as configured in Manager.m_ClientListenPort. This port is used for network communications on the client side, enabling it to receive messages from the server.
Returns
int: The port number on which the client listens.
Example
ServerListenPort⚓️
Gets the port on which the server listens for incoming connections.
- Signature:
public static int ServerListenPort
Description
The ServerListenPort property provides access to the server’s listening port, as configured in Manager.m_ServerListenPort. This port is used for network communications on the server side, enabling it to accept incoming connections from clients.
Returns
int: The port number on which the server listens.
Example
ConnectPort⚓️
Gets the port on which the client connects to the server.
- Signature:
public static int ConnectPort
Description
The ConnectPort property provides access to the port number that the client uses to connect to the server, as configured in Manager.m_ConnectPort. This port is the endpoint on the server to which the client establishes a connection.
Returns
int: The port number that the client uses to connect to the server.
Example
ConnectAddress⚓️
Gets the IP address or hostname that the client uses to connect to the server.
- Signature:
public static string ConnectAddress
Description
The ConnectAddress property provides access to the IP address or hostname of the server that the client connects to, as configured in Manager.m_ConnectAddress. This address is used as the endpoint for the client’s connection to the server.
Returns
string: The IP address or hostname of the server that the client will connect to.
Example
Framerate⚓️
Gets the current framerate of the application.
- Signature:
public static float Framerate { get; private set; }
Description
The Framerate property provides access to the application's current framerate. This value can be used to monitor the performance of the application in real-time, allowing developers to adjust settings or configurations based on performance metrics.
Returns
float: The current framerate of the application.
Example
CpuTimeMs⚓️
Gets the CPU time in milliseconds per frame.
- Signature:
public static float CpuTimeMs { get; private set; }
Description
The CpuTimeMs property provides access to the CPU time taken per frame, measured in milliseconds. This metric indicates the amount of time the CPU spends processing each frame, which is useful for monitoring and optimizing application performance.
Returns
float: The CPU time per frame, in milliseconds.
Example
ClockTime⚓️
Gets the current clock time in seconds, based on tick timing or elapsed stopwatch time.
- Signature:
public static double ClockTime
Description
The ClockTime property provides the current time in seconds, which is independent of the frame rate. The time source depends on the UseTickTiming setting:
- Tick Timing: If UseTickTiming is enabled, the time is based on the TickSystem.ElapsedTicks.
- Stopwatch: If UseTickTiming is disabled, the time is derived from _stopwatch.Elapsed.TotalSeconds.
This setup allows for precise timing control, whether using fixed ticks or continuous stopwatch measurements.
Returns
double: The current clock time in seconds.
Example
UseTickTiming⚓️
Indicates whether the application is using tick-based timing for the ClockTime property.
- Signature:
public static bool UseTickTiming { get; private set; } = false
Description
The UseTickTiming property determines whether the application relies on tick-based timing or real-time stopwatch timing for the ClockTime property. When UseTickTiming is set to true, ClockTime is based on TickSystem.ElapsedTicks; otherwise, it uses _stopwatch.Elapsed.TotalSeconds.
Returns
bool:trueif tick-based timing is enabled;falseif stopwatch timing is used.
Example
LocalEndPoint⚓️
Gets the local network endpoint, represented by an IPEndPoint instance.
- Signature:
public static IPEndPoint LocalEndPoint { get; private set; }
Description
The LocalEndPoint property provides the IP address and port number of the local peer in the form of an IPEndPoint object. This endpoint represents the local network location, allowing other peers to identify and communicate with the local instance.
Returns
IPEndPoint: The IP address and port of the local peer.
Example
IsClientActive⚓️
Indicates whether the client is currently active, authenticated, and connected.
- Signature:
public static bool IsClientActive { get; private set; }
Description
The IsClientActive property returns true if the client is currently active, authenticated, and connected to the network; otherwise, it returns false. This property is used to determine the client’s connection status, allowing the application to handle client-specific logic based on whether the client is connected.
Returns
bool:trueif the client is active, authenticated, and connected;falseotherwise.
Example
IsServerActive⚓️
Indicates whether the server is currently active.
- Signature:
public static bool IsServerActive { get; private set; }
Description
The IsServerActive property returns true if the server is currently active and ready to accept connections; otherwise, it returns false. This property is used to determine the server’s operational status within the network, allowing the application to enable or disable server-dependent features accordingly.
Returns
bool:trueif the server is active;falseotherwise.
Example
SharedPeer⚓️
Gets the shared peer, which is used to secure communication between peers and the server. This peer is useful for handling encryption and authentication in network communications.
- Signature:
public static NetworkPeer SharedPeer
Description
The SharedPeer property provides access to the network peer responsible for securing communication between the client and server. Depending on the network state, it returns the appropriate peer for encryption and authentication:
- Client-Server Mode: When both the client and server are active, it returns Server.ServerPeer.
- Client Only: When only the client is active, it returns Client.ServerPeer.
- Server Only: When only the server is active, it defaults to Server.ServerPeer.
This flexibility allows the property to provide the correct peer for communication in different network configurations.
Returns
NetworkPeer: The peer used for secure communication.
Example
LocalPeer⚓️
Gets the local network peer, representing the client in the network.
- Signature:
public static NetworkPeer LocalPeer
Description
The LocalPeer property provides access to an instance of the NetworkPeer class that represents the local peer in the network. This property is used to identify and interact with the local peer in networked operations. If the client is not active or authenticated, attempting to access this property throws an exception, ensuring that only active and verified clients can reference LocalPeer.
Returns
NetworkPeer: The local peer in the network.
Example
MainThreadId⚓️
Gets the ID of the main thread on which the application is running.
- Signature:
public static int MainThreadId { get; private set; }
Description
The MainThreadId property provides the thread ID of the main thread, allowing the application to distinguish between the main thread and any background or worker threads. This is useful for ensuring that certain operations, particularly those involving UI updates or Unity API calls, are executed on the main thread.
Returns
int: The ID of the main thread.
Example
Pool⚓️
Gets the pool of DataBuffer instances, used to allocate and deallocate DataBuffer objects efficiently.
- Signature:
public static IObjectPooling<DataBuffer> Pool { get; private set; }
Description
The Pool property provides access to an instance of IObjectPooling<DataBuffer>, which manages the allocation and deallocation of DataBuffer instances. By pooling DataBuffer objects, the application reduces memory allocation overhead and enhances performance, especially in networked applications where buffers are frequently used.
Returns
IObjectPooling<DataBuffer>: The object pool for managingDataBufferinstances.
Example
Console⚓️
Gets the NetworkConsole instance, which provides access to the network console module for sending and receiving commands.
- Signature:
public static NetworkConsole Console
Description
The Console property provides access to the NetworkConsole instance, which enables sending and receiving commands within the network terminal. This console module is used to execute commands, facilitate remote debugging, and interact with networked systems. Attempting to access this property before initializing the console module will throw an exception, ensuring that InitializeModule(Module.Console) is called prior to usage. Additionally, trying to set the console module more than once will result in an exception.
Returns
NetworkConsole: The network console instance used for terminal commands.
Example
Matchmaking⚓️
Gets the NetworkMatchmaking instance, which provides access to the matchmaking module for managing groups and connections.
- Signature:
public static NetworkMatchmaking Matchmaking
Description
The Matchmaking property provides access to the NetworkMatchmaking instance, enabling functionality for creating, deleting, and joining groups in a networked environment. This module is essential for managing player groups and facilitating connections between peers for cooperative or competitive play. Attempting to access this property before initializing the matchmaking module will throw an exception, ensuring that InitializeModule(Module.Matchmaking) is called prior to usage. Additionally, trying to set the matchmaking module more than once will result in an exception.
Returns
NetworkMatchmaking: The matchmaking instance used for group and connection management.
Example
Sntp⚓️
Gets the SimpleNtp instance, which provides access to the NTP (Network Time Protocol) clock module for synchronized time and network latency metrics.
- Signature:
public static SimpleNtp Sntp
Description
The Sntp property provides access to the SimpleNtp instance, allowing for synchronized time between client and server. This module provides functionality for obtaining synchronized ticks or real time, as well as metrics such as round-trip time (RTT) and ping. Attempting to access this property before initializing the NTP clock module will throw an exception, ensuring that InitializeModule(Module.NtpClock) is called prior to usage. Additionally, attempting to set the NTP clock module more than once will result in an exception.
Returns
SimpleNtp: The NTP clock instance used for synchronized time and latency measurements.
Example
TickSystem⚓️
Gets the NetworkTickSystem instance, which manages tick-based events that execute at a fixed rate for consistent network timing.
- Signature:
public static NetworkTickSystem TickSystem
Description
The TickSystem property provides access to the NetworkTickSystem instance, responsible for executing events at a fixed rate (defined by TickRate) to ensure consistent timing across the network. This system is used to send and process information at a steady frequency, facilitating synchronized interactions between clients and servers. Attempting to access this property before initializing the tick system will throw an exception, ensuring that InitializeModule(Module.TickSystem) is called prior to usage.
Returns
NetworkTickSystem: The network tick system instance, used for managing tick-based events and timing synchronization.
Events⚓️
OnSceneLoaded⚓️
An event that is triggered when a scene is loaded, providing access to the scene and the load mode.
- Signature:
public static event Action<Scene, LoadSceneMode> OnSceneLoaded
Description
The OnSceneLoaded event is invoked whenever a scene is loaded, passing the loaded Scene and the LoadSceneMode as parameters. This event allows for actions to be performed in response to a scene loading, such as initializing objects or updating the UI. Developers can subscribe to OnSceneLoaded to execute custom logic each time a new scene is loaded.
Event Parameters
| Parameter | Type | Description |
|---|---|---|
Scene |
Scene |
The scene that has been loaded. |
LoadSceneMode |
LoadSceneMode |
The mode in which the scene was loaded (Single or Additive). |
Example
OnSceneUnloaded⚓️
An event that is triggered when a scene is unloaded, providing access to the scene that was removed.
- Signature:
public static event Action<Scene> OnSceneUnloaded
Description
The OnSceneUnloaded event is invoked whenever a scene is unloaded, passing the Scene that was unloaded as a parameter. This event allows developers to execute custom logic in response to scene unloading, such as cleaning up resources, stopping specific services, or updating the user interface.
Event Parameters
| Parameter | Type | Description |
|---|---|---|
Scene |
Scene |
The scene that has been unloaded. |
Example
OnBeforeSceneLoad⚓️
An event that is triggered just before a scene is loaded or unloaded, providing the scene and the operation mode (load or unload).
- Signature:
public static event Action<Scene, SceneOperationMode> OnBeforeSceneLoad
Description
The OnBeforeSceneLoad event is invoked right before a scene is either loaded or unloaded, passing the target Scene and the SceneOperationMode to indicate the type of operation. This event allows developers to execute preparatory logic, such as pausing certain activities or preparing resources, cleaning up resources, based on whether a scene is about to load or unload.
Event Parameters
| Parameter | Type | Description |
|---|---|---|
Scene |
Scene |
The scene that is about to be loaded or unloaded. |
SceneOperationMode |
SceneOperationMode |
Specifies whether the scene operation is a Load or Unload. |
SceneOperationMode Enum
The SceneOperationMode enum defines the operation type for a scene:
Load: The scene is about to be loaded.Unload: The scene is about to be unloaded.
Example
OnServerInitialized⚓️
An event that is triggered when the server has been successfully initialized.
- Signature:
public static event Action OnServerInitialized
Description
The OnServerInitialized event is invoked once the server has been fully initialized and is ready to accept connections. This event allows developers to execute custom logic or set up necessary resources immediately after the server is initialized, such as configuring game settings, starting background tasks.
Example
OnServerPeerConnected⚓️
An event that is triggered when a peer (client) connects to the server, providing access to the connected peer and the connection phase.
- Signature:
public static event Action<NetworkPeer, Phase> OnServerPeerConnected
Description
The OnServerPeerConnected event is invoked when a peer connects to the server, with the connection process divided into three phases (Begin, Normal, and End). This event provides developers with the connected NetworkPeer instance and the current Phase, allowing for custom logic to be executed at each stage of the connection.
Event Parameters
| Parameter | Type | Description |
|---|---|---|
NetworkPeer |
NetworkPeer |
The peer (client) that has connected to the server. |
Phase |
Phase |
The phase of the connection process (Begin, Normal, End). |
Phase Enum
The Phase enum defines the phases of the peer connection process:
Begin: Indicates the start of the connection process.Normal: Represents the main connection phase, during which the primary actions are performed.End: Marks the completion of the connection process, the peer is connected and authenticated, ready to use.
Example
OnServerPeerDisconnected⚓️
An event that is triggered when a peer (client) disconnects from the server, providing access to the disconnected peer and the disconnection phase.
- Signature:
public static event Action<NetworkPeer, Phase> OnServerPeerDisconnected
Description
The OnServerPeerDisconnected event is invoked when a peer disconnects from the server, with the disconnection process divided into three phases (Begin, Normal, and End). This event provides the NetworkPeer instance that is disconnecting and the current Phase, allowing for custom logic to be executed at each stage of the disconnection.
Event Parameters
| Parameter | Type | Description |
|---|---|---|
NetworkPeer |
NetworkPeer |
The peer (client) that is disconnecting from the server. |
Phase |
Phase |
The phase of the disconnection process (Begin, Normal, End). |
Phase Enum
The Phase enum defines the phases of the peer disconnection process:
Begin: Indicates the start of the disconnection process.Normal: Represents the main phase of disconnection, during which the primary cleanup or disconnection actions are performed.End: Marks the completion of the disconnection process, the peer has fully disconnected from the server and all resources have been cleaned up.
Example
OnClientConnected⚓️
An event that is triggered when the client successfully connects to the server.
- Signature:
public static event Action OnClientConnected
Description
The OnClientConnected event is invoked when the client establishes a successful connection with the server. This event allows developers to execute custom logic upon client connection, such as initializing UI elements, loading player data, or notifying the user of the connection status.
Example
OnClientDisconnected⚓️
An event that is triggered when the client disconnects from the server, providing a message with the disconnection reason.
- Signature:
public static event Action<string> OnClientDisconnected
Description
The OnClientDisconnected event is invoked when the client disconnects from the server. This event provides a string message detailing the reason for disconnection, allowing developers to display informative messages to users, handle cleanup tasks, or attempt reconnection based on the disconnection reason.
Event Parameters
| Parameter | Type | Description |
|---|---|---|
string |
string |
A message explaining the disconnection reason. |
Example
OnClientIdentitySpawned⚓️
An event that is triggered when a NetworkIdentity is spawned on the client, providing access to the spawned identity.
- Signature:
public static event Action<NetworkIdentity> OnClientIdentitySpawned
Description
The OnClientIdentitySpawned event is invoked whenever a NetworkIdentity is successfully spawned on the client. This event provides the spawned NetworkIdentity instance, allowing developers to perform setup, initialize components, or trigger gameplay elements related to the spawned entity.
Event Parameters
| Parameter | Type | Description |
|---|---|---|
NetworkIdentity |
NetworkIdentity |
The network identity that was spawned. |
Example
OnPeerSharedDataChanged⚓️
An event that is triggered on the client when the server modifies a specific key in the shared data of a network peer.
- Signature:
public static event Action<NetworkPeer, string> OnPeerSharedDataChanged
Description
The OnPeerSharedDataChanged event is invoked whenever the server updates a key in a peer's shared data. This event provides both the affected NetworkPeer instance and the key that was modified, allowing clients to react to changes in peer-specific shared data.
Event Parameters
| Parameter | Type | Description |
|---|---|---|
NetworkPeer |
NetworkPeer |
The peer whose shared data was modified. |
string |
string |
The key in the shared data that was changed. |
Example
OnGroupSharedDataChanged⚓️
An event that is triggered on the client when the server modifies a specific key in the shared data of a network group.
- Signature:
public static event Action<NetworkGroup, string> OnGroupSharedDataChanged
Description
The OnGroupSharedDataChanged event is invoked whenever the server updates a key in a group's shared data. This event provides both the affected NetworkGroup instance and the key that was modified, allowing clients to react to changes in group-specific shared data.
Event Parameters
| Parameter | Type | Description |
|---|---|---|
NetworkGroup |
NetworkGroup |
The group whose shared data was modified. |
string |
string |
The key in the shared data that was changed. |
Example
OnMessage(Client)⚓️
Represents an event that is triggered when a custom message is received by the client, providing the message ID, data buffer, and sequence channel.
- Signature:
public static event Action<byte, DataBuffer, int> OnMessage
Description
The OnMessage event is invoked whenever the client receives a custom message. This event provides access to the message ID, the data buffer containing the message content, and the sequence channel. Developers can subscribe to OnMessage to handle incoming messages, enabling custom message processing and response handling on the client side.
Event Parameters
| Parameter | Type | Description |
|---|---|---|
byte |
byte |
The ID of the received message. |
DataBuffer |
DataBuffer |
The data buffer containing the message content. |
int |
int |
The sequence channel used for message ordering consistency. |
Example
OnMessage(Server)⚓️
An event that is triggered when a custom message is received by the server, providing access to the message ID, data buffer, originating peer, and sequence channel.
- Signature:
public static event Action<byte, DataBuffer, NetworkPeer, int> OnMessage
Description
The OnMessage event is invoked when the server receives a custom message, allowing developers to handle the incoming message data. This event provides the message ID, data content, the peer who sent the message, and the sequence channel used to manage message ordering. It serves as an interface for handling various types of client-server communication in a flexible manner.
Event Parameters
| Parameter | Type | Description |
|---|---|---|
byte |
byte |
The ID of the received message. |
DataBuffer |
DataBuffer |
The data buffer containing the message content. |
NetworkPeer |
NetworkPeer |
The peer who sent the message. |
int |
int |
The sequence channel used for message ordering and consistency. |