Skip to content

Mirror::NetworkManager

Inherits from MonoBehaviour

Inherited by NetworkManagerListServer, AdditiveNetworkManager, BasicNetManager, BenchmarkNetworkManager, ChatNetworkManager, MultiSceneNetManager, NetworkManagerPong, NetworkRoomManager

Public Functions

Name
virtual void OnValidate()
virtual so that inheriting classes' OnValidate() can call base.OnValidate() too
virtual void Awake()
virtual so that inheriting classes' Awake() can call base.Awake() too
virtual void Start()
virtual so that inheriting classes' Start() can call base.Start() too
virtual void LateUpdate()
virtual so that inheriting classes' LateUpdate() can call base.LateUpdate() too
void StartServer()
This starts a new server.
void StartClient()
This starts a network client. It uses the networkAddress property as the address to connect to.
void StartClient(Uri uri)
This starts a network client. It uses the Uri parameter as the address to connect to.
void StartHost()
This starts a network "host" - a server and client in the same application.
void StopHost()
This stops both the client and the server that the manager is using.
void StopServer()
Stops the server that the manager is using.
void StopClient()
Stops the client that the manager is using.
virtual void OnApplicationQuit()
called when quitting the application by closing the window / pressing stop in the editor
virtual void ConfigureServerFrameRate()
Set the frame rate for a headless server.
virtual void OnDestroy()
virtual so that inheriting classes' OnDestroy() can call base.OnDestroy() too
virtual void ServerChangeScene(string newSceneName)
This causes the server to switch scenes and sets the networkSceneName.
Transform GetStartPosition()
This finds a spawn position based on NetworkStartPosition objects in the scene.
virtual void OnServerConnect(NetworkConnection conn)
Called on the server when a new client connects.
virtual void OnServerDisconnect(NetworkConnection conn)
Called on the server when a client disconnects.
virtual void OnServerReady(NetworkConnection conn)
Called on the server when a client is ready.
virtual void OnServerAddPlayer(NetworkConnection conn)
Called on the server when a client adds a new player with ClientScene.AddPlayer.
virtual void OnServerError(NetworkConnection conn, int errorCode)
Called on the server when a network error occurs for a client connection.
virtual void OnServerChangeScene(string newSceneName)
Called from ServerChangeScene immediately before SceneManager.LoadSceneAsync is executed
virtual void OnServerSceneChanged(string sceneName)
Called on the server when a scene is completed loaded, when the scene load was initiated by the server with ServerChangeScene().
virtual void OnClientConnect(NetworkConnection conn)
Called on the client when connected to a server.
virtual void OnClientDisconnect(NetworkConnection conn)
Called on clients when disconnected from a server.
virtual void OnClientError(NetworkConnection conn, int errorCode)
Called on clients when a network error occurs.
virtual void OnClientNotReady(NetworkConnection conn)
Called on clients when a servers tells the client it is no longer ready.
virtual void OnClientChangeScene(string newSceneName, SceneOperation sceneOperation, bool customHandling)
Called from ClientChangeScene immediately before SceneManager.LoadSceneAsync is executed
virtual void OnClientSceneChanged(NetworkConnection conn)
Called on clients when a scene has completed loaded, when the scene load was initiated by the server.
virtual void OnStartHost()
This is invoked when a host is started.
virtual void OnStartServer()
This is invoked when a server is started - including when a host is started.
virtual void OnStartClient()
This is invoked when the client is started.
virtual void OnStopServer()
This is called when a server is stopped - including when a host is stopped.
virtual void OnStopClient()
This is called when a client is stopped.
virtual void OnStopHost()
This is called when a host is stopped.
bool IsSceneActive(string scene)
void Shutdown()
This is the only way to clear the singleton, so another instance can be created.
void RegisterStartPosition(Transform start)
Registers the transform of a game object as a player spawn location.
void UnRegisterStartPosition(Transform start)
Unregisters the transform of a game object as a player spawn location.

Public Properties

Name
NetworkManager singleton
NetworkManager singleton
NetworkManagerMode mode
string networkSceneName
The name of the current network scene.

Public Attributes

