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. These action receive callbacks in response to the asynchronous Connect method when the connection to Google Play services succeeds, fails, or becomes suspended.

Here is 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 result of the connection  and the connection code. You can use the code to find out exact fail reason. Here is 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 is also few Action you can use to keep 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 following Getters with GooglePlayConnection:

public  bool IsInitialized
public static GPConnectionState State

 

For example, here is how you can find out if the player 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 user was connected to play service, due to Google policy plugin will disconnect from play service and start new connection when app is restored from background. GooglePlayConnection Actions and flags will react accordingly.

 

Current Player Info

After connection to play service is successful. Following information about current connected player is 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 play service by calling:

GooglePlayConnection.Instance.Disconnect ();

 

Or if you want not only disconnect from play service, bit also clear user default account for your game (it means user will be prompted to choose his default account for your game again, when you will 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 will call Connect function, user will see the pop up similar to picture bellow.

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 is 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 player has chosen his account using your UI,  you can force out with certain account using:

GooglePlayConnection.Instance.Connect (accountName);

 

Current player account(e-mail) used to sing into the google play:

string currentAccount = GooglePlayManager.Instance.CurrentAccount;

 

Managing  OAuth Tokens.

You can retrieve OAuth token as on example bellow:

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);
}

There LoadToken() methods is equals 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 to see 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 got ActionPlayerDisconnected event, you can store this to the PlayerPrefs, with 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 that the second on next app launch.

More

Connection API use example can be founded on following plugin example scenes:

  • PlayServiceExample
  • FriendsLoadExample
  • CustomLeaderborUIExample
  • QuestsAndEventsExample
  • RTM_Example