Skip to content

Mirror::NetworkRoomManager

This is a specialized NetworkManager that includes a networked room. More...

Inherits from NetworkManager, MonoBehaviour

Inherited by NetworkRoomManagerExt, NetworkLobbyManager

Public Classes

Name
struct PendingPlayer

Public Functions

Name
virtual override void OnValidate()
virtual so that inheriting classes' OnValidate() can call base.OnValidate() too
virtual override void OnServerReady(NetworkConnection conn)
Called on the server when a client is ready.
void CheckReadyToBegin()
CheckReadyToBegin checks all of the players in the room to see if their readyToBegin flag is set.
virtual override void OnServerConnect(NetworkConnection conn)
Called on the server when a new client connects.
virtual override void OnServerDisconnect(NetworkConnection conn)
Called on the server when a client disconnects.
virtual override void OnServerAddPlayer(NetworkConnection conn)
Called on the server when a client adds a new player with ClientScene.AddPlayer.
void RecalculateRoomPlayerIndices()
virtual override void ServerChangeScene(string newSceneName)
This causes the server to switch scenes and sets the networkSceneName.
virtual override 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 override void OnStartServer()
This is invoked when a server is started - including when a host is started.
virtual override void OnStartHost()
This is invoked when a host is started.
virtual override void OnStopServer()
This is called when a server is stopped - including when a host is stopped.
virtual override void OnStopHost()
This is called when a host is stopped.
virtual override void OnStartClient()
This is invoked when the client is started.
virtual override void OnClientConnect(NetworkConnection conn)
Called on the client when connected to a server.
virtual override void OnClientDisconnect(NetworkConnection conn)
Called on clients when disconnected from a server.
virtual override void OnStopClient()
This is called when a client is stopped.
virtual override void OnClientSceneChanged(NetworkConnection conn)
Called on clients when a scene has completed loaded, when the scene load was initiated by the server.
virtual void OnRoomStartHost()
This is called on the host when a host is started.
virtual void OnRoomStopHost()
This is called on the host when the host is stopped.
virtual void OnRoomStartServer()
This is called on the server when the server is started - including when a host is started.
virtual void OnRoomStopServer()
This is called on the server when the server is started - including when a host is stopped.
virtual void OnRoomServerConnect(NetworkConnection conn)
This is called on the server when a new client connects to the server.
virtual void OnRoomServerDisconnect(NetworkConnection conn)
This is called on the server when a client disconnects.
virtual void OnRoomServerSceneChanged(string sceneName)
This is called on the server when a networked scene finishes loading.
virtual GameObject OnRoomServerCreateRoomPlayer(NetworkConnection conn)
This allows customization of the creation of the room-player object on the server.
virtual GameObject OnRoomServerCreateGamePlayer(NetworkConnection conn, GameObject roomPlayer)
This allows customization of the creation of the GamePlayer object on the server.
virtual void OnRoomServerAddPlayer(NetworkConnection conn)
This allows customization of the creation of the GamePlayer object on the server.
virtual bool OnRoomServerSceneLoadedForPlayer(NetworkConnection conn, GameObject roomPlayer, GameObject gamePlayer)
This is called on the server when it is told that a client has finished switching from the room scene to a game player scene.
virtual void OnRoomServerPlayersReady()
This is called on the server when all the players in the room are ready.
virtual void OnRoomServerPlayersNotReady()
This is called on the server when CheckReadyToBegin finds that players are not ready
virtual void OnRoomClientEnter()
This is a hook to allow custom behaviour when the game client enters the room.
virtual void OnRoomClientExit()
This is a hook to allow custom behaviour when the game client exits the room.
virtual void OnRoomClientConnect(NetworkConnection conn)
This is called on the client when it connects to server.
virtual void OnRoomClientDisconnect(NetworkConnection conn)
This is called on the client when disconnected from a server.
virtual void OnRoomStartClient()
This is called on the client when a client is started.
virtual void OnRoomStopClient()
This is called on the client when the client stops.
virtual void OnRoomClientSceneChanged(NetworkConnection conn)
This is called on the client when the client is finished loading a new networked scene.
virtual void OnRoomClientAddPlayerFailed()
Called on the client when adding a player to the room fails.
virtual void OnGUI()
virtual so inheriting classes can roll their own

