Authentication - StansAssets/com.stansassets.ultimate-mobile GitHub Wiki
Let's talk about general player authentication practices with the Ultimate Mobile plugin.
Sign In
Your game should authenticate the player as early as possible after launching, ideally as soon as you can present a user interface to the player. The code sample below show's how to authenticate the user:
using SA.CrossPlatform.GameServices;
...
var client = UM_GameService.SignInClient;
client.SignIn(result => {
if (result.IsSucceeded) {
Debug.Log("Player is signed");
} else {
Debug.LogError($"Failed to sign in: {result.Error.FullMessage}");
}
});
The player info is available right after successful authentication. How ever, sign-in isn't only one flow that can update player info or state, There is a number of events on different platforms. All those cases handled internally by a plugin.
That's why you need to subscribe to the OnPlayerUpdated event. Also, you should know that OnPlayerUpdated will be fired before the sign-in flow. Use OnPlayerUpdated event to update the player UI in your game. See the code sample below:
using SA.CrossPlatform.GameServices;
...
UM_GameService.SignInClient.OnPlayerUpdated.AddListener(PrintPlayerInfo);
private void PrintPlayerInfo() {
var playerInfo = UM_GameService.SignInClient.PlayerInfo;
Debug.Log($"playerInfo state: {playerInfo.State}");
if(playerInfo.State == UM_PlayerState.SignedIn) {
var player = playerInfo.Player;
Debug.Log($"player id: {player.Id}");
Debug.Log($"player alias: {player.Alias}");
Debug.Log($"player displayName: {player.DisplayName}");
}
}
Player Avatar Image
You can also grab the player avatar as Unity Texture2D object using the GetAvatar method. See the example below:
using SA.CrossPlatform.GameServices;
...
var client = UM_GameService.SignInClient;
if(client.PlayerInfo.State == UM_PlayerState.SignedIn) {
var player = client.PlayerInfo.Player;
player.GetAvatar((texure) => {
//We need to make sure here that player is still signed
//by the time we are getting image load callback
if(client.PlayerInfo.State == UM_PlayerState.SignedIn) {
if(texure == null) {
//You may want to assign some default texture here
}
//Assign image to RawImage object
m_userAvatar.texture = texure;
}
});
}
Check Auth State
The player authentication state can always be accessed via PlayerInfo.State see the example below:
using SA.CrossPlatform.GameServices;
...
var client = UM_GameService.SignInClient;
if(client.PlayerInfo.State == UM_PlayerState.SignedIn) {
Debug.Log("Player is currently signed");
}
Sign Out
Some platforms (Android for example) have a requirement for the developer to provide a user with the ability to sign in and out from the application. You may use SignOut function for that purpose. See the example below:
using SA.CrossPlatform.GameServices;
...
UM_GameService.SignInClient.SignOut((result) => {
if (result.IsSucceeded) {
Debug.Log("Player is just out");
} else {
Debug.LogError($"Sign out failed: {result.Error.FullMessage}");
}
});
Please note that SignOut call will also trigger OnPlayerUpdated event.