Yes, you’re right, I’ll detail my implementation.
I based my work on the Multiplayer Widgets published by Unity. I reused a significant portion of the code, particularly for session creation. Here’s an overview:
var sessionOptions = new SessionOptions
{
MaxPlayers = enterSessionData.MaxPlayers,
IsLocked = false,
Password = enterSessionData.Password,
PlayerProperties = playerProperties,
Name = enterSessionData.SessionAction == SessionAction.Create
? enterSessionData.SessionName
: Guid.NewGuid().ToString()
};
I also define the player’s initial properties when joining or creating a session. This includes properties like the player’s name and a flag for their “ready” state:
private static async Task<Dictionary<string, PlayerProperty>> GetPlayerProperties()
{
var playerName = await Unity.Services.Authentication.AuthenticationService.Instance.GetPlayerNameAsync();
var playerNameProperty = new PlayerProperty(playerName, VisibilityPropertyOptions.Member);
var isReadyProperty = new PlayerProperty("false", VisibilityPropertyOptions.Member);
var playerProperties = new Dictionary<string, PlayerProperty>
{
{ "PlayerName", playerNameProperty },
{ "IsReady", isReadyProperty }
};
return playerProperties;
}
To manage the “ready” state, I implemented this function, which updates the player’s properties dynamically. It toggles the “IsReady” state between true
and false
. WithSetProperties
:
private void UpdatePlayerReadyState()
{
lobby.Session.ActiveSession.CurrentPlayer.Properties.TryGetValue("IsReady", out var isReadyPropertyValue);
var newIsReadyPropertyValue = isReadyPropertyValue is { Value: "true" } ? "false" : "true";
var updatedProperties = new Dictionary<string, PlayerProperty>(lobby.Session.ActiveSession.CurrentPlayer.Properties)
{
["IsReady"] = new PlayerProperty(newIsReadyPropertyValue, VisibilityPropertyOptions.Member)
};
lobby.Session.ActiveSession.CurrentPlayer.SetProperties(updatedProperties);
}
Another implementation wthSetProperty
:
private void UpdatePlayerReadyState()
{
lobby.Session.ActiveSession.CurrentPlayer.Properties.TryGetValue("IsReady", out var isReadyPropertyValue);
var newIsReadyPropertyValue = isReadyPropertyValue is { Value: "true" } ? "false" : "true";
var isReadyNewProperty = new PlayerProperty(newIsReadyPropertyValue, VisibilityPropertyOptions.Member);
lobby.Session.ActiveSession.CurrentPlayer.SetProperty("IsReady", isReadyNewProperty);
}
This is a temporary implementation for testing purposes.
The properties are correctly updated locally but not on the server side.
Multiplayer Services: 1.0.2
Network Topology: Distributed Authority
Protocol Type: Unity Transport