Public Properties

Name
bool allPlayersReady

Public Attributes

Name
string RoomScene
The scene to use for the room. This is similar to the offlineScene of the NetworkManager.
string GameplayScene
The scene to use for the playing the game from the room. This is similar to the onlineScene of the NetworkManager.
List< PendingPlayer > pendingPlayers
List of players that are in the Room
List< NetworkRoomPlayer > roomSlots
These slots track players that enter the room.
int clientIndex

Protected Attributes

Name
int minPlayers
NetworkRoomPlayer roomPlayerPrefab

Additional inherited members

Public Functions inherited from NetworkManager

Name
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
Transform GetStartPosition()
This finds a spawn position based on NetworkStartPosition objects in the scene.
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 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
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 inherited from NetworkManager

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

Public Attributes inherited from NetworkManager

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 inherited from NetworkManager

Name
Transport transport

Detailed Description

class Mirror::NetworkRoomManager;

This is a specialized NetworkManager that includes a networked room.

The room has slots that track the joined players, and a maximum player count that is enforced. It requires that the NetworkRoomPlayer component be on the room player objects.

NetworkRoomManager is derived from NetworkManager, and so it implements many of the virtual functions provided by the NetworkManager class. To avoid accidentally replacing functionality of the NetworkRoomManager, there are new virtual functions on the NetworkRoomManager that begin with "OnRoom". These should be used on classes derived from NetworkRoomManager instead of the virtual functions on NetworkManager.

The OnRoom*() functions have empty implementations on the NetworkRoomManager base class, so the base class functions do not have to be called.

Public Functions Documentation

function OnValidate

inline virtual override void OnValidate()

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

Reimplements: Mirror::NetworkManager::OnValidate

function OnServerReady

inline virtual override void OnServerReady(
    NetworkConnection conn
)

Called on the server when a client is ready.

Parameters:

  • conn Connection from client.

Reimplements: Mirror::NetworkManager::OnServerReady

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

function CheckReadyToBegin

inline void CheckReadyToBegin()

CheckReadyToBegin checks all of the players in the room to see if their readyToBegin flag is set.

If all of the players are ready, then the server switches from the RoomScene to the PlayScene, essentially starting the game. This is called automatically in response to NetworkRoomPlayer.CmdChangeReadyState.

function OnServerConnect

inline virtual override void OnServerConnect(
    NetworkConnection conn
)

Called on the server when a new client connects.

Parameters:

  • conn Connection from client.

Reimplements: Mirror::NetworkManager::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 override void OnServerDisconnect(
    NetworkConnection conn
)

Called on the server when a client disconnects.

Parameters:

  • conn Connection from client.

Reimplements: Mirror::NetworkManager::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 OnServerAddPlayer

inline virtual override void OnServerAddPlayer(
    NetworkConnection conn
)

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

Parameters:

  • conn Connection from client.

Reimplements: Mirror::NetworkManager::OnServerAddPlayer

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

function RecalculateRoomPlayerIndices

inline void RecalculateRoomPlayerIndices()

function ServerChangeScene

inline virtual override void ServerChangeScene(
    string newSceneName
)

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

Parameters:

  • newSceneName

Reimplements: Mirror::NetworkManager::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 OnServerSceneChanged

inline virtual override 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.

Reimplements: Mirror::NetworkManager::OnServerSceneChanged

function OnStartServer

inline virtual override void OnStartServer()

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

Reimplements: Mirror::NetworkManager::OnStartServer

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

function OnStartHost

inline virtual override void OnStartHost()

This is invoked when a host is started.

Reimplements: Mirror::NetworkManager::OnStartHost

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

function OnStopServer

inline virtual override void OnStopServer()

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

