Authentication

Let's talk about general player authentication practices with the Ultimate Mobile plugin.

Sing 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.SingIn((result) => {
    if(result.IsSucceeded) {
        Debug.Log("Player is signed");
    } else {
        Debug.Log("Sing in failed: " + result.Error.FullMessage);
    }
});

The player info is available right after successful authentication. How evet, sing-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(() => {
    PrintPlayerIfno();
});

 private void PrintPlayerIfno() {
    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.State: " + player.Id);
        Debug.Log("player.State: " + player.Alias);
        Debug.Log("player.State: " + 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 singed 
    //by the time we are getting image load callback
    if(client.PlayerInfo.State == UM_PlayerState.SignedIn) {

        if(texure == null) {
            //You mau want to assing some default texture here
        }

        //Assign image to RawImage object
        m_userAvatar.texture = texure;
    }
    m_userAvatarLoader.gameObject.SetActive(false);
});

Check Auth State

The player authentication state can always be acssed 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");
}

Sing Out

Some platforms (Android for example) have a requirement for the developer to provide a user with the ability to sing in and out from the application. You may use SingOut function for that purpose. See the example below:

using SA.CrossPlatform.GameServices;
...

UM_GameService.SignInClient.SingOut((result) => {
    if (result.IsSucceeded) {
        Debug.Log("Player is just out");
    } else {
        Debug.Log("Sing out failed: " + result.Error.FullMessage);
    }
});

Please note that  SingOut call will also trigger OnPlayerUpdated event.