Synchronization of player names in a session
Expose and read player display names as session player properties so all players can view each other's names.
Read time 1 minuteLast updated 12 hours ago
When a player is authenticated through the Authentication service, you can write the player's name into the session as a player property, and update it during the session lifecycle. You can achieve this functionality using the following two extension methods:
- to enable synchronization on session creation and join.
WithPlayerName(VisibilityPropertyOptions visibility) - to read the name from any session player.
GetPlayerName()
Enable player name sync in session options
UseWithPlayerNamevar myPlayerName = "CustomName";await UnityServices.InitializeAsync();await AuthenticationService.Instance.SignInAnonymouslyAsync();await AuthenticationService.Instance.UpdatePlayerNameAsync(myPlayerName);// Configure your session and enable name sync (member visibility makes it readable by other members)var sessionOptions = new SessionOptions{ MaxPlayers = 4, Type = "Session"}.WithPlayerName(VisibilityPropertyOptions.Member);
Read player names from the session
UseGetPlayerNameIf a name isn’t available for any reason,// Read your own player name (from the local session player)string myName = session.CurrentPlayer.GetPlayerName();// Read other players’ namesforeach (var player in session.Players){ var name = player.GetPlayerName() ?? $"Player {player.Id}"; UnityEngine.Debug.Log($"Player {player.Id} name: {name}");}
GetPlayerName()null