Class NetworkManager
Namespace: Mirror
Assembly: doc.dll
Syntax
public class NetworkManager : MonoBehaviour
Fields
| Improve this Doc View Sourceauthenticator
Declaration
public NetworkAuthenticator authenticator
Field Value
Type | Description |
---|---|
NetworkAuthenticator |
autoCreatePlayer
Enable to automatically create player objects on connect and on scene change.
Declaration
public bool autoCreatePlayer
Field Value
Type | Description |
---|---|
Boolean |
autoStartServerBuild
Should the server auto-start when 'Server Build' is checked in build settings
Declaration
public bool autoStartServerBuild
Field Value
Type | Description |
---|---|
Boolean |
clientLoadedScene
True if the client loaded a new scene when connecting to the server.
Declaration
protected bool clientLoadedScene
Field Value
Type | Description |
---|---|
Boolean |
dontDestroyOnLoad
Enable to keep NetworkManager alive when changing scenes.
Declaration
public bool dontDestroyOnLoad
Field Value
Type | Description |
---|---|
Boolean |
loadingSceneAsync
Declaration
public static AsyncOperation loadingSceneAsync
Field Value
Type | Description |
---|---|
AsyncOperation |
maxConnections
The maximum number of concurrent network connections to support.
Declaration
public int maxConnections
Field Value
Type | Description |
---|---|
Int32 |
networkAddress
Server's address for clients to connect to.
Declaration
public string networkAddress
Field Value
Type | Description |
---|---|
String |
offlineScene
Automatically switch to this scene upon going offline (on start / on disconnect / on shutdown).
Declaration
public string offlineScene
Field Value
Type | Description |
---|---|
String |
onlineScene
Automatically switch to this scene upon going online (after connect/startserver).
Declaration
public string onlineScene
Field Value
Type | Description |
---|---|
String |
playerPrefab
The default prefab to be used to create player objects on the server.
Declaration
public GameObject playerPrefab
Field Value
Type | Description |
---|---|
GameObject |
playerSpawnMethod
Where to spawn players.
Declaration
public PlayerSpawnMethod playerSpawnMethod
Field Value
Type | Description |
---|---|
PlayerSpawnMethod |
runInBackground
Multiplayer games should always run in the background so the network doesn't time out.
Declaration
public bool runInBackground
Field Value
Type | Description |
---|---|
Boolean |
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.
Declaration
public int serverTickRate
Field Value
Type | Description |
---|---|
Int32 |
spawnPrefabs
Prefabs that can be spawned over the network need to be registered here.
Declaration
public List<GameObject> spawnPrefabs
Field Value
Type | Description |
---|---|
List<GameObject> |
startPositionIndex
Declaration
public static int startPositionIndex
Field Value
Type | Description |
---|---|
Int32 |
startPositions
List of transforms populated by NetworkStartPositions
Declaration
public static List<Transform> startPositions
Field Value
Type | Description |
---|---|
List<Transform> |
transport
Declaration
protected Transport transport
Field Value
Type | Description |
---|---|
Transport |
Properties
| Improve this Doc View SourceisNetworkActive
True if the server is running or client is connected/connecting.
Declaration
public bool isNetworkActive { get; }
Property Value
Type | Description |
---|---|
Boolean |
mode
Declaration
public NetworkManagerMode mode { get; }
Property Value
Type | Description |
---|---|
NetworkManagerMode |
networkSceneName
The name of the current network scene.
Declaration
public static string networkSceneName { get; protected set; }
Property Value
Type | Description |
---|---|
String |
numPlayers
Number of active player objects across all connections on the server.
Declaration
public int numPlayers { get; }
Property Value
Type | Description |
---|---|
Int32 |
singleton
The one and only NetworkManager
Declaration
public static NetworkManager singleton { get; }
Property Value
Type | Description |
---|---|
NetworkManager |
Methods
| Improve this Doc View SourceAwake()
Declaration
public virtual void Awake()
ConfigureHeadlessFrameRate()
Set the frame rate for a headless builds. Override to disable or modify.
Declaration
public virtual void ConfigureHeadlessFrameRate()
FinishLoadScene()
Declaration
protected void FinishLoadScene()
GetStartPosition()
Get the next NetworkStartPosition based on the selected PlayerSpawnMethod.
Declaration
public Transform GetStartPosition()
Returns
Type | Description |
---|---|
Transform |
IsSceneActive(String)
Declaration
public static bool IsSceneActive(string scene)
Parameters
Type | Name | Description |
---|---|---|
String | scene |
Returns
Type | Description |
---|---|
Boolean |
LateUpdate()
Declaration
public virtual void LateUpdate()
OnApplicationQuit()
Declaration
public virtual void OnApplicationQuit()
OnClientChangeScene(String, SceneOperation, Boolean)
Called from ClientChangeScene immediately before SceneManager.LoadSceneAsync is executed
Declaration
public virtual void OnClientChangeScene(string newSceneName, SceneOperation sceneOperation, bool customHandling)
Parameters
Type | Name | Description |
---|---|---|
String | newSceneName | |
SceneOperation | sceneOperation | |
Boolean | customHandling |
OnClientConnect()
Called on the client when connected to a server. By default it sets client as ready and adds a player.
Declaration
public virtual void OnClientConnect()
OnClientDisconnect()
Called on clients when disconnected from a server.
Declaration
public virtual void OnClientDisconnect()
OnClientError(Exception)
Declaration
public virtual void OnClientError(Exception exception)
Parameters
Type | Name | Description |
---|---|---|
Exception | exception |
OnClientError(TransportError, String)
Called on client when transport raises an exception.
Declaration
public virtual void OnClientError(TransportError error, string reason)
Parameters
Type | Name | Description |
---|---|---|
TransportError | error | |
String | reason |
OnClientNotReady()
Called on clients when a servers tells the client it is no longer ready, e.g. when switching scenes.
Declaration
public virtual void OnClientNotReady()
OnClientSceneChanged()
Called on clients when a scene has completed loaded, when the scene load was initiated by the server.
Declaration
public virtual void OnClientSceneChanged()
OnDestroy()
Declaration
public virtual void OnDestroy()
OnServerAddPlayer(NetworkConnectionToClient)
Called on server when a client requests to add the player. Adds playerPrefab by default. Can be overwritten.
Declaration
public virtual void OnServerAddPlayer(NetworkConnectionToClient conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnectionToClient | conn |
OnServerChangeScene(String)
Called from ServerChangeScene immediately before SceneManager.LoadSceneAsync is executed
Declaration
public virtual void OnServerChangeScene(string newSceneName)
Parameters
Type | Name | Description |
---|---|---|
String | newSceneName |
OnServerConnect(NetworkConnectionToClient)
Called on the server when a new client connects.
Declaration
public virtual void OnServerConnect(NetworkConnectionToClient conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnectionToClient | conn |
OnServerDisconnect(NetworkConnectionToClient)
Called on the server when a client disconnects.
Declaration
public virtual void OnServerDisconnect(NetworkConnectionToClient conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnectionToClient | conn |
OnServerError(NetworkConnectionToClient, Exception)
Declaration
public virtual void OnServerError(NetworkConnectionToClient conn, Exception exception)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnectionToClient | conn | |
Exception | exception |
OnServerError(NetworkConnectionToClient, TransportError, String)
Called on server when transport raises an exception. NetworkConnection may be null.
Declaration
public virtual void OnServerError(NetworkConnectionToClient conn, TransportError error, string reason)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnectionToClient | conn | |
TransportError | error | |
String | reason |
OnServerReady(NetworkConnectionToClient)
Called on the server when a client is ready (= loaded the scene)
Declaration
public virtual void OnServerReady(NetworkConnectionToClient conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnectionToClient | conn |
OnServerSceneChanged(String)
Called on server after a scene load with ServerChangeScene() is completed.
Declaration
public virtual void OnServerSceneChanged(string sceneName)
Parameters
Type | Name | Description |
---|---|---|
String | sceneName |
OnStartClient()
This is invoked when the client is started.
Declaration
public virtual void OnStartClient()
OnStartHost()
This is invoked when a host is started.
Declaration
public virtual void OnStartHost()
OnStartServer()
This is invoked when a server is started - including when a host is started.
Declaration
public virtual void OnStartServer()
OnStopClient()
This is called when a client is stopped.
Declaration
public virtual void OnStopClient()
OnStopHost()
This is called when a host is stopped.
Declaration
public virtual void OnStopHost()
OnStopServer()
This is called when a server is stopped - including when a host is stopped.
Declaration
public virtual void OnStopServer()
OnValidate()
Declaration
public virtual void OnValidate()
RegisterStartPosition(Transform)
Registers the transform of a game object as a player spawn location.
This is done automatically by NetworkStartPosition components, but can be done manually from user script code.
Declaration
public static void RegisterStartPosition(Transform start)
Parameters
Type | Name | Description |
---|---|---|
Transform | start | Transform to register. |
Reset()
Declaration
public virtual void Reset()
ResetStatics()
Declaration
public static void ResetStatics()
ServerChangeScene(String)
Change the server scene and all client's scenes across the network.
Declaration
public virtual void ServerChangeScene(string newSceneName)
Parameters
Type | Name | Description |
---|---|---|
String | newSceneName |
Start()
Declaration
public virtual void Start()
StartClient()
Starts the client, connects it to the server with networkAddress.
Declaration
public void StartClient()
StartClient(Uri)
Starts the client, connects it to the server via Uri
Declaration
public void StartClient(Uri uri)
Parameters
Type | Name | Description |
---|---|---|
Uri | uri |
StartHost()
Starts a network "host" - a server and client in the same application.
Declaration
public void StartHost()
StartServer()
Starts the server, listening for incoming connections.
Declaration
public void StartServer()
StopClient()
Stops and disconnects the client.
Declaration
public void StopClient()
StopHost()
This stops both the client and the server that the manager is using.
Declaration
public void StopHost()
StopServer()
Stops the server from listening and simulating the game.
Declaration
public void StopServer()
UnRegisterStartPosition(Transform)
Unregister a Transform from start positions.
Declaration
public static void UnRegisterStartPosition(Transform start)
Parameters
Type | Name | Description |
---|---|---|
Transform | start |