Class NetworkManager
The main component of the library
#
InheritanceSystem.Object
NetworkManager
#
ImplementsINetworkUpdateSystem
#
Namespace: Unity.Netcode#
Assembly: MLAPI.dll#
Syntax#
Fields#
LogLevelThe log level to use
#
Declaration#
Field ValueType | Description |
---|---|
LogLevel |
#
NetworkConfigThe current NetworkConfig
#
Declaration#
Field ValueType | Description |
---|---|
NetworkConfig |
#
PendingClientsGets 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#
Field ValueType | Description |
---|---|
System.Collections.Generic.Dictionary\<System.UInt64, PendingClient> |
#
RunInBackgroundGets or sets if the application should be set to run in background
#
Declaration#
Field ValueType | Description |
---|---|
System.Boolean |
#
ServerClientIdThe client id used to represent the server
#
Declaration#
Field ValueType | Description |
---|---|
System.UInt64 |
#
Properties#
ConnectedClientsGets a dictionary of connected clients and their clientId keys. This is only accessible on the server.
#
Declaration#
Property ValueType | Description |
---|---|
IReadOnlyDictionary\<System.UInt64, NetworkClient> |
#
ConnectedClientsIdsGets a list of just the IDs of all connected clients. This is only accessible on the server.
#
Declaration#
Property ValueType | Description |
---|---|
IReadOnlyList\<System.UInt64> |
#
ConnectedClientsListGets a list of connected clients. This is only accessible on the server.
#
Declaration#
Property ValueType | Description |
---|---|
IReadOnlyList\<NetworkClient> |
#
ConnectedHostnameThe current host name we are connected to, used to validate certificate
#
Declaration#
Property ValueType | Description |
---|---|
System.String |
#
ConnectionApprovalCallbackThe callback to invoke during connection approval. Allows client code to decide whether or not to allow incoming client connection
#
Declaration#
Property ValueType | Description |
---|---|
System.Action\<NetworkManager.ConnectionApprovalRequest, NetworkManager.ConnectionApprovalResponse> |
#
CustomMessagingManagerGets the CustomMessagingManager for this NetworkManager
#
Declaration#
Property ValueType | Description |
---|---|
CustomMessagingManager |
#
IsClientGets Whether or not a client is running
#
Declaration#
Property ValueType | Description |
---|---|
System.Boolean |
#
IsConnectedClientGets if we are connected as a client
#
Declaration#
Property ValueType | Description |
---|---|
System.Boolean |
#
IsHostGets if we are running as host
#
Declaration#
Property ValueType | Description |
---|---|
System.Boolean |
#
IsListeningGets Whether or not we are listening for connections
#
Declaration#
Property ValueType | Description |
---|---|
System.Boolean |
#
IsServerGets Whether or not a server is running
#
Declaration#
Property ValueType | Description |
---|---|
System.Boolean |
#
LocalClientGets the local NetworkClient for this client.
#
Declaration#
Property ValueType | Description |
---|---|
NetworkClient |
#
LocalClientIdReturns ServerClientId if IsServer or LocalClientId if not
#
Declaration#
Property ValueType | Description |
---|---|
System.UInt64 |
#
LocalTimeThe local NetworkTime
#
Declaration#
Property ValueType | Description |
---|---|
NetworkTime |
#
NetworkTickSystemAccessor for the NetworkTickSystem of the NetworkManager.
#
Declaration#
Property ValueType | Description |
---|---|
NetworkTickSystem |
#
NetworkTimeSystemAccessor for the NetworkTimeSystem of the NetworkManager. Prefer the use of the LocalTime and ServerTime properties
#
Declaration#
Property ValueType | Description |
---|---|
NetworkTimeSystem |
#
PrefabHandlerThe NetworkPrefabHandler instance created after starting the NetworkManager
#
Declaration#
Property ValueType | Description |
---|---|
NetworkPrefabHandler |
#
SceneManagerThe NetworkSceneManager instance created after starting the NetworkManager
#
Declaration#
Property ValueType | Description |
---|---|
NetworkSceneManager |
#
ServerTimeThe NetworkTime on the server
#
Declaration#
Property ValueType | Description |
---|---|
NetworkTime |
#
ShutdownInProgressCan be used to determine if the NetworkManager is currently shutting itself down
#
Declaration#
Property ValueType | Description |
---|---|
System.Boolean |
#
SingletonThe singleton instance of the NetworkManager
#
Declaration#
Property ValueType | Description |
---|---|
NetworkManager |
#
SpawnManagerGets the SpawnManager for this NetworkManager
#
Declaration#
Property ValueType | Description |
---|---|
NetworkSpawnManager |
#
Methods#
AddNetworkPrefab(GameObject)Adds a new prefab to the network prefab list. This can be any GameObject with a NetworkObject component, from any source (addressables, asset bundles, Resource.Load, dynamically created, etc)
There are three limitations to this method:
- If you have NetworkConfig.ForceSamePrefabs enabled, you can only do this before starting networking, and the server and all connected clients must all have the same exact set of prefabs added via this method before connecting
- Adding a prefab on the server does not automatically add it on the client - it's up to you to make sure the client and server are synchronized via whatever method makes sense for your game (RPCs, configs, deterministic loading, etc)
- If the server sends a Spawn message to a client that has not yet added a prefab for, the spawn message and any other relevant messages will be held for a configurable time (default 1 second, configured via NetworkConfig.SpawnTimeout) before an error is logged. This is intented to enable the SDK to gracefully handle unexpected conditions (slow disks, slow network, etc) that slow down asset loading. This timeout should not be relied on and code shouldn't be written around it - your code should be written so that the asset is expected to be loaded before it's needed.
#
Declaration#
ParametersType | Name | Description |
---|---|---|
GameObject | prefab |
#
ExceptionsType | Condition |
---|---|
System.Exception |
#
DisconnectClient(UInt64)Disconnects the remote client.
#
Declaration#
ParametersType | Name | Description |
---|---|---|
System.UInt64 | clientId | The ClientId to disconnect |
#
GetNetworkPrefabOverride(GameObject)Returns the to use as the override as could be defined within the NetworkPrefab list Note: This should be used to create pools (with NetworkObject components) under the scenario where you are using the Host model as it spawns everything locally. As such, the override will not be applied when spawning locally on a Host. Related Classes and Interfaces: NetworkPrefabHandler INetworkPrefabInstanceHandler
#
Declaration#
ParametersType | Name | Description |
---|---|---|
GameObject | gameObject | the to be checked for a NetworkManager defined NetworkPrefab override |
#
ReturnsType | Description |
---|---|
GameObject | a that is either the override or if no overrides exist it returns the same as the one passed in as a parameter |
#
NetworkUpdate(NetworkUpdateStage)The update method that is being executed in the context of related NetworkUpdateStage.
#
Declaration#
ParametersType | Name | Description |
---|---|---|
NetworkUpdateStage | updateStage | The NetworkUpdateStage that is being executed. |
#
RemoveNetworkPrefab(GameObject)Remove a prefab from the prefab list. As with AddNetworkPrefab, this is specific to the client it's called on - calling it on the server does not automatically remove anything on any of the client processes.
Like AddNetworkPrefab, when NetworkConfig.ForceSamePrefabs is enabled, this cannot be called after connecting.
#
Declaration#
ParametersType | Name | Description |
---|---|---|
GameObject | prefab |
#
SetSingleton()Set this NetworkManager instance as the static NetworkManager singleton
#
Declaration#
Shutdown(Boolean)Globally shuts down the library. Disconnects clients if connected and stops server if running.
#
Declaration#
ParametersType | Name | Description |
---|---|---|
System.Boolean | 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#
ReturnsType | Description |
---|---|
System.Boolean | (/) returns true if NetworkManager started in client mode successfully. |
#
StartHost()Starts a Host
#
Declaration#
ReturnsType | Description |
---|---|
System.Boolean | (/) returns true if NetworkManager started in host mode successfully. |
#
StartServer()Starts a server
#
Declaration#
ReturnsType | Description |
---|---|
System.Boolean | (/) returns true if NetworkManager started in server mode successfully. |
#
Events#
OnClientConnectedCallbackThe callback to invoke once a client connects. This callback is only ran on the server and on the local client that connects.
#
Declaration#
Event TypeType | Description |
---|---|
System.Action\<System.UInt64> |
#
OnClientDisconnectCallbackThe callback to invoke when a client disconnects. This callback is only ran on the server and on the local client that disconnects.
#
Declaration#
Event TypeType | Description |
---|---|
System.Action\<System.UInt64> |
#
OnServerStartedThe callback to invoke once the server is ready
#
Declaration#
Event TypeType | Description |
---|---|
System.Action |
#
OnTransportFailureThe callback to invoke if the NetworkTransport fails.
#
Declaration#
Event TypeType | Description |
---|---|
System.Action |
#
RemarksA 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.
#
ImplementsINetworkUpdateSystem
#
Extension MethodsNetworkUpdateLoop.RegisterAllNetworkUpdates(INetworkUpdateSystem)
NetworkUpdateLoop.RegisterNetworkUpdate(INetworkUpdateSystem, NetworkUpdateStage)
NetworkUpdateLoop.UnregisterAllNetworkUpdates(INetworkUpdateSystem)
NetworkUpdateLoop.UnregisterNetworkUpdate(INetworkUpdateSystem, NetworkUpdateStage)