This is the publishing of some notes I took while learning how to make a working lobby. This isn't a detailed tutorial (I recommend not only the Unity Docs but also Code Monkey's excellent video if you're completely new to the topic). In this article I just want to show some features that might be useful to think about when making your own lobbies. Really the main reason for this article is styling practice to find a good format I like for this. To test some CSS and HTML out for making nice code blocks. My next two post will feature more code and written examples for why things work the way they do. Features I implemented
AuthenticationService.Instance.SignedIn += () => { Debug.Log("Signed in " + AuthenticationService.Instance.PlayerId); };
await AuthenticationService.Instance.SignInAnonymouslyAsync();
Once you get a lobby set up you do need to send some kind of heartbeat to unity's services or it will be labeled as expired and can be deleted (although on their documentation it seems the let you leave it up for an hour. I have mine set to 30 seconds).
public async Task CreateRelay()
{
try
{
Allocation allocation = await RelayService.Instance.CreateAllocationAsync(3);
string joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
Debug.Log("Join Code: " + joinCode);
RelayServerData relayServerData = new RelayServerData(allocation, "dtls");
NetworkManager.Singleton.GetComponent().SetRelayServerData(relayServerData);
NetworkManager.Singleton.StartHost();
Debug.Log("Host started with Player ID: " + AuthenticationService.Instance.PlayerId);
return joinCode;
}
catch(RelayServiceException ex)
{
Debug.LogException(ex);
return null;
}
}
if (IsLobbyHost())
{
GameObject.Destroy(MainCamera);
GameObject.Destroy(eventManager);
Debug.Log("Host confirmed, changing level.");
//Levels will be dynamic based on playe choices but for this demo we have only made the one level.
NetworkManager.Singleton.SceneManager.LoadScene("Stronghold_1", LoadSceneMode.Single);
}
StartCoroutine(WaitForPlayerData());
playerData = player.GetComponent();
private void Initialize()
{
// Subscribe to importantEvents events
healthAndArmorComponent.OnHealthChanged += UpdateHealthDisplay;
healthAndArmorComponent.OnArmorChanged += UpdateArmorDisplay;
UpdateSoulLabel();
OnUIInitialized?.Invoke(this);
}
Using coroutines and events were a God send while figuring out the best way to set up a multiplayer match. Similarly how using interfaces,
and creating several small behavior classes for enemy NPCs has allowed for some really cool possible combinations to make challenging unique enemies with different abilities and behaviors.
I'm going to cut this off here, there's obviously so much more that went into this. But this kind of gives the gist of how you can quickly get up a working multiplayer demo. Unity Lobby + Relay.
And once the game is loaded for all clients, you need to ensure that every script is initialized properly for the server and for the clients. Don't forget to use
if(IsServer) or if (IsOwner)
when objects need to be spawned in or when a local player is performing an action only on their characters. If this is something you're working on and somehow find this, if YouTube or the manuals aren't getting you the help you need,
feel free to email/ message me on LinkedIn and I'll try to help you troubleshoot.
Best of luck in all your projects!