Name
bool dontDestroyOnLoad
A flag to control whether the NetworkManager object is destroyed when the scene changes.
bool runInBackground
Controls whether the program runs when it is in the background.
bool autoStartServerBuild
Automatically invoke StartServer()
bool showDebugMessages
Enables verbose debug messages in the console
int serverTickRate
Server Update frequency, per second. Use around 60Hz for fast paced games like Counter-Strike to minimize latency. Use around 30Hz for games like WoW to minimize computations. Use around 1-10Hz for slow paced games like EVE.
string offlineScene
The scene to switch to when offline.
string onlineScene
The scene to switch to when online.
string networkAddress
The network address currently in use.
int maxConnections
The maximum number of concurrent network connections to support.
bool disconnectInactiveConnections
Should the server disconnect remote connections that have gone silent for more than Server Idle Timeout?
float disconnectInactiveTimeout
Timeout in seconds since last message from a client after which server will auto-disconnect.
NetworkAuthenticator authenticator
GameObject playerPrefab
The default prefab to be used to create player objects on the server.
bool autoCreatePlayer
A flag to control whether or not player objects are automatically created on connect, and on scene change.
PlayerSpawnMethod playerSpawnMethod
The current method of spawning players used by the NetworkManager.
List< GameObject > spawnPrefabs
List of prefabs that will be registered with the spawning system.
int numPlayers
Number of active player objects across all connections on the server.
bool isNetworkActive
True if the server or client is started and running
bool clientLoadedScene
This is true if the client loaded a new scene when connecting to the server.
UnityEngine.AsyncOperation loadingSceneAsync
int startPositionIndex
List< Transform > startPositions
List of transforms populted by NetworkStartPosition components found in the scene.

Protected Attributes

Name
Transport transport

Public Functions Documentation

function OnValidate

inline virtual void OnValidate()

virtual so that inheriting classes' OnValidate() can call base.OnValidate() too

Reimplemented by: Mirror::NetworkRoomManager::OnValidate

function Awake

inline virtual void Awake()

virtual so that inheriting classes' Awake() can call base.Awake() too

function Start

inline virtual void Start()

virtual so that inheriting classes' Start() can call base.Start() too

function LateUpdate

inline virtual void LateUpdate()

virtual so that inheriting classes' LateUpdate() can call base.LateUpdate() too

function StartServer

inline void StartServer()

This starts a new server.

function StartClient

inline void StartClient()

This starts a network client. It uses the networkAddress property as the address to connect to.

This makes the newly created client connect to the server immediately.

function StartClient

inline void StartClient(
    Uri uri
)

This starts a network client. It uses the Uri parameter as the address to connect to.

Parameters:

  • uri location of the server to connect to

This makes the newly created client connect to the server immediately.

function StartHost

inline void StartHost()

This starts a network "host" - a server and client in the same application.

The client returned from StartHost() is a special "local" client that communicates to the in-process server using a message queue instead of the real network. But in almost all other cases, it can be treated as a normal client.

function StopHost

inline void StopHost()

This stops both the client and the server that the manager is using.

function StopServer

inline void StopServer()

Stops the server that the manager is using.

function StopClient

inline void StopClient()

Stops the client that the manager is using.

function OnApplicationQuit

inline virtual void OnApplicationQuit()

called when quitting the application by closing the window / pressing stop in the editor

virtual so that inheriting classes' OnApplicationQuit() can call base.OnApplicationQuit() too

function ConfigureServerFrameRate

inline virtual void ConfigureServerFrameRate()

Set the frame rate for a headless server.

Override if you wish to disable the behavior or set your own tick rate.

function OnDestroy

inline virtual void OnDestroy()

virtual so that inheriting classes' OnDestroy() can call base.OnDestroy() too

function ServerChangeScene

inline virtual void ServerChangeScene(
    string newSceneName
)

This causes the server to switch scenes and sets the networkSceneName.

Parameters:

  • newSceneName

Reimplemented by: Mirror::NetworkRoomManager::ServerChangeScene

Clients that connect to this server will automatically switch to this scene. This is called autmatically if onlineScene or offlineScene are set, but it can be called from user code to switch scenes again while the game is in progress. This automatically sets clients to be not-ready. The clients must call NetworkClient.Ready() again to participate in the new scene.

