4. A working project
A project has one Unity project with two scenes. ServerScene runs on MultiConnectCloud, and ClientScene runs on players' devices. Scripts not attached to a GameObject can be used by both.
The developer does these in Unity:
- Import the package from https://multiconnectcloud.com/download-sdks/ with Assets > Import Package > Custom Package.
- On the dashboard, open Game Analytics, choose Create SDK app, and copy the SDK key from its Setup tab.
- In ServerScene, add a GameObject with MultiConnectGameServer. Set Sdk Key, Use Custom Hosting off, Use As Master Server on, Local Port
7777. - In ClientScene, add a GameObject with MultiConnectClient. Set the same Sdk Key, and Use Custom Hosting off.
You write the scripts below.
Script 4.1: GameMessages.cs, used by both scenes. Do not attach it to anything.
using MultiConnect.Serialization;
public enum GameMessage { PlayerMove = 1 }
[McSerializationObject]
public class PlayerMoveData
{
public string playerId;
public float x, y, z;
}
Script 4.2: ServerBootstrap.cs, attached in ServerScene
using System.Collections.Generic;
using MultiConnect.Global;
using MultiConnect.Server;
using MultiConnect;
using UnityEngine;
public class ServerBootstrap : MonoBehaviour, IRoomController
{
// In Awake, so it is set before the first player asks for a room.
// The server starts by itself: never call StartServer unless Use Custom Hosting is on.
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: the server decides what is allowed.
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();
}
Script 4.3: GameClient.cs, attached in ClientScene
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;
room.AddResponseListener(GameMessage.PlayerMove, OnPlayerMove);
};
if (MultiConnectInfo.IsMultiConnectInitialized()) Connect();
else MultiConnectInfo.onMultiConnectInitialized += Connect;
}
// MultiConnectCloud finds the server from the SDK key.
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);
void OnPlayerMove(object message)
{
var move = (PlayerMoveData)message;
}
}
Test and deploy
To test locally, the developer turns on Connect To Local Server on MultiConnectClient, with Local Server Address 127.0.0.1:7777. Unity opens a project in only one Editor, so one side runs in the Editor and the other as a Development Build. Release builds ignore this setting.
To deploy the first time, the developer does this:
- In File > Build Profiles, enable only ServerScene, because every enabled scene goes into the server build.
- Open MultiConnect > Build and Deploy Server, and press Build First Zip.
- On the dashboard, open their project, then Create New Deployment > Server, with Platform Unity (Linux Dedicated Server), Entry Point File the same as the window's Binary name, SDK App the app from step 2 above, and the zip as Build. The SDK App must be in the same project.
To go live, turn off Connect To Local Server, enable only ClientScene, and build the client.
For later updates, the developer creates a key with Allow deploys (CLI) on under Access Keys, attaches it on the deployment's page, pastes it into the window, and presses Build and Deploy.

