Documentation
Ask or search…
K
Links
Comment on page

Lobby Manager

  • In scene 1_lobby, the UI to create is as illustrated below.
  • Create an object named LobbyManager and establish links to the UI Elements
  • You can add Singleton for Lobby Manager.
  • In Awake method make it DontDestroyOnLoad and call ServiceInit.
protected override void Awake()
{
DontDestroyOnLoad(gameObject);
this.ServiceInit();
}
protected virtual async void ServiceInit()
{
this.profileName = "profile_"+Random.Range(1111111, 10001000).ToString();
Debug.Log("ServiceInit: " + this.profileName);
var options = new InitializationOptions();
options.SetProfile(this.profileName);
await UnityServices.InitializeAsync(options);
await AuthenticationService.Instance.SignInAnonymouslyAsync();
this.playerId = AuthenticationService.Instance.PlayerId;
}
  • In Start method call RandomLobbyName and run an InvokeRepeating for LobbyUpdate
protected override void Start()
{
this.RandomLobbyName();
InvokeRepeating(nameof(LobbyUpdate), 2f, 1f);
}
protected virtual void RandomLobbyName()
{
this.inputLobbyName.text = Random.Range(1111111, 10001000).ToString();
}
protected virtual void LobbyUpdate()
{
if (this.lobby == null) return;
Debug.Log("LobbyUpdate");
this.countPlayer = this.lobby.Players.Count;
this.maxPlayer = this.lobby.MaxPlayers;
this.inputPlayerCount.text = this.countPlayer.ToString();
this.inputMaxPlayer.text = this.maxPlayer.ToString();
}
  • Link CreateLobby method to the UI Button.
public virtual async void CreateLobby() {
this.lobbyName = this.inputLobbyName.text;
this.maxPlayer = int.Parse(this.inputMaxPlayer.text);
CreateLobbyOptions options = new CreateLobbyOptions
{
IsPrivate = false
};
this.lobby = await LobbyService.Instance.CreateLobbyAsync(this.lobbyName, this.maxPlayer, options);
this.lobbyCode = this.lobby.LobbyCode;
this.lobbyId = this.lobby.Id;
this.isHost = true;
this.inputLobbyCode.text = this.lobbyCode;
this.txtTextLobbyId.text = this.lobbyId;
this.lobbyEvents.SubscribeToLobbyEvents();
}
  • Link JoinLobby method to the UI Button.
public virtual async void JoinLobby()
{
this.lobbyCode = this.inputLobbyCode.text;
this.lobby = await LobbyService.Instance.JoinLobbyByCodeAsync(this.lobbyCode);
this.lobbyId = this.lobby.Id;
this.lobbyName= this.lobby.Name;
this.txtTextLobbyId.text = this.lobbyId;
this.lobbyEvents.SubscribeToLobbyEvents();
}
  • You might wonder what this.lobbyEvents is. To enhance code readability, all events will be organized in a seperate file within a child object.
public class LobbyEvents : LobbyAbstract
{
public virtual void SubscribeToLobbyEvents()
{
var callbacks = new LobbyEventCallbacks();
callbacks.LobbyChanged += OnLobbyChanged;
Lobbies.Instance.SubscribeToLobbyEventsAsync(this.lobbyManager.lobbyId, callbacks);
}
protected virtual void OnLobbyChanged(ILobbyChanges changes)
{
changes.ApplyToLobby(this.lobbyManager.lobby);
}
}
  • The Start Game button will load to the next scene
protected override void OnClick()
{
if (!LobbyManager.Instance.IsReady()) return;
Debug.Log("Start Game");
SceneManager.LoadScene("2_game");
}
  • If you find it hard to follow, you can download all the related files below.
LobbyManager.cs
5KB
Text
LobbyEvents.cs
561B
Text
LobbyAbstract.cs
742B
Text
UICreateLobby.cs
230B
Text
UIJoinLobby.cs
226B
Text
UIStartGame.cs
350B
Text