function GetStartPosition

inline Transform GetStartPosition()

This finds a spawn position based on NetworkStartPosition objects in the scene.

Return: Returns the transform to spawn a player at, or null.

This is used by the default implementation of OnServerAddPlayer.

function OnServerConnect

inline virtual void OnServerConnect(
    NetworkConnection conn
)

Called on the server when a new client connects.

Parameters:

  • conn Connection from client.

Reimplemented by: Mirror::Cloud::Example::NetworkManagerListServer::OnServerConnect, Mirror::NetworkRoomManager::OnServerConnect

Unity calls this on the Server when a Client connects to the Server. Use an override to tell the NetworkManager what to do when a client connects to the server.

function OnServerDisconnect

inline virtual void OnServerDisconnect(
    NetworkConnection conn
)

Called on the server when a client disconnects.

Parameters:

  • conn Connection from client.

Reimplemented by: Mirror::Examples::Pong::NetworkManagerPong::OnServerDisconnect, Mirror::Cloud::Example::NetworkManagerListServer::OnServerDisconnect, Mirror::NetworkRoomManager::OnServerDisconnect

This is called on the Server when a Client disconnects from the Server. Use an override to decide what should happen when a disconnection is detected.

function OnServerReady

inline virtual void OnServerReady(
    NetworkConnection conn
)

Called on the server when a client is ready.

Parameters:

  • conn Connection from client.

Reimplemented by: Mirror::NetworkRoomManager::OnServerReady

The default implementation of this function calls NetworkServer.SetClientReady() to continue the network setup process.

function OnServerAddPlayer

inline virtual void OnServerAddPlayer(
    NetworkConnection conn
)

Called on the server when a client adds a new player with ClientScene.AddPlayer.

Parameters:

  • conn Connection from client.

Reimplemented by: Mirror::Cloud::Example::NetworkManagerListServerPong::OnServerAddPlayer, Mirror::Examples::Pong::NetworkManagerPong::OnServerAddPlayer, Mirror::Examples::Basic::BasicNetManager::OnServerAddPlayer, Mirror::Examples::MultipleAdditiveScenes::MultiSceneNetManager::OnServerAddPlayer, Mirror::NetworkRoomManager::OnServerAddPlayer

The default implementation for this function creates a new player object from the playerPrefab.

function OnServerError

inline virtual void OnServerError(
    NetworkConnection conn,
    int errorCode
)

Called on the server when a network error occurs for a client connection.

Parameters:

  • conn Connection from client.
  • errorCode Error code.

function OnServerChangeScene

inline virtual void OnServerChangeScene(
    string newSceneName
)

Called from ServerChangeScene immediately before SceneManager.LoadSceneAsync is executed

Parameters:

  • newSceneName Name of the scene that's about to be loaded

This allows server to do work / cleanup / prep before the scene changes.

function OnServerSceneChanged

inline virtual void OnServerSceneChanged(
    string sceneName
)

Called on the server when a scene is completed loaded, when the scene load was initiated by the server with ServerChangeScene().

Parameters:

  • sceneName The name of the new scene.

Reimplemented by: Mirror::NetworkRoomManager::OnServerSceneChanged

function OnClientConnect

inline virtual void OnClientConnect(
    NetworkConnection conn
)

Called on the client when connected to a server.

Parameters:

  • conn Connection to the server.

Reimplemented by: Mirror::Examples::Chat::ChatNetworkManager::OnClientConnect, Mirror::NetworkRoomManager::OnClientConnect

The default implementation of this function sets the client as ready and adds a player. Override the function to dictate what happens when the client connects.

function OnClientDisconnect

inline virtual void OnClientDisconnect(
    NetworkConnection conn
)

Called on clients when disconnected from a server.

Parameters:

  • conn Connection to the server.

Reimplemented by: Mirror::NetworkRoomManager::OnClientDisconnect

This is called on the client when it disconnects from the server. Override this function to decide what happens when the client disconnects.

function OnClientError

inline virtual void OnClientError(
    NetworkConnection conn,
    int errorCode
)

Called on clients when a network error occurs.

Parameters:

  • conn Connection to a server.
  • errorCode Error code.

function OnClientNotReady

