Android Games Sing-in

In order to access Google Play games services functionality, your game needs to provide the signed-in player’s account. If the player is not authenticated, your game may encounter errors when making calls to the Google Play games services APIs. This documentation describes how to implement a seamless sign-in experience in your game.

Checking Availability 

Before you begin to use any GMS API, you should consider checking if you can use GMS API on this device at all. The AN_GoogleApiAvailability  is a helper class for verifying that the Google Play services APK is available and up-to-date on this device. With its help, you can check if GMS API is available on a current device, and if it's not, you may try to resolve the issue. See the example below:

using SA.Android.GMS.Common;

int responce =  AN_GoogleApiAvailability.IsGooglePlayServicesAvailable();
if(responce == AN_ConnectionResult.SUCCESS) {
    // All good you can use GMS API
} else {
    Debug.Log("Google Api not avaliable on current device, trying to resolve");
    AN_GoogleApiAvailability.MakeGooglePlayServicesAvailable((result) => {
        if(result.IsSucceeded) {
           // Issue resolved you can use GMS API now
        } else {
           // Failed to resolve, all attempts to use GMS API on this device will fail
        }
    });
}

Implementing player sign-in

The AN_GoogleSignInClient class is the main entry point to retrieve the account of the currently signed-in player, and to sign-in the player if they have not previously done so on your app in the device.

To create a sign-in client, follow these steps:

  • Create a sign-in client via the AN_GoogleSignInOptions object. In the AN_GoogleSignInOptions.Builder to configure your sign-in, you must specify AN_GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN.
  • Call the AN_GoogleSignIn.GetClient() method and pass in options you previously configured. If the call is successful, Google Sign-In API returns an instance of AN_GoogleSignInClient.


To determine if a player has already signed-in, you can call the AN_GoogleSignIn.getLastSignedInAccount() method and check if it returns null.

using SA.Android.GMS.Auth;
...

private boolean isSignedIn() {
  return AN_GoogleSignIn.GetLastSignedInAccount() != null;
}

Performing silent sign-in

You can call SilentSignIn() to retrieve the currently signed-in player’s account, and try to sign players in without displaying a user interface if they have successfully signed in to your app on a different device.

If the silent sign-in attempt fails, you can optionally send the sign-in intent to display a sign-in user interface, as described in Performing interactive sign-in.

Since the state of the signed-in player can change when the activity is not in the foreground, Google recommends calling SilentSignIn()  after the game is resumed. For Unity  the best place to perform a silent sign in is when your game is unpaused.

The following code snippet shows how your app can perform silent sign-in:

using SA.Android.GMS.Auth;
...

