Class NetworkManager
The main component of the library
Implements
Inherited Members
Namespace: Unity.Netcode
Assembly: solution.dll
Syntax
[AddComponentMenu("Netcode/Network Manager", -100)]
public class NetworkManager : MonoBehaviour, INetworkUpdateSystem
Fields
LogLevel
The log level to use
Declaration
[HideInInspector]
public LogLevel LogLevel
Field Value
Type | Description |
---|---|
LogLevel |
NetworkConfig
The current netcode project configuration
Declaration
[HideInInspector]
public NetworkConfig NetworkConfig
Field Value
Type | Description |
---|---|
NetworkConfig |
PendingClients
Gets a dictionary of the clients that have been accepted by the transport but are still pending by the Netcode. This is only populated on the server.
Declaration
public readonly Dictionary<ulong, PendingClient> PendingClients
Field Value
Type | Description |
---|---|
Dictionary<ulong, PendingClient> |
RpcTarget
Provides access to the various SendTo targets at runtime, as well as runtime-bound targets like Single(ulong, RpcTargetUse), Unity.Netcode.RpcTarget.Group(NativeArray<ulong>), Unity.Netcode.RpcTarget.Group(NativeList<ulong>), Unity.Netcode.RpcTarget.Group(ulong[]), Unity.Netcode.RpcTarget.Group<T>(T), Unity.Netcode.RpcTarget.Not(ulong), Unity.Netcode.RpcTarget.Not(NativeArray<ulong>), Unity.Netcode.RpcTarget.Not(NativeList<ulong>), Unity.Netcode.RpcTarget.Not(ulong[]), and Unity.Netcode.RpcTarget.Not<T>(T)
Declaration
public RpcTarget RpcTarget
Field Value
Type | Description |
---|---|
RpcTarget |
RunInBackground
Gets or sets if the application should be set to run in background
Declaration
[HideInInspector]
public bool RunInBackground
Field Value
Type | Description |
---|---|
bool |
ServerClientId
The client id used to represent the server
Declaration
public const ulong ServerClientId = 0
Field Value
Type | Description |
---|---|
ulong |
Properties
ConnectedClients
Gets a dictionary of connected clients and their clientId keys. This is only accessible on the server.
Declaration
public IReadOnlyDictionary<ulong, NetworkClient> ConnectedClients { get; }
Property Value
Type | Description |
---|---|
IReadOnlyDictionary<ulong, NetworkClient> |
ConnectedClientsIds
Gets a list of just the IDs of all connected clients. This is only accessible on the server.
Declaration
public IReadOnlyList<ulong> ConnectedClientsIds { get; }
Property Value
Type | Description |
---|---|
IReadOnlyList<ulong> |
ConnectedClientsList
Gets a list of connected clients. This is only accessible on the server.
Declaration
public IReadOnlyList<NetworkClient> ConnectedClientsList { get; }
Property Value
Type | Description |
---|---|
IReadOnlyList<NetworkClient> |
ConnectedHostname
The current host name we are connected to, used to validate certificate
Declaration
public string ConnectedHostname { get; }
Property Value
Type | Description |
---|---|
string |
ConnectionApprovalCallback
The callback to invoke during connection approval. Allows client code to decide whether or not to allow incoming client connection
Declaration
public Action<NetworkManager.ConnectionApprovalRequest, NetworkManager.ConnectionApprovalResponse> ConnectionApprovalCallback { get; set; }
Property Value
Type | Description |
---|---|
Action<NetworkManager.ConnectionApprovalRequest, NetworkManager.ConnectionApprovalResponse> |
CustomMessagingManager
Gets the CustomMessagingManager for this NetworkManager
Declaration
public CustomMessagingManager CustomMessagingManager { get; }
Property Value
Type | Description |
---|---|
CustomMessagingManager |
DisconnectReason
When disconnected from the server, the server may send a reason. If a reason was sent, this property will tell client code what the reason was. It should be queried after the OnClientDisconnectCallback is called
Declaration
public string DisconnectReason { get; }
Property Value
Type | Description |
---|---|
string |
IsApproved
Is true when the client has been approved.
Declaration
public bool IsApproved { get; }
Property Value
Type | Description |
---|---|
bool |
Remarks
This only reflects the client's approved status and does not mean the client has finished the connection and synchronization process. The server-host will always be approved upon being starting the NetworkManager NetworkClient.IsConnectedClient
IsClient
Gets Whether or not a client is running
Declaration
public bool IsClient { get; }
Property Value
Type | Description |
---|---|
bool |
IsConnectedClient
When true, the client is connected, approved, and synchronized with
the server.
IsConnected
IsApproved
Declaration
public bool IsConnectedClient { get; }
Property Value
Type | Description |
---|---|
bool |
IsHost
Gets if we are running as host
Declaration
public bool IsHost { get; }
Property Value
Type | Description |
---|---|
bool |
IsListening
Is true when a server or host is listening for connections. Is true when a client is connecting or connected to a network session. Is false when not listening, connecting, or connected.
Declaration
public bool IsListening { get; }
Property Value
Type | Description |
---|---|
bool |
IsServer
Gets Whether or not a server is running
Declaration
public bool IsServer { get; }
Property Value
Type | Description |
---|---|
bool |
LocalClient
Gets the local NetworkClient for this client.
Declaration
public NetworkClient LocalClient { get; }
Property Value
Type | Description |
---|---|
NetworkClient |
LocalClientId
Returns ServerClientId if IsServer or LocalClientId if not
Declaration
public ulong LocalClientId { get; }
Property Value
Type | Description |
---|---|
ulong |
LocalTime
The local NetworkTime
Declaration
public NetworkTime LocalTime { get; }
Property Value
Type | Description |
---|---|
NetworkTime |
MaximumFragmentedMessageSize
Sets the maximum size of a message (or message batch) passed through the transport with the ReliableFragmented delivery. Warning: setting this value too low may result in the SDK becoming non-functional with projects that have a large number of NetworkBehaviours or NetworkVariables, as the SDK relies on the transport's ability to fragment some messages when they grow beyond the MTU size.
Declaration
public int MaximumFragmentedMessageSize { get; set; }
Property Value
Type | Description |
---|---|
int |
MaximumTransmissionUnitSize
Sets the maximum size of a single non-fragmented message (or message batch) passed through the transport. This should represent the transport's default MTU size, minus any transport-level overhead. This value will be used for any remote endpoints that haven't had per-endpoint MTUs set. This value is also used as the size of the temporary buffer used when serializing a single message (to avoid serializing multiple times when sending to multiple endpoints), and thus should be large enough to ensure it can hold each message type. This value defaults to 1296.
Declaration
public int MaximumTransmissionUnitSize { get; set; }
Property Value
Type | Description |
---|---|
int |
NetworkTickSystem
Accessor property for the NetworkTickSystem of the NetworkManager.
Declaration
public NetworkTickSystem NetworkTickSystem { get; }
Property Value
Type | Description |
---|---|
NetworkTickSystem |
NetworkTimeSystem
Accessor property for the NetworkTimeSystem of the NetworkManager. Prefer the use of the LocalTime and ServerTime properties
Declaration
public NetworkTimeSystem NetworkTimeSystem { get; }
Property Value
Type | Description |
---|---|
NetworkTimeSystem |
PrefabHandler
The NetworkPrefabHandler instance created after starting the NetworkManager
Declaration
public NetworkPrefabHandler PrefabHandler { get; }
Property Value
Type | Description |
---|---|
NetworkPrefabHandler |
SceneManager
The NetworkSceneManager instance created after starting the NetworkManager
Declaration
public NetworkSceneManager SceneManager { get; }
Property Value
Type | Description |
---|---|
NetworkSceneManager |
ServerIsHost
Gets whether or not the current server (local or remote) is a host - i.e., also a client
Declaration
public bool ServerIsHost { get; }
Property Value
Type | Description |
---|---|
bool |
ServerTime
The NetworkTime on the server
Declaration
public NetworkTime ServerTime { get; }
Property Value
Type | Description |
---|---|
NetworkTime |
ShutdownInProgress
Can be used to determine if the NetworkManager is currently shutting itself down
Declaration
public bool ShutdownInProgress { get; }
Property Value
Type | Description |
---|---|
bool |
Singleton
The singleton instance of the NetworkManager
Declaration
public static NetworkManager Singleton { get; }
Property Value
Type | Description |
---|---|
NetworkManager |
SpawnManager
Gets the SpawnManager for this NetworkManager
Declaration
public NetworkSpawnManager SpawnManager { get; }
Property Value
Type | Description |
---|---|
NetworkSpawnManager |
Methods
AddNetworkPrefab(GameObject)
Declaration
public void AddNetworkPrefab(GameObject prefab)
Parameters
Type | Name | Description |
---|---|---|
GameObject | prefab |
Exceptions
Type | Condition |
---|---|
Exception |
DisconnectClient(ulong)
Disconnects the remote client.
Declaration
public void DisconnectClient(ulong clientId)
Parameters
Type | Name | Description |
---|---|---|
ulong | clientId | The ClientId to disconnect |
DisconnectClient(ulong, string)
Disconnects the remote client.
Declaration
public void DisconnectClient(ulong clientId, string reason = null)
Parameters
Type | Name | Description |
---|---|---|
ulong | clientId | The ClientId to disconnect |
string | reason | Disconnection reason. If set, client will receive a DisconnectReasonMessage and have the reason available in the NetworkManager.DisconnectReason property |
GetNetworkPrefabOverride(GameObject)
Declaration
public GameObject GetNetworkPrefabOverride(GameObject gameObject)
Parameters
Type | Name | Description |
---|---|---|
GameObject | gameObject | the GameObject to be checked for a NetworkManager defined NetworkPrefab override |
Returns
Type | Description |
---|---|
GameObject | a GameObject that is either the override or if no overrides exist it returns the same as the one passed in as a parameter |
GetPeerMTU(ulong)
Queries the current MTU size for a client. If no MTU has been set for that client, will return MaximumTransmissionUnitSize
Declaration
public int GetPeerMTU(ulong clientId)
Parameters
Type | Name | Description |
---|---|---|
ulong | clientId |
Returns
Type | Description |
---|---|
int |
NetworkUpdate(NetworkUpdateStage)
The update method that is being executed in the context of related NetworkUpdateStage.
Declaration
public void NetworkUpdate(NetworkUpdateStage updateStage)
Parameters
Type | Name | Description |
---|---|---|
NetworkUpdateStage | updateStage | The NetworkUpdateStage that is being executed. |
RemoveNetworkPrefab(GameObject)
Declaration
public void RemoveNetworkPrefab(GameObject prefab)
Parameters
Type | Name | Description |
---|---|---|
GameObject | prefab |
SetPeerMTU(ulong, int)
Set the maximum transmission unit for a specific peer. This determines the maximum size of a message batch that can be sent to that client. If not set for any given client, MaximumTransmissionUnitSize will be used instead.
Declaration
public void SetPeerMTU(ulong clientId, int size)
Parameters
Type | Name | Description |
---|---|---|
ulong | clientId | |
int | size |
SetSingleton()
Set this NetworkManager instance as the static NetworkManager singleton
Declaration
public void SetSingleton()
Shutdown(bool)
Globally shuts down the library. Disconnects clients if connected and stops server if running.
Declaration
public void Shutdown(bool discardMessageQueue = false)
Parameters
Type | Name | Description |
---|---|---|
bool | discardMessageQueue | If false, any messages that are currently in the incoming queue will be handled, and any messages in the outgoing queue will be sent, before the shutdown is processed. If true, NetworkManager will shut down immediately, and any unprocessed or unsent messages will be discarded. |
StartClient()
Starts a client
Declaration
public bool StartClient()
Returns
Type | Description |
---|---|
bool | (true/false) returns true if NetworkManager started in client mode successfully. |
StartHost()
Starts a Host
Declaration
public bool StartHost()
Returns
Type | Description |
---|---|
bool | (true/false) returns true if NetworkManager started in host mode successfully. |
StartServer()
Starts a server
Declaration
public bool StartServer()
Returns
Type | Description |
---|---|
bool | (true/false) returns true if NetworkManager started in server mode successfully. |
Events
OnClientConnectedCallback
The callback to invoke once a client connects. This callback is only ran on the server and on the local client that connects.
It is recommended to use OnConnectionEvent instead, as this will eventually be deprecated
Declaration
public event Action<ulong> OnClientConnectedCallback
Event Type
Type | Description |
---|---|
Action<ulong> |
OnClientDisconnectCallback
The callback to invoke when a client disconnects. This callback is only ran on the server and on the local client that disconnects.
It is recommended to use OnConnectionEvent instead, as this will eventually be deprecated
Declaration
public event Action<ulong> OnClientDisconnectCallback
Event Type
Type | Description |
---|---|
Action<ulong> |
OnClientStarted
The callback to invoke once the local client is ready
Declaration
public event Action OnClientStarted
Event Type
Type | Description |
---|---|
Action |
OnClientStopped
The callback to invoke once the local client stops
Declaration
public event Action<bool> OnClientStopped
Event Type
Type | Description |
---|---|
Action<bool> |
Remarks
The parameter states whether the client was running in host mode
OnConnectionEvent
The callback to invoke on any connection event. See ConnectionEvent and ConnectionEventData for more info.
Declaration
public event Action<NetworkManager, ConnectionEventData> OnConnectionEvent
Event Type
Type | Description |
---|---|
Action<NetworkManager, ConnectionEventData> |
OnServerStarted
This callback is invoked when the local server is started and listening for incoming connections.
Declaration
public event Action OnServerStarted
Event Type
Type | Description |
---|---|
Action |
OnServerStopped
This callback is invoked once the local server is stopped.
Declaration
public event Action<bool> OnServerStopped
Event Type
Type | Description |
---|---|
Action<bool> |
OnTransportFailure
The callback to invoke if the NetworkTransport fails.
Declaration
public event Action OnTransportFailure
Event Type
Type | Description |
---|---|
Action |
Remarks
A failure of the transport is always followed by the NetworkManager shutting down. Recovering from a transport failure would normally entail reconfiguring the transport (e.g. re-authenticating, or recreating a new service allocation depending on the transport) and restarting the client/server/host.