inline virtual void OnClientNotReady(
    NetworkConnection conn
)

Called on clients when a servers tells the client it is no longer ready.

Parameters:

  • conn Connection to the server.

This is commonly used when switching scenes.

function OnClientChangeScene

inline virtual void OnClientChangeScene(
    string newSceneName,
    SceneOperation sceneOperation,
    bool customHandling
)

Called from ClientChangeScene immediately before SceneManager.LoadSceneAsync is executed

Parameters:

  • newSceneName Name of the scene that's about to be loaded
  • sceneOperation Scene operation that's about to happen
  • customHandling true to indicate that scene loading will be handled through overrides

This allows client to do work / cleanup / prep before the scene changes.

function OnClientSceneChanged

inline virtual void OnClientSceneChanged(
    NetworkConnection conn
)

Called on clients when a scene has completed loaded, when the scene load was initiated by the server.

Parameters:

  • conn The network connection that the scene change message arrived on.

Reimplemented by: Mirror::NetworkRoomManager::OnClientSceneChanged

Scene changes can cause player objects to be destroyed. The default implementation of OnClientSceneChanged in the NetworkManager is to add a player object for the connection if no player object exists.

function OnStartHost

inline virtual void OnStartHost()

This is invoked when a host is started.

Reimplemented by: Mirror::NetworkRoomManager::OnStartHost

StartHost has multiple signatures, but they all cause this hook to be called.

function OnStartServer

inline virtual void OnStartServer()

This is invoked when a server is started - including when a host is started.

Reimplemented by: Mirror::Examples::Additive::AdditiveNetworkManager::OnStartServer, Mirror::Examples::Chat::ChatNetworkManager::OnStartServer, Mirror::Examples::Benchmark::BenchmarkNetworkManager::OnStartServer, Mirror::Cloud::Example::NetworkManagerListServer::OnStartServer, Mirror::Examples::MultipleAdditiveScenes::MultiSceneNetManager::OnStartServer, Mirror::NetworkRoomManager::OnStartServer

StartServer has multiple signatures, but they all cause this hook to be called.

function OnStartClient

inline virtual void OnStartClient()

This is invoked when the client is started.

Reimplemented by: Mirror::NetworkRoomManager::OnStartClient

function OnStopServer

inline virtual void OnStopServer()

This is called when a server is stopped - including when a host is stopped.

Reimplemented by: Mirror::Examples::Additive::AdditiveNetworkManager::OnStopServer, Mirror::Cloud::Example::NetworkManagerListServer::OnStopServer, Mirror::Examples::MultipleAdditiveScenes::MultiSceneNetManager::OnStopServer, Mirror::NetworkRoomManager::OnStopServer

function OnStopClient

inline virtual void OnStopClient()

This is called when a client is stopped.

Reimplemented by: Mirror::Examples::Additive::AdditiveNetworkManager::OnStopClient, Mirror::Examples::MultipleAdditiveScenes::MultiSceneNetManager::OnStopClient, Mirror::NetworkRoomManager::OnStopClient

function OnStopHost

inline virtual void OnStopHost()

This is called when a host is stopped.

Reimplemented by: Mirror::NetworkRoomManager::OnStopHost

function IsSceneActive

static inline bool IsSceneActive(
    string scene
)

function Shutdown

static inline void Shutdown()

This is the only way to clear the singleton, so another instance can be created.

function RegisterStartPosition

static inline void RegisterStartPosition(
    Transform start
)

Registers the transform of a game object as a player spawn location.

Parameters:

  • start Transform to register.

This is done automatically by NetworkStartPosition components, but can be done manually from user script code.

function UnRegisterStartPosition

static inline void UnRegisterStartPosition(
    Transform start
)

Unregisters the transform of a game object as a player spawn location.

Parameters:

  • start Transform to unregister.

This is done automatically by the NetworkStartPosition component, but can be done manually from user code.

Public Property Documentation

property singleton

static NetworkManager singleton;

NetworkManager singleton

property mode

NetworkManagerMode mode;

property networkSceneName

static string networkSceneName;

The name of the current network scene.

This is populated if the NetworkManager is doing scene management. Calls to ServerChangeScene() cause this to change. New clients that connect to a server will automatically load this scene.