AN_GoogleSignInOptions.Builder builder = new AN_GoogleSignInOptions.Builder(AN_GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
builder.RequestId();
builder.RequestEmail();
builder.RequestProfile();

AN_GoogleSignInOptions gso = builder.Build();
AN_GoogleSignInClient client = AN_GoogleSignIn.GetClient(gso);
AN_Logger.Log("SignInNoSilent Start ");

client.SignIn((signInResult) => {
    AN_Logger.Log("Sign In StatusCode: " + signInResult.StatusCode);
    if (signInResult.IsSucceeded) {
        AN_Logger.Log("SignIn Succeeded");
        UpdateUIWithAccount(signInResult.Account);
    } else {
        AN_Logger.Log("SignIn filed: " + signInResult.Error.FullMessage);
    }
});

If the silent sign-in attempt fails, you can analyze the signInResult.Error.Message and signInResult.Error.Code fields. From example A status code of AN_CommonStatusCodes.SIGN_IN_REQUIRED indicates that the player needs to take explicit action to sign-in. In this case, your app should launch an interactive sign-in flow as described in the next section.

Performing interactive sign-in

To sign in with player interaction, your app needs to launch the sign-in intent. If successful, the Google Sign-In API displays a user interface that prompts the player to enter their credentials to sign in. This approach simplifies your app development, since the sign-in activity handles scenarios such as needing to update Google Play services or showing consent prompts, on your app’s behalf.

As an example of how to perform the sign-in interactively, see the snippet below:

using SA.Android.GMS.Auth;
...

AN_GoogleSignInOptions.Builder builder = new AN_GoogleSignInOptions.Builder(AN_GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);

//Google play documentation syas that
// you don't need to use this, however, we recommend you still
// add those option to your Sing In builder. Some version of play service lib
// may retirn a signed account with all fileds empty if you will not add this.
// However according to the google documentation this step isn't required
// So the decision is up to you.
builder.RequestId();
builder.RequestEmail();
builder.RequestProfile();

AN_GoogleSignInOptions gso = builder.Build();
AN_GoogleSignInClient client = AN_GoogleSignIn.GetClient(gso);

AN_Logger.Log("Starting the default Sign in flow");
client.SignIn((signInResult) => {
    AN_Logger.Log("Sign In StatusCode: " + signInResult.StatusCode);
    if (signInResult.IsSucceeded) {
        AN_Logger.Log("SignIn Succeeded");
        UpdateUIWithAccount(signInResult.Account);
    } else {
        AN_Logger.Log("SignIn filed: " + signInResult.Error.FullMessage);
    }
});

Retrieving player information

The AN_GoogleSignInAccount that the Google Sign-In API returns does not contain any player information. If your game uses player information, such as the player’s display name and player ID, you can follow these steps to retrieve this information:

  • Obtain a AN_PlayersClient object by calling the GetPlayersClient() method, and passing in the AN_GoogleSignInAccount as a parameter.
  • Use the AN_PlayersClient methods to asynchronously load the AN_Player object that contains a player’s information. For example, you can call GetCurrentPlayer() to load the currently signed-in player. If the callback returns an error with status a code of SIGN_IN_REQUIRED, this indicates that the player needs to be re-authenticated. To do this, you need to sign in the player interactively.
  • If the task successfully returns the AN_Player object, you can then call the methods of the AN_Player  object to retrieve specific player details (for example, DisplayName or Id).
using SA.Android.GMS.Games;
...

AN_PlayersClient client = AN_Games.GetPlayersClient();
client.GetCurrentPlayer((result) => {
    if(result.IsSucceeded) {
        AN_Player player = result.Data;
        //Printing player info:
        AN_Logger.Log("player.Id: " + player.Id);
        AN_Logger.Log("player.Title: " + player.Title);
        AN_Logger.Log("player.DisplayName: " + player.DisplayName);
        AN_Logger.Log("player.HiResImageUri: " + player.HiResImageUri);
        AN_Logger.Log("player.IconImageUri: " + player.IconImageUri);
        AN_Logger.Log("player.HasIconImage: " + player.HasIconImage);
        AN_Logger.Log("player.HasHiResImage: " + player.HasHiResImage);
    } else {
        AN_Logger.Log("Failed to load Current Player " + result.Error.FullMessage);
    }
});

Displaying game pop-ups

You can display pop-up views in your game using the AN_GamesClient class. For example, your game can display a “Welcome back” or an “Achievements unlocked” pop-up. To allow Google Play games services to launch pop-ups in views in your game, call the SetViewForPopups() method. You can further customize where the pop-up appears in the screen by calling SetGravityForPopups().

using SA.Android.App;
using SA.Android.App.View;
using SA.Android.GMS.Games;
...

//When Sign in is finished with successes
var gamesClient = AN_Games.GetGamesClient();
gamesClient.SetViewForPopups(AN_MainActivity.Instance);

//optionally
gamesClient.SetGravityForPopups(AN_Gravity.TOP | AN_Gravity.CENTER_HORIZONTAL);

Signing the player out

Signing-out is done via call the SignOut() method on the AN_GoogleSignInClient.

using SA.Android.GMS.Auth;
...

AN_GoogleSignInOptions gso = new AN_GoogleSignInOptions.Builder(AN_GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).Build(); 
AN_GoogleSignInClient client = AN_GoogleSignIn.GetClient(gso);

client.SignOut(() => {
    ClearAccountUI();
});

Applications are required to provide users that are signed in with Google the ability to disconnect their Google account from the app. If the user deletes their account, you must delete the information that your app obtained from the Google APIs. You can also use RevokeAccess. Method revokes access given to the current application. Future sign-in attempts will require the user to re-consent to all requested scopes. 

using SA.Android.GMS.Auth;
...

AN_GoogleSignInOptions gso = new AN_GoogleSignInOptions.Builder(AN_GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).Build(); 
AN_GoogleSignInClient client = AN_GoogleSignIn.GetClient(gso);

client.RevokeAccess (() => {
    ClearAccountUI();
});