Reimplements: Mirror::NetworkManager::OnStopServer

function OnStopHost

inline virtual override void OnStopHost()

This is called when a host is stopped.

Reimplements: Mirror::NetworkManager::OnStopHost

function OnStartClient

inline virtual override void OnStartClient()

This is invoked when the client is started.

Reimplements: Mirror::NetworkManager::OnStartClient

function OnClientConnect

inline virtual override void OnClientConnect(
    NetworkConnection conn
)

Called on the client when connected to a server.

Parameters:

  • conn Connection to the server.

Reimplements: Mirror::NetworkManager::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 override void OnClientDisconnect(
    NetworkConnection conn
)

Called on clients when disconnected from a server.

Parameters:

  • conn Connection to the server.

Reimplements: Mirror::NetworkManager::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 OnStopClient

inline virtual override void OnStopClient()

This is called when a client is stopped.

Reimplements: Mirror::NetworkManager::OnStopClient

function OnClientSceneChanged

inline virtual override void OnClientSceneChanged(
    NetworkConnection conn
)

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

Parameters:

  • conn Connection of the client

Reimplements: Mirror::NetworkManager::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 OnRoomStartHost

inline virtual void OnRoomStartHost()

This is called on the host when a host is started.

function OnRoomStopHost

inline virtual void OnRoomStopHost()

This is called on the host when the host is stopped.

function OnRoomStartServer

inline virtual void OnRoomStartServer()

This is called on the server when the server is started - including when a host is started.

function OnRoomStopServer

inline virtual void OnRoomStopServer()

This is called on the server when the server is started - including when a host is stopped.

Reimplemented by: Mirror::Examples::NetworkRoom::NetworkRoomManagerExt::OnRoomStopServer

function OnRoomServerConnect

inline virtual void OnRoomServerConnect(
    NetworkConnection conn
)

This is called on the server when a new client connects to the server.

Parameters:

  • conn The new connection.

function OnRoomServerDisconnect

inline virtual void OnRoomServerDisconnect(
    NetworkConnection conn
)

This is called on the server when a client disconnects.

Parameters:

  • conn The connection that disconnected.

function OnRoomServerSceneChanged

inline virtual void OnRoomServerSceneChanged(
    string sceneName
)

This is called on the server when a networked scene finishes loading.

Parameters:

  • sceneName Name of the new scene.

function OnRoomServerCreateRoomPlayer

inline virtual GameObject OnRoomServerCreateRoomPlayer(
    NetworkConnection conn
)

This allows customization of the creation of the room-player object on the server.

Parameters:

  • conn The connection the player object is for.

Return: The new room-player object.

By default the roomPlayerPrefab is used to create the room-player, but this function allows that behaviour to be customized.

function OnRoomServerCreateGamePlayer

inline virtual GameObject OnRoomServerCreateGamePlayer(
    NetworkConnection conn,
    GameObject roomPlayer
)

This allows customization of the creation of the GamePlayer object on the server.

Parameters:

  • conn The connection the player object is for.
  • roomPlayer The room player object for this connection.

Return: A new GamePlayer object.

By default the gamePlayerPrefab is used to create the game-player, but this function allows that behaviour to be customized. The object returned from the function will be used to replace the room-player on the connection.

function OnRoomServerAddPlayer

inline virtual void OnRoomServerAddPlayer(
    NetworkConnection conn
)

This allows customization of the creation of the GamePlayer object on the server.

Parameters:

  • conn The connection the player object is for.

This is only called for subsequent GamePlay scenes after the first one.

See OnRoomServerCreateGamePlayer(NetworkConnection, GameObject) to customize the player object for the initial GamePlay scene.

function OnRoomServerSceneLoadedForPlayer

inline virtual bool OnRoomServerSceneLoadedForPlayer(
    NetworkConnection conn,
    GameObject roomPlayer,
    GameObject gamePlayer
)

This is called on the server when it is told that a client has finished switching from the room scene to a game player scene.

