Unity Gaming Services
These are code examples are copied from some of Unity's Gaming Services. I use these example methods throughout EasyCode docs especially when describing use cases for Dynamic Async events
GetJoinedLobbies() - https://docs.unity.com/lobby/get-joined-lobbies.html
public async Task GetJoinedLobbies()
{
try
{
var lobbyIds = await LobbyService.Instance.GetJoinedLobbiesAsync();
}
catch (LobbyServiceException e)
{
Debug.Log(e);
}
}
RemovePlayerFromLobby() - https://docs.unity.com/lobby/leave-a-lobby.html
public async Task RemovePlayerFromLobby()
{
try
{
//Ensure you sign-in before calling Authentication Instance
//See IAuthenticationService interface
string playerId = AuthenticationService.Instance.PlayerId;
await LobbyService.Instance.RemovePlayerAsync("lobbyId", playerId);
}
catch (LobbyServiceException e)
{
Debug.Log(e);
}
}
LoadPlayerData() - https://docs.unity.com/cloud-save/cloud-save-usage.html#Fetching_items
public async Task LoadPlayerData()
{
try
{
Dictionary<string, string> savedData = await CloudSaveService.Instance.Data.LoadAsync(new HashSet<string> { "key" });
Debug.Log("Done: " + savedData["key"]);
}
catch (LobbyServiceException e)
{
Debug.Log(e);
}
}
SavePlayerData() - https://docs.unity.com/cloud-save/cloud-save-usage.html#Save_an_item
public async Task SavePlayerData()
{
try
{
var data = new Dictionary<string, object> { { "key", "someValue" } };
await CloudSaveService.Instance.Data.ForceSaveAsync(data);
}
catch (Exception e)
{
Debug.Log(e);
}
}
UpdatePlayerData() - https://docs.unity.com/lobby/update-player-data.html
public async Task UpdatePlayerData()
{
try
{
UpdatePlayerOptions options = new UpdatePlayerOptions();
options.Data = new Dictionary<string, PlayerDataObject>()
{
{
"existing data key", new PlayerDataObject(
visibility: PlayerDataObject.VisibilityOptions.Private,
value: "updated data value")
},
{
"new data key", new PlayerDataObject(
visibility: PlayerDataObject.VisibilityOptions.Public,
value: "new data value")
}
};
//Ensure you sign-in before calling Authentication Instance
//See IAuthenticationService interface
string playerId = AuthenticationService.Instance.PlayerId;
var lobby = await LobbyService.Instance.UpdatePlayerAsync("lobbyId", playerId, options);
}
catch (LobbyServiceException e)
{
Debug.Log(e);
}
}
Last updated
Was this helpful?