Hello,
I am new to using unity and am trying to create a multiplayer lobby system.
Currently I have a way for players to join a lobby, but I am unsure if I am supposed to use a NetworkManager to start a host at the same time.
Line 3 is what I am unsure of if I need. This function is linked to a button in the UI of my scene. I know this function is doing a lot, and I am probably doing something in an inefficient way, but I can figure that out later.
The function starts a host, sets a lobby name, creates a private lobby, sets the lobby to visible, shows the lobby name, and lobby code, and spawns a prefab (image and text) that shows what player is in the lobby (currently just shows the host), and sets that prefab as a child of a GridLayoutGroup.
public async void CreateLobby()
{
NetworkManager.Singleton.StartHost();
var callbacks = new LobbyEventCallbacks();
//callbacks.LobbyChanged += OnLobbyChanged;
if (string.IsNullOrEmpty(lobbyName.text.Trim()) || string.IsNullOrWhiteSpace(lobbyName.text.Trim()) || lobbyName.text == "")
{
lobbyName.text = await AuthenticationService.Instance.GetPlayerNameAsync() + "'s Lobby";
}
CreateLobbyOptions options = new CreateLobbyOptions();
options.IsPrivate = true;
Lobby lobby = await LobbyService.Instance.CreateLobbyAsync(lobbyName.text, playerCount.value, options);
hostLobby = lobby;
try
{
await Lobbies.Instance.SubscribeToLobbyEventsAsync(hostLobby.Id, callbacks);
}
catch (LobbyServiceException e)
{
Debug.Log(e);
}
Debug.Log($"Created {lobby.Name} with {lobby.MaxPlayers} players");
lobbyCreationStuff.SetActive(false);
lobbyScreen.SetActive(true);
lobbyScreenName.text = lobby.Name;
lobbyCode.text = lobby.LobbyCode;
GameObject d = Instantiate(playerCardPrefab);
d.GetComponentInChildren<TMP_Text>().text = playerName;
d.transform.SetParent(cardHolder.transform);
}