Documentation
Ask or search…
K
Links

Show Lobby Players

Making a list of players in the Room, including their names and readiness state to show to all players

Show the list of Players in the Lobby

Define several variables to control List Player data in MenuSceneManager:
[Header("List Players in Room")]
[SerializeField] private Transform _listPlayersContentTransform;
[SerializeField] private PlayerItem _playerItemPrefab; /* NOtice : PlayerItem */
public List<PlayerItem> listPlayers = new();
It's the same with List Lobby, so I create the PlayerItem.cs for each Player item in List Player:
public class PlayerItem : MonoBehaviour
{
[SerializeField] public TextMeshProUGUI sttText;
[SerializeField] public TextMeshProUGUI nameText;
[SerializeField] public TextMeshProUGUI roleText;
[SerializeField] public Image readyImage;
[SerializeField] public Sprite isReadyImage;
[SerializeField] public Sprite isNotReadyImage;
public void SetData(string stt,string name, PlayerTypeInGame role, bool isReady = false)
{
sttText.text = stt;
nameText.text = name;
roleText.text = role.ToString();
SetReadyImage(isReady);
this.gameObject.SetActive(true);
}
public void SetReadyImage(bool isReady = false)
{
if (isReady)
{
readyImage.sprite = isReadyImage;
}
else
{
readyImage.sprite = isNotReadyImage;
}
}
}

Reload the list of Players in Room

In MenuSceneManager.cs, start a coroutine to reload the list of players in the room using the PlayerDataObject in LobbyData:
public IEnumerator IECheckUpdateListPLayerInRoom()
{
while (true)
{
yield return new WaitForSeconds(1f);
if (AuthenticationService.Instance.IsSignedIn && LobbyManager.Instance.CurrentLobby is not null &&
!string.IsNullOrEmpty(LobbyManager.Instance.CurrentLobby.Id))
{
/* Disative all old lobby item in list */
foreach (Transform child in _listPlayersContentTransform)
{
child.gameObject.SetActive(false);
}
listPlayers.Clear();
/* Show every lobby item in list */
int i = 0;
foreach (Player p in LobbyManager.Instance.CurrentLobby.Players)
{
try
{
if (p.Data["Name"] != null && p.Data["Role"] != null && p.Data["IsReady"] != null)
{
i++;
PlayerItem playerItem;
try
{
playerItem = _listPlayersContentTransform.GetChild(i).GetComponent<PlayerItem>();
}
catch (Exception)
{
playerItem = Instantiate(_playerItemPrefab, _listPlayersContentTransform);
}
playerItem.SetData("#" + (i), p.Data["Name"].Value, p.Data["Role"].Value.ToString(),
bool.Parse(p.Data["IsReady"].Value));
listPlayers.Add(playerItem);
}
else
{
continue;
}
}
catch (Exception e)
{
continue;
}
}
}
}
}
Add a function to Update in MenuSceneManager.cs that checks if you are in a room. If you are in a room, set List Player in Room to the data and display the list of players in the UI.
Please note that when using the PlayerDataObject in the Lobby, when you call LobbyService.Instance.UpdatePlayerAsync(currentLobbyId, playerId, options), it updates only the parameters specified in the options. Even if you create new options, old data will still exist unless you remove or change it.