Player board
Lists everyone in the room and updates on OnPlayerJoined and OnPlayerLeft. See Players.
PlayerBoard.cs
using StageTime.SDK;
using TMPro;
using UnityEngine;
/// <summary>
/// Lists the players in the room (OnPlayerJoined/OnPlayerLeft, GetPlayers, displayName, isLocal, isMaster), the board's
/// owner, and a shared clock (Networking.GetServerTimeInSeconds) updated twice a second.
/// </summary>
public class PlayerBoard : StageTimeBehaviour
{
public TMP_Text playersLabel;
public TMP_Text clockLabel;
private int joins;
private int lefts;
private float nextClock;
private void Start()
{
Refresh();
}
private void Update()
{
if (Time.time < nextClock) return;
nextClock = Time.time + 0.5f;
if (clockLabel != null) clockLabel.text = "Server time: " + (Mathf.Round((float)Networking.GetServerTimeInSeconds() * 10f) / 10f) + " s";
}
public override void OnPlayerJoined(StageTimePlayer player)
{
joins++;
Refresh();
}
public override void OnPlayerLeft(StageTimePlayer player)
{
lefts++;
Refresh();
}
public override void OnOwnershipTransferred(StageTimePlayer player)
{
Refresh();
}
public void Refresh()
{
int count = StageTimePlayer.GetPlayerCount();
StageTimePlayer[] players = StageTimePlayer.GetPlayers(new StageTimePlayer[count]);
string text = $"Players: {count} (joined {joins}, left {lefts})\n";
for (int i = 0; i < players.Length; i++)
{
StageTimePlayer player = players[i];
if (player == null) continue;
text += $"[{player.playerId}] {player.displayName}";
if (player.isLocal) text += " (you)";
if (player.isMaster) text += " (master)";
text += "\n";
}
StageTimePlayer owner = Networking.GetOwner(gameObject);
text += "Board owner: " + (owner != null ? owner.displayName : "-");
if (Networking.IsMaster) text += "\nYou are the master";
if (playersLabel != null) playersLabel.text = text;
}
}