This is used to make sure that all scene changes are initialized by Mirror.

Loading a scene manually wont set networkSceneName, so Mirror would still load it again on start.

Public Attributes Documentation

variable dontDestroyOnLoad

bool dontDestroyOnLoad = true;

A flag to control whether the NetworkManager object is destroyed when the scene changes.

This should be set if your game has a single NetworkManager that exists for the lifetime of the process. If there is a NetworkManager in each scene, then this should not be set.

variable runInBackground

bool runInBackground = true;

Controls whether the program runs when it is in the background.

This is required when multiple instances of a program using networking are running on the same machine, such as when testing using localhost. But this is not recommended when deploying to mobile platforms.

variable autoStartServerBuild

bool autoStartServerBuild = true;

Automatically invoke StartServer()

If the application is a Server Build, StartServer is automatically invoked.

Server build is true when "Server build" is checked in build menu, or BuildOptions.EnableHeadlessMode flag is in BuildOptions

variable showDebugMessages

bool showDebugMessages;

Enables verbose debug messages in the console

variable serverTickRate

int serverTickRate = 30;

Server Update frequency, per second. Use around 60Hz for fast paced games like Counter-Strike to minimize latency. Use around 30Hz for games like WoW to minimize computations. Use around 1-10Hz for slow paced games like EVE.

variable offlineScene

string offlineScene = "";

The scene to switch to when offline.

Setting this makes the NetworkManager do scene management. This scene will be switched to when a network session is completed - such as a client disconnect, or a server shutdown.

variable onlineScene

string onlineScene = "";

The scene to switch to when online.

Setting this makes the NetworkManager do scene management. This scene will be switched to when a network session is started - such as a client connect, or a server listen.

variable networkAddress

string networkAddress = "localhost";

The network address currently in use.

For clients, this is the address of the server that is connected to. For servers, this is the local address.

variable maxConnections

int maxConnections = 4;

The maximum number of concurrent network connections to support.

This effects the memory usage of the network layer.

variable disconnectInactiveConnections

bool disconnectInactiveConnections;

Should the server disconnect remote connections that have gone silent for more than Server Idle Timeout?

variable disconnectInactiveTimeout

float disconnectInactiveTimeout = 60f;

Timeout in seconds since last message from a client after which server will auto-disconnect.

By default, clients send at least a Ping message every 2 seconds.

The Host client is immune from idle timeout disconnection.

Default value is 60 seconds.

variable authenticator

NetworkAuthenticator authenticator;

variable playerPrefab

GameObject playerPrefab;

The default prefab to be used to create player objects on the server.

Player objects are created in the default handler for AddPlayer() on the server. Implementing OnServerAddPlayer overrides this behaviour.

variable autoCreatePlayer

bool autoCreatePlayer = true;

A flag to control whether or not player objects are automatically created on connect, and on scene change.

variable playerSpawnMethod

PlayerSpawnMethod playerSpawnMethod;

The current method of spawning players used by the NetworkManager.

variable spawnPrefabs

List< GameObject > spawnPrefabs = new List<GameObject>();

List of prefabs that will be registered with the spawning system.

For each of these prefabs, ClientScene.RegisterPrefab() will be automatically invoked.

variable numPlayers

int numPlayers => NetworkServer.connections.Count(kv => kv.Value.identity != null);

Number of active player objects across all connections on the server.

This is only valid on the host / server.

variable isNetworkActive

bool isNetworkActive;

True if the server or client is started and running

This is set True in StartServer / StartClient, and set False in StopServer / StopClient

variable clientLoadedScene

bool clientLoadedScene;

This is true if the client loaded a new scene when connecting to the server.

This is set before OnClientConnect is called, so it can be checked there to perform different logic if a scene load occurred.

variable loadingSceneAsync

static UnityEngine.AsyncOperation loadingSceneAsync = "";

variable startPositionIndex

static int startPositionIndex;

variable startPositions

static List< Transform > startPositions = new List<Transform>();

List of transforms populted by NetworkStartPosition components found in the scene.

Protected Attributes Documentation

variable transport

Transport transport;

Updated on 17 January 2021 at 15:32:31 UTC