Appearance
Appearance
Google Sign-In allows players to log in with their Google account. This is similar to Platform: Google Play Games login, except it is cross-platform, is less geared towards games, and is more generic.
To use Google Sign-In, you need to:
Configure a Google API Console project.
Follow instructions at https://developers.google.com/identity/sign-in/android/start-integrating.
📌 Note
These are the same steps as with the 1. Create a Project in Google Play Console section of Platform: Google Play Games, so if you've done those before, there is no need to do it again!
Configure the game server to accept and validate Google Sign-In requests.
In the relevant Options.<env>.yaml
, set:
GooglePlayStore:
EnableGoogleAuthentication: true
GooglePlayOAuth2ClientCredentialsPath: [Oauth 2.0 client credentials json created in step 1]
Implement the client-side logic.
In the game UI, allow players to log in and log out. After a successful login, send a SocialAuthenticationClaimGoogleSignIn
request.
In this example, we use the Google Sign-In plugin from https://github.com/googlesamples/google-signin-unity.
Platform Dependency
Installing the plugin will also install the External Dependency Manager for Unity (EDM4U). For more information, see Using the External Dependency Manager for Unity on Android.
Configure the plugin once:
GoogleSignIn.Configuration = new GoogleSignInConfiguration()
{
RequestIdToken = true,
WebClientId = <Oauth 2.0 client id created in step 1>
};
Note that rather than hard-coding the WebClientId
, the client may also use MetaplaySDK.Connection.ServerOptions.GameServerGooglePlaySignInOAuthClientId
after the connection has been established.
On app launch, start opportunistic silent sign-in:
GoogleSignIn.DefaultInstance.SignInSilently().ContinueWith(task =>
{
if (task.Status == TaskStatus.RanToCompletion)
{
OnGALoggedIn(task.Result);
}
else if (task.IsFaulted)
{
// Silent sign-in failed, do nothing but observe the exception
_ = task.Exception;
}
});
On user UI action, perform new login and logout:
// On sign-in button tapped: Perform log-in flow.
// \note: Make sure to manually Sign-Out first to force new login flow and the
// generation of a new token.
GoogleSignIn.DefaultInstance.SignOut();
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(task =>
{
if (task.Status == TaskStatus.RanToCompletion)
{
// login success
OnGALoggedIn(task.Result);
}
else if (task.IsFaulted)
{
// Login failed
_ = task.Exception;
}
});
// On log-out button tapped: Sign out.
GoogleSignIn.DefaultInstance.SignOut();
OnGALoggedOut();
⚠️ Race Condition Warning
Task completions are not guaranteed to be executed on the Unity thread. Proper synchronization is required.
Handle passing the pending login state to the game server:
void OnLoggedIn(GoogleSignInUser user)
{
string claimedId = user.UserId;
string idToken = user.IdToken;
var claim = new SocialAuthenticationClaimGoogleSignIn(claimedId, idToken);
MetaplaySDK.MessageDispatcher.SendMessage(new SocialAuthenticateRequest(claim));
}