Parameters:

  • conn The connection of the player
  • roomPlayer The room player object.
  • gamePlayer The game player object.

Return: False to not allow this player to replace the room player.

Reimplemented by: Mirror::Examples::NetworkRoom::NetworkRoomManagerExt::OnRoomServerSceneLoadedForPlayer

When switching from the room, the room-player is replaced with a game-player object. This callback function gives an opportunity to apply state from the room-player to the game-player object.

function OnRoomServerPlayersReady

inline virtual void OnRoomServerPlayersReady()

This is called on the server when all the players in the room are ready.

Reimplemented by: Mirror::Examples::NetworkRoom::NetworkRoomManagerExt::OnRoomServerPlayersReady

The default implementation of this function uses ServerChangeScene() to switch to the game player scene. By implementing this callback you can customize what happens when all the players in the room are ready, such as adding a countdown or a confirmation for a group leader.

function OnRoomServerPlayersNotReady

inline virtual void OnRoomServerPlayersNotReady()

This is called on the server when CheckReadyToBegin finds that players are not ready

May be called multiple times while not ready players are joining

function OnRoomClientEnter

inline virtual void OnRoomClientEnter()

This is a hook to allow custom behaviour when the game client enters the room.

function OnRoomClientExit

inline virtual void OnRoomClientExit()

This is a hook to allow custom behaviour when the game client exits the room.

function OnRoomClientConnect

inline virtual void OnRoomClientConnect(
    NetworkConnection conn
)

This is called on the client when it connects to server.

Parameters:

  • conn The connection that connected.

function OnRoomClientDisconnect

inline virtual void OnRoomClientDisconnect(
    NetworkConnection conn
)

This is called on the client when disconnected from a server.

Parameters:

  • conn The connection that disconnected.

function OnRoomStartClient

inline virtual void OnRoomStartClient()

This is called on the client when a client is started.

Parameters:

  • roomClient The connection for the room.

function OnRoomStopClient

inline virtual void OnRoomStopClient()

This is called on the client when the client stops.

Reimplemented by: Mirror::Examples::NetworkRoom::NetworkRoomManagerExt::OnRoomStopClient

function OnRoomClientSceneChanged

inline virtual void OnRoomClientSceneChanged(
    NetworkConnection conn
)

This is called on the client when the client is finished loading a new networked scene.

Parameters:

  • conn The connection that finished loading a new networked scene.

function OnRoomClientAddPlayerFailed

inline virtual void OnRoomClientAddPlayerFailed()

Called on the client when adding a player to the room fails.

This could be because the room is full, or the connection is not allowed to have more players.

function OnGUI

inline virtual void OnGUI()

virtual so inheriting classes can roll their own

Reimplemented by: Mirror::Examples::NetworkRoom::NetworkRoomManagerExt::OnGUI

Public Property Documentation

property allPlayersReady

bool allPlayersReady;

Public Attributes Documentation

variable RoomScene

string RoomScene;

The scene to use for the room. This is similar to the offlineScene of the NetworkManager.

variable GameplayScene

string GameplayScene;

The scene to use for the playing the game from the room. This is similar to the onlineScene of the NetworkManager.

variable pendingPlayers

List< PendingPlayer > pendingPlayers = new List<[PendingPlayer](/Documentation/Cops%20And%20Robbers/Classes/structMirror_1_1NetworkRoomManager_1_1PendingPlayer/)>();

List of players that are in the Room

variable roomSlots

List< NetworkRoomPlayer > roomSlots = new List<[NetworkRoomPlayer](/Documentation/Cops%20And%20Robbers/Classes/classMirror_1_1NetworkRoomPlayer/)>();

These slots track players that enter the room.

The slotId on players is global to the game - across all players.

variable clientIndex

int clientIndex;

Protected Attributes Documentation

variable minPlayers

int minPlayers = 1;

variable roomPlayerPrefab

NetworkRoomPlayer roomPlayerPrefab;

Updated on 18 January 2021 at 03:17:52 UTC