0
0
Unityframework~7 mins

Lobby and matchmaking basics in Unity

Choose your learning style9 modes available
Introduction

A lobby lets players gather before a game starts. Matchmaking helps find players to play together or against each other.

When you want players to join a waiting room before a multiplayer game.
When you want to connect players with similar skill levels automatically.
When you want to create teams or groups for a game session.
When you want to manage who joins a game and when it starts.
When you want to let players invite friends to play together.
Syntax
Unity
using UnityEngine;
using UnityEngine.Networking;

public class LobbyManager : NetworkLobbyManager
{
    // Override methods to customize lobby behavior
    public override void OnLobbyClientEnter()
    {
        Debug.Log("Player joined the lobby");
    }

    public override GameObject OnLobbyServerCreateLobbyPlayer(NetworkConnection conn, short playerControllerId)
    {
        Debug.Log("Creating lobby player");
        return base.OnLobbyServerCreateLobbyPlayer(conn, playerControllerId);
    }
}

Unity uses NetworkLobbyManager to handle lobby and matchmaking basics.

You can override methods to customize what happens when players join or leave.

Examples
This runs when a player joins the lobby and prints a message.
Unity
public override void OnLobbyClientEnter()
{
    Debug.Log("A new player has entered the lobby.");
}
This runs when the server creates a player in the lobby.
Unity
public override GameObject OnLobbyServerCreateLobbyPlayer(NetworkConnection conn, short playerControllerId)
{
    Debug.Log("Lobby player created.");
    return base.OnLobbyServerCreateLobbyPlayer(conn, playerControllerId);
}
Sample Program

This simple lobby manager logs when players join and starts the game when all are ready.

Unity
using UnityEngine;
using UnityEngine.Networking;

public class SimpleLobby : NetworkLobbyManager
{
    public override void OnLobbyClientEnter()
    {
        Debug.Log($"Player connected: {NetworkClient.connection.connectionId}");
    }

    public override void OnLobbyServerPlayersReady()
    {
        Debug.Log("All players are ready. Starting game...");
        ServerChangeScene(playScene);
    }
}
OutputSuccess
Important Notes

Matchmaking often requires a server or service to connect players.

Unity's built-in networking is deprecated; consider using newer solutions like Mirror or Unity Relay.

Always test lobbies with multiple players to ensure smooth joining and starting.

Summary

Lobbies let players wait and prepare before a game.

Matchmaking finds and connects players automatically.

Override lobby methods to customize player joining and game start.