Sign In

When you want to make a connection to one of the Google APIs provided in the Google Play services library (such as Google+ or Games), you need to use an instance of GooglePlayConnection. The GooglePlayConnection manages the network connection between the user's device and each Google service.

 

Start a Connection

This guide shows how you can use GooglePlayConnection:

  • Connect to one or more Google Play services asynchronously and handle failures.
  • Perform synchronous and asynchronous API calls to any of the Google Play services.

Before starting connection, you need to specify the list of API's you want to use with Google Play. You can do this in plugin editor settings. Open Plugin Settings:

Windows → Android Native → Edit settings

You can also specify auto image loading arms. 

However, before you can begin a connection by calling:

GooglePlayConnection.Instance.Connect ();

 

you must specify an implementation for the ActionConnectionResultReceived action. This action receives callbacks in response to the asynchronous Connect method when the connection to Google Play services succeeds, fails or becomes suspended.

Here is an implementation example:

GooglePlayConnection.ActionConnectionResultReceived += ActionConnectionResultReceived;
GooglePlayConnection.Instance.Connect ();;

private void ActionConnectionResultReceived(GooglePlayConnectionResult result) {
	if(result.IsSuccess) {
		Debug.Log("Connected!");
	} else {
		Debug.Log("Cnnection failed with code: " + result.code.ToString());
	}
}

 

As you may notice, the GooglePlayConnectionResult contains a result of the connection  and the connection code. You can use the code to find out an exact fail reason. Here are available result codes:

CANCELED = 13,
DATE_INVALID = 12,
DEVELOPER_ERROR = 10,
DRIVE_EXTERNAL_STORAGE_REQUIRED = 1500,
INTERNAL_ERROR = 8,
INTERRUPTED = 15,
INVALID_ACCOUNT = 5,
LICENSE_CHECK_FAILED = 11,
NETWORK_ERROR = 7,
RESOLUTION_REQUIRED = 6,
SERVICE_DISABLED = 3,
SERVICE_INVALID = 9,
SERVICE_MISSING = 1,
SERVICE_VERSION_UPDATE_REQUIRED = 2,
SIGN_IN_REQUIRED = 4,
SUCCESS = 0,
TIMEOUT = 14

 

Result codes description can be found under the Google Offisial Documentation.

There are also a few Actions you can use to be informed with the current connection state:

public static Action ActionPlayerConnected =  delegate {};
public static Action ActionPlayerDisconnected =  delegate {};
public static Action<GPConnectionState> ActionConnectionStateChanged =  delegate {};

 

You can also use the following Getters with GooglePlayConnection:

public  bool IsInitialized
public static GPConnectionState State

 

For example, here is how you can find out if the player is currently connected using State getter.

if(GooglePlayConnection.State == GPConnectionState.STATE_CONNECTED) {
    //Player is connected
} 

 

Available connection states:

public enum GPConnectionState  {
	STATE_UNCONFIGURED,  //the default sate, means we never tried to connect in current session
	STATE_DISCONNECTED,
	STATE_CONNECTING,
	STATE_CONNECTED
}

 

Note: Every time when application goes to background (home button press), if a user is connected to the play service, due to Google policy plugin will disconnect from the play service and start new connection when app is restored from background. GooglePlayConnection Actions and flags will react accordingly.

 

Current Player Info

After connection with the play service is successful, the following information about a current connected player will be available.

Player Info GooglePlayManager.Instance.Player

GooglePlayerTemplate currentPlayer = GooglePlayManager.Instance.Player;

 

The Player is represented as GooglePlayerTemplate class:

public class GooglePlayerTemplate {
    public string playerId
    public string name 
    public bool hasIconImage
    public bool hasHiResImage
    public string iconImageUrl 
    public string hiResImageUrl 


    public Texture2D icon 
    public Texture2D image 
}

 

 

Disconnecting

You can easily disconnect from the play service by calling:

GooglePlayConnection.Instance.Disconnect ();

 

Or if you want not only disconnect from the play service, but also to clear a user's default account for your game (it means user will be prompted to choose his default account for your game again, when you try to connect ), you may use:

GooglePlusAPI.Instance.ClearDefaultAccount();

Note: Should be used instead of disconnect method.

 

GooglePlayConnection Actions and flags will react accordingly.

 

Advanced Sign In.

Note: As soon as you call Connect function, user will see the pop up similar to the picture below 

and will be prompted to choose from available accounts on his device(On first connect only).

If you want to draw one UI for this or simply find out what accounts are available on users device, here is the example how you can do this:

 

GooglePlayManager.ActionAvaliableDeviceAccountsLoaded += ActionAvaliableDeviceAccountsLoaded;
GooglePlayManager.Instance.RetriveDeviceGoogleAccounts();


private void ActionAvaliableDeviceAccountsLoaded() {
	string msg = "Device contains following google accounts:" + "\n";
	foreach(string acc in GooglePlayManager.Instance.DeviceGoogleAccountList) {
		msg += acc + "\n";
	} 

	AndroidNative.showMessage("Accounts Loaded", msg);
}

 

And after a player has chosen his account using your UI,  you can force out with certain account using:

GooglePlayConnection.Instance.Connect (accountName);

 

Current player's account(e-mail) is used to sign into the google play:

string currentAccount = GooglePlayManager.Instance.CurrentAccount;

 

Managing  OAuth Tokens.

You can retrieve OAuth token as in the example below:

GooglePlayManager.ActionOAuthTokenLoaded += ActionOAuthTokenLoaded;
GooglePlayManager.Instance.LoadToken();
//or
GooglePlayConnection.Instance.LoadToken(string accountName,  string scopes);

private void ActionOAuthTokenLoaded(string token) {
	AndroidNative.showMessage("Token Loaded", GooglePlayManager.Instance.LoadedAuthToken);
}

The LoadToken() methods equal the LoadToken(string accountName,  string scopes);  with the following paramethers:

GooglePlayManager.Instance.LoadToken(GooglePlayManager.Instance.CurrentAccount, "oauth2:https://www.googleapis.com/auth/games");

Token can be invalidated with:

GooglePlayManager.Instance.InvalidateToken(token) 

Similar methods from native Android Google Play lib:

Common questions

I want to check if my player has connected to GPServices in the past, and if so, connect them immediately when the game starts up.

If the user was connected in the past

GooglePlayConnection.Instance.Connect (); 

will connect user immediately without any additional popups or auctions. So you do not have to track it by yourself. Just call connect method on your app start as you normally do.

 

But I don't want to connect them automatically if they haven't signed in before. How can I detect that? 

If after connection request you get ActionPlayerDisconnected event, you can store this to the PlayerPrefs which you can check on your app startup and do not call connect function anymore. But I would not recommend doing that, it's always better to have your users connected to play service besides if user has connected once, the signing process will take less than the second on the next app launch.

More

An example of using the Connection API can be found in following plugin example scenes:

  • PlayServiceExample
  • FriendsLoadExample
  • CustomLeaderborUIExample
  • QuestsAndEventsExample
  • RTM_Example