MultiConnectMultiConnectDocs Website home
MultiConnectSDK › Developer Guide

2. Quick Start

This takes one Unity project with a server scene and a client scene to a live server. You need a MultiConnectCloud account, and creating one is free. Some steps happen in Unity and some on the dashboard in your browser; each dashboard step links to its page.

1. Install MultiConnectSDK

You need Unity 2021.2 or later. Download the latest package from multiconnectcloud.com/download-sdks and import it with Assets > Import Package > Custom Package. To update later, use MultiConnect > Check For Update. To reopen this guide, use MultiConnect > Open Developer Guide.

2. Get your free SDK key

Sign up or log in; MultiConnect > Get Started in Unity takes you there too. On the dashboard, open Game Analytics and choose Create SDK app. Pick a project, or create one there. Copy the key from the app's Setup tab. The key is free. Remember the project: your deployment goes in the same one.

The key is public by design, because it ships inside every build of your game. It can only find your servers and send analytics. It cannot deploy, spend credits or read your account.

3. Create your two scenes

A multiplayer game is two separate programs, and in Unity each one starts from its own scene. Both scenes live in the same Unity project.

Scene Who runs it What it does
ServerScene MultiConnectCloud, on its servers Matches players into rooms and runs your game rules
ClientScene Your players, on their devices Shows the game and sends what the player does

Create both now. In Unity, choose File > New Scene and save it as Assets/Scenes/ServerScene.unity, then do the same for Assets/Scenes/ClientScene.unity. Steps 5 and 6 fill them in.

4. Define the messages both scenes use

The client and the server talk by sending messages. A message is a small class holding the data, plus a number that says what kind of message it is. Both sides must use exactly the same definition, or they cannot read each other's messages.

A script does not belong to a scene unless you attach it to a GameObject, so one file can be used by both:

  1. Create Assets/Scripts/GameMessages.cs, and paste in Script 2.1.
  2. Leave it unattached. Do not add it to any GameObject.

The server uses it in Script 2.2, and the client uses it in Script 2.3. Add your game's own messages to this file as you need them.

Script 2.1: GameMessages.cs

using MultiConnect.Serialization;

public enum GameMessage { PlayerMove = 1 }

[McSerializationObject]
public class PlayerMoveData
{
    public string playerId;
    public float x, y, z;
}

5. Set up the server scene

Open ServerScene. Add an empty GameObject, add the MultiConnectGameServer component to it, and set:

Field Value
Sdk Key Your key
Use Custom Hosting Off
Use As Master Server On (one server does matchmaking and gameplay)
Local Port 7777

Then create Assets/Scripts/ServerBootstrap.cs, paste in Script 2.2, and add it to the same GameObject. It tells the server how to make rooms.

You never start the server yourself: with Use Custom Hosting off, it starts by itself on the port MultiConnectCloud gives it, or on Local Port on your machine.

Script 2.2: ServerBootstrap.cs

using System.Collections.Generic;
using MultiConnect.Global;
using MultiConnect.Server;
using MultiConnect;
using UnityEngine;

public class ServerBootstrap : MonoBehaviour, IRoomController
{
    // Awake, so it is set before the first player can ask for a room.
    void Awake() => MatchMaking.SetRoomController(this);

    public IRoom GetRoom(Dictionary<string, string> parameters) => new MyRoom();
    public IRoom GetRoom(string parameterClass) => new MyRoom();
}

public class MyRoom : IRoom
{
    private Room room;

    public void OnRoomCreated(Room room)
    {
        this.room = room;
        room.AddResponseListener(GameMessage.PlayerMove, OnPlayerMove);
    }

    void OnPlayerMove(object message)
    {
        // Check the message here, then pass it on to everyone in the room.
        room.SendMcBinaryToAll((PlayerMoveData)message, GameMessage.PlayerMove, McProtocol.Unreliable);
    }

    public void OnRoomJoined(string playerId, string playerData = "") { }
    public void OnLeaveRoom(string playerId, string msg = "") { }
    public void OnRejoinRoom(string playerId) { }
    public void OnPlayerDisconnected(string playerId) { }
    public void OnDestroyRoom() => room.RemoveAllListener();
}

6. Set up the client scene

Open ClientScene. Add an empty GameObject, and add the MultiConnectClient component to it, with the same Sdk Key and Use Custom Hosting off.

Then create Assets/Scripts/GameClient.cs, paste in Script 2.3, and add it to the same GameObject. It connects to the server and joins a room.

Script 2.3: GameClient.cs

using System.Collections.Generic;
using MultiConnect;
using MultiConnect.Client;
using MultiConnect.Global;
using UnityEngine;

public class GameClient : MonoBehaviour
{
    private Room room;

    void Start()
    {
        Client.onMasterServerConnected += isReconnect =>
        {
            if (!isReconnect) GameRooms.FindRoom(new Dictionary<string, string>(), maxPlayers: 4, minPlayers: 2);
        };
        GameRooms.OnRoomJoined += joined => room = joined;

        if (MultiConnectInfo.IsMultiConnectInitialized()) Connect();
        else MultiConnectInfo.onMultiConnectInitialized += Connect;
    }

    // MultiConnectCloud finds your server from the SDK key. Pass your own player id, or "" for a generated one.
    async void Connect() => await Client.ConnectToServer(ServerRegions.Any, "");

    void SendPosition(Vector3 p) => room?.SendMcBinary(
        new PlayerMoveData { playerId = Client.MyID, x = p.x, y = p.y, z = p.z },
        GameMessage.PlayerMove, McProtocol.Unreliable);
}

7. Test on your machine

  1. On MultiConnectClient, turn on Connect To Local Server and set Local Server Address to 127.0.0.1:7777.
  2. Unity opens a project in one Editor at a time, so press Play on ServerScene in the Editor, and run the client as a Development Build: in File > Build Profiles (Build Settings on older Unity versions), enable only ClientScene and tick Development Build. Unity's Multiplayer Play Mode also works, if you use it.

The build you test is the build you ship. Connect To Local Server only works in the Editor and Development Builds, so a release build always goes to your live deployment. Local runs send no analytics, so tests never count as players.

8. Create your server deployment

A new deployment starts from a first build, and Unity makes it for you:

  1. In File > Build Profiles, enable only ServerScene. Every enabled scene goes into the server build.
  2. Open MultiConnect > Build and Deploy Server. Under New deployment? Make its first build, check the Binary name and press Build First Zip. If it asks, add Linux Build Support and Linux Dedicated Server Build Support to this Unity version in Unity Hub.
  3. When it finishes, press Show the Zip to find the zip.
  4. On the dashboard, open the project from step 2, choose Create New Deployment, then Server, and fill in the form:
    • Platform: Unity (Linux Dedicated Server)
    • Mode: Combined
    • Plan and regions: what you want to run, and where
    • Entry Point File: the same Binary name, such as MyGameServer.x86_64
    • SDK App: the app from step 2. This is how clients find the deployment from their SDK key
    • Build: your zip

For separate masters and game servers, see Master and game servers.

9. Go live

  1. On MultiConnectClient, turn Connect To Local Server off.
  2. In File > Build Profiles, enable only ClientScene.
  3. Build your client and play. It finds your deployment by its SDK key.

Open Game Analytics on the dashboard to see players arrive.