docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class NetworkLobbyManager

    This is a specialized NetworkManager that includes a networked lobby.

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

    NetworkLobbyManager 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 NetworkLobbyManager, there are new virtual functions on the NetworkLobbyManager that begin with "OnLobby". These should be used on classes derived from NetworkLobbyManager instead of the virtual functions on NetworkManager.

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

    Inheritance
    object
    Object
    Component
    Behaviour
    MonoBehaviour
    NetworkManager
    NetworkLobbyManager
    Inherited Members
    NetworkManager.matchName
    NetworkManager.matchSize
    NetworkManager.networkPort
    NetworkManager.serverBindToIP
    NetworkManager.serverBindAddress
    NetworkManager.networkAddress
    NetworkManager.dontDestroyOnLoad
    NetworkManager.runInBackground
    NetworkManager.scriptCRCCheck
    NetworkManager.sendPeerInfo
    NetworkManager.maxDelay
    NetworkManager.logLevel
    NetworkManager.playerPrefab
    NetworkManager.autoCreatePlayer
    NetworkManager.playerSpawnMethod
    NetworkManager.offlineScene
    NetworkManager.onlineScene
    NetworkManager.spawnPrefabs
    NetworkManager.startPositions
    NetworkManager.customConfig
    NetworkManager.connectionConfig
    NetworkManager.globalConfig
    NetworkManager.maxConnections
    NetworkManager.channels
    NetworkManager.secureTunnelEndpoint
    NetworkManager.useWebSockets
    NetworkManager.useSimulator
    NetworkManager.simulatedLatency
    NetworkManager.packetLossPercentage
    NetworkManager.matchHost
    NetworkManager.matchPort
    NetworkManager.clientLoadedScene
    NetworkManager.migrationManager
    NetworkManager.numPlayers
    NetworkManager.defaultTransport
    NetworkManager.activeTransport
    NetworkManager.networkSceneName
    NetworkManager.isNetworkActive
    NetworkManager.client
    NetworkManager.matchInfo
    NetworkManager.matchMaker
    NetworkManager.matches
    NetworkManager.singleton
    NetworkManager.SetupMigrationManager(NetworkMigrationManager)
    NetworkManager.StartServer(ConnectionConfig, int)
    NetworkManager.StartServer()
    NetworkManager.StartServer(MatchInfo)
    NetworkManager.UseExternalClient(NetworkClient)
    NetworkManager.StartClient(MatchInfo, ConnectionConfig, int)
    NetworkManager.StartClient(MatchInfo)
    NetworkManager.StartClient()
    NetworkManager.StartClient(MatchInfo, ConnectionConfig)
    NetworkManager.StartHost(ConnectionConfig, int)
    NetworkManager.StartHost(MatchInfo)
    NetworkManager.StartHost()
    NetworkManager.StopHost()
    NetworkManager.StopServer()
    NetworkManager.StopClient()
    NetworkManager.RegisterStartPosition(Transform)
    NetworkManager.UnRegisterStartPosition(Transform)
    NetworkManager.IsClientConnected()
    NetworkManager.Shutdown()
    NetworkManager.OnServerReady(NetworkConnection)
    NetworkManager.OnServerAddPlayer(NetworkConnection, short, NetworkReader)
    NetworkManager.GetStartPosition()
    NetworkManager.OnServerError(NetworkConnection, int)
    NetworkManager.OnClientError(NetworkConnection, int)
    NetworkManager.OnClientNotReady(NetworkConnection)
    NetworkManager.StartMatchMaker()
    NetworkManager.StopMatchMaker()
    NetworkManager.SetMatchHost(string, int, bool)
    NetworkManager.OnStopServer()
    NetworkManager.OnMatchCreate(bool, string, MatchInfo)
    NetworkManager.OnMatchList(bool, string, List<MatchInfoSnapshot>)
    NetworkManager.OnMatchJoined(bool, string, MatchInfo)
    NetworkManager.OnDestroyMatch(bool, string)
    NetworkManager.OnDropConnection(bool, string)
    NetworkManager.OnSetMatchAttributes(bool, string)
    MonoBehaviour.IsInvoking()
    MonoBehaviour.CancelInvoke()
    MonoBehaviour.Invoke(string, float)
    MonoBehaviour.InvokeRepeating(string, float, float)
    MonoBehaviour.CancelInvoke(string)
    MonoBehaviour.IsInvoking(string)
    MonoBehaviour.StartCoroutine(string)
    MonoBehaviour.StartCoroutine(string, object)
    MonoBehaviour.StartCoroutine(IEnumerator)
    MonoBehaviour.StartCoroutine_Auto(IEnumerator)
    MonoBehaviour.StopCoroutine(IEnumerator)
    MonoBehaviour.StopCoroutine(Coroutine)
    MonoBehaviour.StopCoroutine(string)
    MonoBehaviour.StopAllCoroutines()
    MonoBehaviour.print(object)
    MonoBehaviour.useGUILayout
    MonoBehaviour.runInEditMode
    Behaviour.enabled
    Behaviour.isActiveAndEnabled
    Component.GetComponent(Type)
    Component.GetComponent<T>()
    Component.TryGetComponent(Type, out Component)
    Component.TryGetComponent<T>(out T)
    Component.GetComponent(string)
    Component.GetComponentInChildren(Type, bool)
    Component.GetComponentInChildren(Type)
    Component.GetComponentInChildren<T>(bool)
    Component.GetComponentInChildren<T>()
    Component.GetComponentsInChildren(Type, bool)
    Component.GetComponentsInChildren(Type)
    Component.GetComponentsInChildren<T>(bool)
    Component.GetComponentsInChildren<T>(bool, List<T>)
    Component.GetComponentsInChildren<T>()
    Component.GetComponentsInChildren<T>(List<T>)
    Component.GetComponentInParent(Type)
    Component.GetComponentInParent<T>()
    Component.GetComponentsInParent(Type, bool)
    Component.GetComponentsInParent(Type)
    Component.GetComponentsInParent<T>(bool)
    Component.GetComponentsInParent<T>(bool, List<T>)
    Component.GetComponentsInParent<T>()
    Component.GetComponents(Type)
    Component.GetComponents(Type, List<Component>)
    Component.GetComponents<T>(List<T>)
    Component.GetComponents<T>()
    Component.CompareTag(string)
    Component.SendMessageUpwards(string, object, SendMessageOptions)
    Component.SendMessageUpwards(string, object)
    Component.SendMessageUpwards(string)
    Component.SendMessageUpwards(string, SendMessageOptions)
    Component.SendMessage(string, object)
    Component.SendMessage(string)
    Component.SendMessage(string, object, SendMessageOptions)
    Component.SendMessage(string, SendMessageOptions)
    Component.BroadcastMessage(string, object, SendMessageOptions)
    Component.BroadcastMessage(string, object)
    Component.BroadcastMessage(string)
    Component.BroadcastMessage(string, SendMessageOptions)
    Component.transform
    Component.gameObject
    Component.tag
    Object.GetInstanceID()
    Object.GetHashCode()
    Object.Equals(object)
    Object.Instantiate(Object, Vector3, Quaternion)
    Object.Instantiate(Object, Vector3, Quaternion, Transform)
    Object.Instantiate(Object)
    Object.Instantiate(Object, Transform)
    Object.Instantiate(Object, Transform, bool)
    Object.Instantiate<T>(T)
    Object.Instantiate<T>(T, Vector3, Quaternion)
    Object.Instantiate<T>(T, Vector3, Quaternion, Transform)
    Object.Instantiate<T>(T, Transform)
    Object.Instantiate<T>(T, Transform, bool)
    Object.Destroy(Object, float)
    Object.Destroy(Object)
    Object.DestroyImmediate(Object, bool)
    Object.DestroyImmediate(Object)
    Object.FindObjectsOfType(Type)
    Object.DontDestroyOnLoad(Object)
    Object.DestroyObject(Object, float)
    Object.DestroyObject(Object)
    Object.FindSceneObjectsOfType(Type)
    Object.FindObjectsOfTypeIncludingAssets(Type)
    Object.FindObjectsOfType<T>()
    Object.FindObjectOfType<T>()
    Object.FindObjectsOfTypeAll(Type)
    Object.FindObjectOfType(Type)
    Object.ToString()
    Object.name
    Object.hideFlags
    object.Equals(object, object)
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    Namespace: UnityEngine.Networking
    Assembly: com.unity.multiplayer-hlapi.Runtime.dll
    Syntax
    [AddComponentMenu("Network/NetworkLobbyManager")]
    [Obsolete("The high level API classes are deprecated and will be removed in the future.")]
    public class NetworkLobbyManager : NetworkManager

    Fields

    lobbySlots

    These slots track players that enter the lobby.

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

    Declaration
    public NetworkLobbyPlayer[] lobbySlots
    Field Value
    Type Description
    NetworkLobbyPlayer[]

    Properties

    gamePlayerPrefab

    This is the prefab of the player to be created in the PlayScene.

    When CheckReadyToBegin starts the game from the lobby, a new player object is created from this prefab, and that object is made the active player object using NetworkServer.ReplacePlayerForConnection.

    Declaration
    public GameObject gamePlayerPrefab { get; set; }
    Property Value
    Type Description
    GameObject

    lobbyPlayerPrefab

    This is the prefab of the player to be created in the LobbyScene.

    This prefab must have a NetworkLobbyPlayer component on it.

    In the lobby scene, this will be the active player object, but in other scenes while the game is running, this will be replaced by a player object created from the GamePlayerPrefab. But once returned to the lobby scene this will again become the active player object.

    This can be used to store user data that persists for the lifetime of the session, such as color choices or weapon choices.

    Declaration
    public NetworkLobbyPlayer lobbyPlayerPrefab { get; set; }
    Property Value
    Type Description
    NetworkLobbyPlayer

    lobbyScene

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

    Declaration
    public string lobbyScene { get; set; }
    Property Value
    Type Description
    string

    maxPlayers

    The maximum number of players allowed in the game.

    Note that this is the number "players" not clients or connections. There can be multiple players per client.

    Declaration
    public int maxPlayers { get; set; }
    Property Value
    Type Description
    int

    maxPlayersPerConnection

    The maximum number of players per connection.

    Calling ClientScene.AddPlayer will fail if this limit is reached.

    Declaration
    public int maxPlayersPerConnection { get; set; }
    Property Value
    Type Description
    int

    minPlayers

    The minimum number of players required to be ready for the game to start.

    If this is zero then the game can start with any number of players.

    Declaration
    public int minPlayers { get; set; }
    Property Value
    Type Description
    int

    playScene

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

    Declaration
    public string playScene { get; set; }
    Property Value
    Type Description
    string

    showLobbyGUI

    This flag enables display of the default lobby UI.

    This is rendered using the old GUI system, so is only recommended for testing purposes.

    Declaration
    public bool showLobbyGUI { get; set; }
    Property Value
    Type Description
    bool

    Methods

    CheckReadyToBegin()

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

    If all of the players are ready, then the server switches from the LobbyScene to the PlayScene - essentially starting the game. This is called automatically in response to NetworkLobbyPlayer.SendReadyToBeginMessage().

    Declaration
    public void CheckReadyToBegin()

    OnClientConnect(NetworkConnection)

    Called on the client when connected to a server.

    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.

    //Attach this script to a GameObject
                                                                                                                                                                          //Create a Text GameObject(Create>UI>Text) and attach it to the Text field in the Inspector window
                                                                                                                                                                          //This script changes the Text depending on if a client connects or disconnects to the server
    
                                                                                                                                                                      using UnityEngine;
                                                                                                                                                                      using UnityEngine.Networking;
                                                                                                                                                                      using UnityEngine.UI;
    
                                                                                                                                                                      public class Example : NetworkManager
                                                                                                                                                                      {
                                                                                                                                                                         //Assign a Text component in the GameObject's Inspector
                                                                                                                                                                         public Text m_ClientText;
    
                                                                                                                                                                         //Detect when a client connects to the Server
                                                                                                                                                                         public override void OnClientConnect(NetworkConnection connection)
                                                                                                                                                                         {
                                                                                                                                                                             //Change the text to show the connection on the client side
                                                                                                                                                                             m_ClientText.text =  " " + connection.connectionId + " Connected!";
                                                                                                                                                                         }
    
                                                                                                                                                                         //Detect when a client connects to the Server
                                                                                                                                                                         public override void OnClientDisconnect(NetworkConnection connection)
                                                                                                                                                                         {
                                                                                                                                                                             //Change the text to show the connection loss on the client side
                                                                                                                                                                             m_ClientText.text = "Connection" + connection.connectionId + " Lost!";
                                                                                                                                                                         }
                                                                                                                                                                      }</code></pre>
    
    Declaration
    public override void OnClientConnect(NetworkConnection conn)
    Parameters
    Type Name Description
    NetworkConnection conn

    Connection to the server.

    Overrides
    NetworkManager.OnClientConnect(NetworkConnection)

    OnClientDisconnect(NetworkConnection)

    Called on clients when disconnected from a server.

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

    //Attach this script to a GameObject
                                                                                                                                                       //Create a Text GameObject(Create>UI>Text) and attach it to the Text field in the Inspector window
                                                                                                                                                       //This script changes the Text depending on if a client connects or disconnects to the server
    
                                                                                                                                                   using UnityEngine;
                                                                                                                                                   using UnityEngine.Networking;
                                                                                                                                                   using UnityEngine.UI;
    
                                                                                                                                                   public class OnClientConnectExample : NetworkManager
                                                                                                                                                   {
                                                                                                                                                      //Assign a Text component in the GameObject's Inspector
                                                                                                                                                      public Text m_ClientText;
    
                                                                                                                                                      //Detect when a client connects to the Server
                                                                                                                                                      public override void OnClientConnect(NetworkConnection connection)
                                                                                                                                                      {
                                                                                                                                                          //Change the text to show the connection on the client side
                                                                                                                                                          m_ClientText.text =  " " + connection.connectionId + " Connected!";
                                                                                                                                                      }
    
                                                                                                                                                      //Detect when a client connects to the Server
                                                                                                                                                      public override void OnClientDisconnect(NetworkConnection connection)
                                                                                                                                                      {
                                                                                                                                                          //Change the text to show the connection loss on the client side
                                                                                                                                                          m_ClientText.text = "Connection" + connection.connectionId + " Lost!";
                                                                                                                                                      }
                                                                                                                                                   }</code></pre>
    
    Declaration
    public override void OnClientDisconnect(NetworkConnection conn)
    Parameters
    Type Name Description
    NetworkConnection conn

    Connection to the server.

    Overrides
    NetworkManager.OnClientDisconnect(NetworkConnection)

    OnClientSceneChanged(NetworkConnection)

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

    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.

    Declaration
    public override void OnClientSceneChanged(NetworkConnection conn)
    Parameters
    Type Name Description
    NetworkConnection conn

    The network connection that the scene change message arrived on.

    Overrides
    NetworkManager.OnClientSceneChanged(NetworkConnection)

    OnLobbyClientAddPlayerFailed()

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

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

    Declaration
    public virtual void OnLobbyClientAddPlayerFailed()

    OnLobbyClientConnect(NetworkConnection)

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

    Declaration
    public virtual void OnLobbyClientConnect(NetworkConnection conn)
    Parameters
    Type Name Description
    NetworkConnection conn

    The connection that connected.

    OnLobbyClientDisconnect(NetworkConnection)

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

    Declaration
    public virtual void OnLobbyClientDisconnect(NetworkConnection conn)
    Parameters
    Type Name Description
    NetworkConnection conn

    The connection that disconnected.

    OnLobbyClientEnter()

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

    Declaration
    public virtual void OnLobbyClientEnter()

    OnLobbyClientExit()

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

    Declaration
    public virtual void OnLobbyClientExit()

    OnLobbyClientSceneChanged(NetworkConnection)

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

    Declaration
    public virtual void OnLobbyClientSceneChanged(NetworkConnection conn)
    Parameters
    Type Name Description
    NetworkConnection conn

    The connection that finished loading a new networked scene.

    OnLobbyServerConnect(NetworkConnection)

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

    Declaration
    public virtual void OnLobbyServerConnect(NetworkConnection conn)
    Parameters
    Type Name Description
    NetworkConnection conn

    The new connection.

    OnLobbyServerCreateGamePlayer(NetworkConnection, short)

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

    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 lobby-player on the connection.

    Declaration
    public virtual GameObject OnLobbyServerCreateGamePlayer(NetworkConnection conn, short playerControllerId)
    Parameters
    Type Name Description
    NetworkConnection conn

    The connection the player object is for.

    short playerControllerId

    The controllerId of the player on the connnection.

    Returns
    Type Description
    GameObject

    A new GamePlayer object.

    OnLobbyServerCreateLobbyPlayer(NetworkConnection, short)

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

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

    Declaration
    public virtual GameObject OnLobbyServerCreateLobbyPlayer(NetworkConnection conn, short playerControllerId)
    Parameters
    Type Name Description
    NetworkConnection conn

    The connection the player object is for.

    short playerControllerId

    The controllerId of the player.

    Returns
    Type Description
    GameObject

    The new lobby-player object.

    OnLobbyServerDisconnect(NetworkConnection)

    This is called on the server when a client disconnects.

    Declaration
    public virtual void OnLobbyServerDisconnect(NetworkConnection conn)
    Parameters
    Type Name Description
    NetworkConnection conn

    The connection that disconnected.

    OnLobbyServerPlayerRemoved(NetworkConnection, short)

    This is called on the server when a player is removed.

    Declaration
    public virtual void OnLobbyServerPlayerRemoved(NetworkConnection conn, short playerControllerId)
    Parameters
    Type Name Description
    NetworkConnection conn

    The connection the player object is for.

    short playerControllerId

    The controllerId of the player that was removed.

    OnLobbyServerPlayersReady()

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

    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 lobby are ready, such as adding a countdown or a confirmation for a group leader.

    using UnityEngine;
    using UnityEngine.Networking;
    

    public class GuiLobby : NetworkLobbyManager { float countTimer = 0;

    public override void OnLobbyServerPlayersReady() { countTimer = Time.time + 5; }

    void Update() { if (countTimer == 0) return; if (Time.time > countTimer) { countTimer = 0; ServerChangeScene(playScene); } else { Debug.Log("Counting down " + (countTimer - Time.time)); } } }

    Declaration
    public virtual void OnLobbyServerPlayersReady()

    OnLobbyServerSceneChanged(string)

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

    Declaration
    public virtual void OnLobbyServerSceneChanged(string sceneName)
    Parameters
    Type Name Description
    string sceneName

    Name of the new scene.

    OnLobbyServerSceneLoadedForPlayer(GameObject, GameObject)

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

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

    Declaration
    public virtual bool OnLobbyServerSceneLoadedForPlayer(GameObject lobbyPlayer, GameObject gamePlayer)
    Parameters
    Type Name Description
    GameObject lobbyPlayer

    The lobby player object.

    GameObject gamePlayer

    The game player object.

    Returns
    Type Description
    bool

    False to not allow this player to replace the lobby player.

    OnLobbyStartClient(NetworkClient)

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

    using UnityEngine;
    using UnityEngine.Networking;
    using UnityEngine.UI;
    
    public class Example : NetworkLobbyManager
    {
       //Add this script to your GameObject. Make sure there isn't another NetworkManager in the Scene.
       //Create 2 Buttons (Create>UI>Text) and either:
       //1. assign them in the Inspector of the GameObject this script is attached to or
       //2. remove this part and the listeners and alter the OnClick section on each Button to match up with each function
       //Create a Text GameObject (Create>UI>Text) and attach it to the Status Text field in the Inspector.
    
       public Button m_ClientButton, m_ServerButton;
       bool m_ServerStarted, m_ClientStarted;
    
       void Start()
       {
           showLobbyGUI = true;
           //Call these functions when each Button is clicked
           m_ServerButton.onClick.AddListener(ServerButton);
           m_ClientButton.onClick.AddListener(ClientButton);
       }
    
       //Output a message when your client enters the lobby
       public override void OnLobbyClientEnter()
       {
           m_ClientStarted = true;
           base.OnLobbyClientEnter();
           Debug.Log("Your client has entered the lobby!");
       }
    
       public override void OnLobbyStopClient()
       {
           Debug.Log("Client stopped");
           base.OnLobbyStopClient();
       }
    
       public override void OnLobbyStartServer()
       {
           m_ServerStarted = true;
           base.OnLobbyStartServer();
           Debug.Log("Server Started!");
       }
    
       public override void OnStopServer()
       {
           m_ServerStarted = false;
           base.OnStopServer();
           Debug.Log("Server Stopped!");
       }
    
       //Start the Client when this Button is pressed
       public void ClientButton()
       {
           if (m_ClientStarted == false)
           {
               StartClient();
               m_ClientButton.GetComponentInChildren<Text>().text = "Stop Client";
           }
           else
           {
               StopClient();
               m_ClientButton.GetComponentInChildren<Text>().text = "Start Client";
           }
       }
    
       //Start the Server when this Button is pressed
       public void ServerButton()
       {
           Debug.Log("Server : " + m_ServerStarted);
           if (m_ServerStarted == false)
           {
               StartServer();
               m_ServerButton.GetComponentInChildren<Text>().text = "Stop Server";
           }
           else
           {
               StopServer();
               ServerReturnToLobby();
               m_ServerButton.GetComponentInChildren<Text>().text = "Start Server";
           }
       }
    }
    Declaration
    public virtual void OnLobbyStartClient(NetworkClient lobbyClient)
    Parameters
    Type Name Description
    NetworkClient lobbyClient

    The connection for the lobby.

    OnLobbyStartHost()

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

    //This script shows you how to add extra functionality when the lobby host starts and stops
    //Add this script to your GameObject. Make sure there isn't another NetworkManager in the Scene.
    //Create a Host Button (Create>UI>Text) and assign it in the Inspector of the GameObject this script is attached to
    //Create a Text GameObject (Create>UI>Text) and attach it to the Status Text field in the Inspector.
    
    using UnityEngine;
    using UnityEngine.Networking;
    using UnityEngine.UI;
    
    public class Example : NetworkLobbyManager
    {
       public Button m_HostButton;
       public Text m_StatusText;
       bool m_HostStarted;
    
       void Start()
       {
           //Set the minimum and maximum number of players
           maxPlayers = 6;
           minPlayers = 2;
           maxPlayersPerConnection = 1;
           //Call these functions when each Button is clicked
           m_HostButton.onClick.AddListener(HostButton);
           m_StatusText.text = "Current Scene : " + lobbyScene;
       }
    
       //Output a message when the host joins the lobby
       public override void OnLobbyStartHost()
       {
           //Change the Text to show this message
           m_StatusText.text = "A Host has joined the lobby!";
           m_HostStarted = true;
           //Do the default actions for when the host starts in the lobby
           base.OnLobbyStartHost();
       }
    
       // Output a message to the host when the host stops
       public override void OnLobbyStopHost()
       {
           //Output this message when the host stops
           m_StatusText.text = "A Host has left the lobby!";
           //Do the default actions when the host stops
           base.OnLobbyStopHost();
           m_HostStarted = false;
       }
    
       /// This is where the Buttons are given functionality
       //Start the host when this Button is pressed
       public void HostButton()
       {
           //Check if the host has already started
           if (m_HostStarted == false)
           {
               //Start the host
               StartHost();
               //Change the Button's Text
               m_HostButton.GetComponentInChildren<Text>().text = "Stop Host";
           }
           else
           {
               //If the host has already started, stop the host
               StopHost();
               //Change the Button's Text
               m_HostButton.GetComponentInChildren<Text>().text = "Start Host";
           }
       }
    }
    Declaration
    public virtual void OnLobbyStartHost()

    OnLobbyStartServer()

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

    using UnityEngine;
    using UnityEngine.Networking;
    using UnityEngine.UI;
    
    public class Example : NetworkLobbyManager
    {
       //Add this script to your GameObject. Make sure there isn't another NetworkManager in the Scene.
       //Create 2 Buttons (Create>UI>Text) and either:
       //1. assign them in the Inspector of the GameObject this script is attached to or
       //2. remove this part and the listeners and alter the OnClick section on each Button to match up with each function
       //Create a Text GameObject (Create>UI>Text) and attach it to the Status Text field in the Inspector.
    
       public Button m_ClientButton, m_ServerButton;
       bool m_ServerStarted, m_ClientStarted;
    
       void Start()
       {
           showLobbyGUI = true;
           //Call these functions when each Button is clicked
           m_ServerButton.onClick.AddListener(ServerButton);
           m_ClientButton.onClick.AddListener(ClientButton);
       }
    
       //Output a message when your client enters the lobby
       public override void OnLobbyClientEnter()
       {
           m_ClientStarted = true;
           base.OnLobbyClientEnter();
           Debug.Log("Your client has entered the lobby!");
       }
    
       public override void OnLobbyStopClient()
       {
           Debug.Log("Client stopped");
           base.OnLobbyStopClient();
       }
    
       public override void OnLobbyStartServer()
       {
           m_ServerStarted = true;
           base.OnLobbyStartServer();
           Debug.Log("Server Started!");
       }
    
       public override void OnStopServer()
       {
           m_ServerStarted = false;
           base.OnStopServer();
           Debug.Log("Server Stopped!");
       }
    
       //Start the Client when this Button is pressed
       public void ClientButton()
       {
           if (m_ClientStarted == false)
           {
               StartClient();
               m_ClientButton.GetComponentInChildren<Text>().text = "Stop Client";
           }
           else
           {
               StopClient();
               m_ClientButton.GetComponentInChildren<Text>().text = "Start Client";
           }
       }
    
       //Start the Server when this Button is pressed
       public void ServerButton()
       {
           Debug.Log("Server : " + m_ServerStarted);
           if (m_ServerStarted == false)
           {
               StartServer();
               m_ServerButton.GetComponentInChildren<Text>().text = "Stop Server";
           }
           else
           {
               StopServer();
               ServerReturnToLobby();
               m_ServerButton.GetComponentInChildren<Text>().text = "Start Server";
           }
       }
    }
    Declaration
    public virtual void OnLobbyStartServer()

    OnLobbyStopClient()

    This is called on the client when the client stops.

    Declaration
    public virtual void OnLobbyStopClient()

    OnLobbyStopHost()

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

    Declaration
    public virtual void OnLobbyStopHost()

    OnServerAddPlayer(NetworkConnection, short)

    Declaration
    public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    Parameters
    Type Name Description
    NetworkConnection conn
    short playerControllerId
    Overrides
    NetworkManager.OnServerAddPlayer(NetworkConnection, short)

    OnServerConnect(NetworkConnection)

    Called on the server when a new client connects.

    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.

    //Attach this script to a GameObject and add a NetworkHUD component to the GameObject.
                                                                                                                                                                           //Create a Text GameObject (Create>UI>Text) and attach it in the Text field in the Inspector.
                                                                                                                                                                           //This script changes Text on the screen when a client connects to the server
    
                                                                                                                                                                       using UnityEngine;
                                                                                                                                                                       using UnityEngine.Networking;
                                                                                                                                                                       using UnityEngine.UI;
    
                                                                                                                                                                       public class OnServerConnectExample : NetworkManager
                                                                                                                                                                       {
                                                                                                                                                                          //Assign a Text component in the GameObject's Inspector
                                                                                                                                                                          public Text m_Text;
    
                                                                                                                                                                          //Detect when a client connects to the Server
                                                                                                                                                                          public override void OnServerConnect(NetworkConnection connection)
                                                                                                                                                                          {
                                                                                                                                                                              //Change the text to show the connection and the client's ID
                                                                                                                                                                              m_Text.text = "Client " + connection.connectionId + " Connected!";
                                                                                                                                                                          }
                                                                                                                                                                       }</code></pre>
    
    Declaration
    public override void OnServerConnect(NetworkConnection conn)
    Parameters
    Type Name Description
    NetworkConnection conn

    Connection from client.

    Overrides
    NetworkManager.OnServerConnect(NetworkConnection)

    OnServerDisconnect(NetworkConnection)

    Called on the server when a client disconnects.

    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.

    //This script outputs a message when a client connects or disconnects from the server
                                                                                                                                                                 //Attach this script to your GameObject.
                                                                                                                                                                 //Attach a NetworkManagerHUD to your by clicking Add Component in the Inspector window of the GameObject. Then go to Network>NetworkManagerHUD.
                                                                                                                                                                 //Create a Text GameObject and attach it to the Text field in the Inspector.
    
                                                                                                                                                             using UnityEngine;
                                                                                                                                                             using UnityEngine.Networking;
                                                                                                                                                             using UnityEngine.UI;
    
                                                                                                                                                             public class Example : NetworkManager
                                                                                                                                                             {
                                                                                                                                                                //Assign a Text component in the GameObject's Inspector
                                                                                                                                                                public Text m_Text;
    
                                                                                                                                                                //Detect when a client connects to the Server
                                                                                                                                                                public override void OnServerConnect(NetworkConnection connection)
                                                                                                                                                                {
                                                                                                                                                                    //Change the text to show the connection
                                                                                                                                                                    m_Text.text = "Client " + connection.connectionId + " Connected!";
                                                                                                                                                                }
    
                                                                                                                                                                //Detect when a client disconnects from the Server
                                                                                                                                                                public override void OnServerDisconnect(NetworkConnection connection)
                                                                                                                                                                {
                                                                                                                                                                    //Change the text to show the loss of connection
                                                                                                                                                                    m_Text.text = "Client " + connection.connectionId + "Connection Lost!";
                                                                                                                                                                }
                                                                                                                                                             }</code></pre>
    
    Declaration
    public override void OnServerDisconnect(NetworkConnection conn)
    Parameters
    Type Name Description
    NetworkConnection conn

    Connection from client.

    Overrides
    NetworkManager.OnServerDisconnect(NetworkConnection)

    OnServerRemovePlayer(NetworkConnection, PlayerController)

    Called on the server when a client removes a player.

    The default implementation of this function destroys the corresponding player object.

    Declaration
    public override void OnServerRemovePlayer(NetworkConnection conn, PlayerController player)
    Parameters
    Type Name Description
    NetworkConnection conn

    The connection to remove the player from.

    PlayerController player

    The player controller to remove.

    Overrides
    NetworkManager.OnServerRemovePlayer(NetworkConnection, PlayerController)

    OnServerSceneChanged(string)

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

    Declaration
    public override void OnServerSceneChanged(string sceneName)
    Parameters
    Type Name Description
    string sceneName

    The name of the new scene.

    Overrides
    NetworkManager.OnServerSceneChanged(string)

    OnStartClient(NetworkClient)

    This is a hook that is invoked when the client is started.

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

    Declaration
    public override void OnStartClient(NetworkClient lobbyClient)
    Parameters
    Type Name Description
    NetworkClient lobbyClient
    Overrides
    NetworkManager.OnStartClient(NetworkClient)

    OnStartHost()

    This hook is invoked when a host is started.

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

    Declaration
    public override void OnStartHost()
    Overrides
    NetworkManager.OnStartHost()

    OnStartServer()

    This hook is invoked when a server is started - including when a host is started. StartServer has multiple signatures, but they all cause this hook to be called.

    Declaration
    public override void OnStartServer()
    Overrides
    NetworkManager.OnStartServer()

    OnStopClient()

    This hook is called when a client is stopped.

    Declaration
    public override void OnStopClient()
    Overrides
    NetworkManager.OnStopClient()

    OnStopHost()

    This hook is called when a host is stopped.

    Declaration
    public override void OnStopHost()
    Overrides
    NetworkManager.OnStopHost()

    SendReturnToLobby()

    Sends a message to the server to make the game return to the lobby scene.

    Declaration
    public bool SendReturnToLobby()
    Returns
    Type Description
    bool

    True if message was sent.

    ServerChangeScene(string)

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

    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.

    Declaration
    public override void ServerChangeScene(string sceneName)
    Parameters
    Type Name Description
    string sceneName
    Overrides
    NetworkManager.ServerChangeScene(string)

    ServerReturnToLobby()

    Calling this causes the server to switch back to the lobby scene.

    Declaration
    public void ServerReturnToLobby()

    TryToAddPlayer()

    This is used on clients to attempt to add a player to the game.

    This may fail if the game is full or the connection cannot have more players.

    Declaration
    public void TryToAddPlayer()
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)