Skip to content

Google Play Games Services V2🔗

Login with Google

Google Play Games Services V2 - a set of services that allows your users to log in, track their best results in the leaderboard, compare their achievements, invite friends to play the game. Applications may include any or all of the following features supported by Google Play Games:

  • Leaderboards — compares scores with player friends and other players from around the world
  • Achievements — Scores are awarded to players in the Google Play Games Achievement Tracking System. Players can earn scores by completing certain in-game tasks. Players cannot use the section for anything else but to evaluate the progress in the games. It was designed so that players can communicate and compete with each other.

Important

The main differences from Google Play Games Services V1 are:

  • Authorization start at the time of application launch.
  • There is no API to log out the user.
  • There is no API to get a client token.
  • There is no API to login with Google Sign In.

Pre-setup🔗

The pre-setup can be found in the GoogleSignIn - Pre-setup section.

Add dependencies🔗

Add the MRGS SDK to the project:

Unity:

Adding to the project (general instruction)

Step 1. Add Sources

To add MRGS to a project via the Unity Package Manager (available from Unity 2018+) simply add a scopedRegistries section to the Packages/manifest.json file by adding the following entry:

{
    "dependencies": {
        ...
    },
    "scopedRegistries": [
            {
                "name": "MRGS",
                "url": "https://mrgs-nexus.my.games/repository/mrgs-uninty-plugins/",
                "scopes": [
                    "games.my.mrgs"
                ]
            }
    ]
}

Alternatively, you can click Edit -> Project Settings -> Package Manager -> '+' in scoped registry section, and fill in the fields according to the data above.

Step 2. Add dependency

  • Click Window -> Package Manager -> select 'Packages: MyRegistries' from dropdown list, select package MRGSAuthenticationGoogleGames from the list, then click "Install"
  • Import the module: using MRGS;
  • Download the latest version of the library. Unzip the archive.
  • (For unitypackage integration) In Unity, click Assets -> Import Package -> Custom Package, and select the games.my.mrgs.authenticationgooglegames.unitypackage package from the downloaded archive.
  • (For tgz integration) In Unity, click Window -> Package Manager -> '+' -> Add package from tarball, and select the games.my.mrgs.authenticationgooglegames-<version> package. tgz from the downloaded archive.
  • Import the module: using MRGS;

Android:

Adding to the project

Add a dependency in build.gradle file:

dependencies {
    def mrgsVersion = "6.x.x"

    implementation "games.my.mrgs:authentication-google-play:$mrgsVersion"
}

Copy the MRGSAuthentication.aar and MRGSAuthenticationGoogleGames.aar files into the libs directory of your project. Add the dependencies into the build.gradle file.

dependencies {
    //...
    implementation(name: 'MRGSAuthentication', ext:'aar')
    implementation(name: 'MRGSAuthenticationGoogleGames', ext:'aar')

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'androidx.browser:browser:1.5.0'

    implementation "com.google.android.gms:play-services-games-v2:17.0.0"
}

Setting MRGS parameters🔗

Now you need to specify the received Web Client Id when initializing MRGS

using MRGS;

public class MasterController : MonoBehaviour
{
    void Awake()
    {
        // Setting up mrgs params
        // ...

        var modulesParams = new List<MRGSExternalSDKSettings>;
        modulesParams.Add(new MRGSGooglePlayGamesParams(WEB_CLIENT_ID));
        // Setting up external sdk params
        // ...
        IMRGSServerDataDelegate loadDelegate =...
        MRGService.Instance.Initialize(serviceParams, modulesParams, loadDelegate);
    }
}
import games.my.mrgs.MRGServiceParams;
import games.my.mrgs.MRGSModuleParams;
import games.my.mrgs.MRGService;
import games.my.mrgs.authentication.google.play.MRGSGooglePlayGames;

public class YourApplicationClass extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        final MRGServiceParams serviceParams = ...;
        final List<MRGSModuleParams> moduleParams = ...;
        // Setting External SDKs
        final MRGSGooglePlayGamesParams googlePlayGamesParams = MRGSGooglePlayGamesParams.init("GOOGLE_CLIENT_ID");
        moduleParams.add(googlePlayGamesParams);

        MRGService.service(context, serviceParams, moduleParams);
    }
}

Work with MRGSAuthentication interface🔗

To work with the Google Play Games, use the MRGSAuthenticationGoogleGames class. Please note that this class implements main interface MRGSAuthentication, so it includes all the methods of this interface. The following describes the standard implementation of the MRGSAuthentication interface:

Check authorization status (whether the user is logged in):

final bool isLoggedIn = MRGSAuthenticationGoogleGames.Instance.IsLoggedIn();
import games.my.mrgs.authentication.google.play.MRGSGooglePlayGames;

final boolean isLoggedIn = MRGSGooglePlayGames.getInstance().isLoggedIn();

For regular login use one of the methods:

// If error occured, error object will be non-null
MRGSAuthenticationGoogleGames.Instance.Login((MRGSError error) => {
    if (!error) {
        // Auth success
    } 
});
import android.app.Activity;

import androidx.annotation.NonNull;

import games.my.mrgs.MRGSError;
import games.my.mrgs.authenticationMRGSCredentials;
import games.my.mrgs.authentication.MRGSLoginCallback;
import games.my.mrgs.authentication.google.play.MRGSGooglePlayGames;

MRGSGooglePlayGames.getInstance().login(activity, new MRGSLoginCallback() {
    @Override
    public void onSuccess(@NonNull MRGSCredentials credentials) {
        // Handle result.
    }

    @Override
    public void onError(@NonNull MRGSError error) {
        // Handle error.
    }
});

To get all the information about the current authorization status, user information, authorization status, use the methods:

//An object is returned that combines information about the user (identifier, name, email, etc.) and about the token.
MRGSAuthenticationGoogleGames.Instance.GetCredentials((MRGSAuthenticationCredential credentials, MRGSError error) => {
    string resultDescription = "getCredentials:\nCredentials: " + credentials + "\nError:\n" + error;
    MRGSLog.getInstance().addLog("MRGSAuthentication<Unity> - AppleGameCenter " + resultDescription);
    if (!error) {
        // Credentials data work
    }
});

//Information about the user, identifier, name, email, etc. is returned separately.
MRGSAuthenticationGoogleGames.Instance.GetCurrentUser((MRGSAuthenticationUser user, MRGSError error) => {
    string resultDescription = "getCurrentUser:\User: " + user + "\nError:\n" + error;
    MRGSLog.getInstance().addLog("MRGSAuthentication<Unity> - AppleGameCenter " + resultDescription);
    if (!error) {
        // User data work
    }
});
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import games.my.mrgs.MRGSError;
import games.my.mrgs.authentication.MRGSAccessToken;
import games.my.mrgs.authentication.MRGSAuthentication;
import games.my.mrgs.authentication.MRGSUser;
import games.my.mrgs.authentication.google.play.MRGSGooglePlayGames;
import games.my.mrgs.utils.optional.BiConsumer;

// Returns user's information.
MRGSGooglePlayGames.getInstance().getCurrentUser(new MRGSAuthentication.UserCallback() {
    @Override
    public void onSuccess(@NonNull final MRGSUser user) {
    }

    @Override
    public void onError(@NonNull MRGSError error) {
    }
});

// Returns authentication's information.
MRGSGooglePlayGames.getInstance().getAccessToken(new BiConsumer<MRGSAccessToken, MRGSError>() {
    @Override
    public void accept(@Nullable MRGSAccessToken accessToken, @Nullable MRGSError error) {
        if (error != null) {
            Log.d("MRGSAuthentication", "AccessToken error: " + error.getErrorText());
        } else {
            Log.d("MRGSAuthentication", "AccessToken success: " + accessToken);
        }
    }
});

In order to "log out" of a user’s account, use the method:

MRGSAuthenticationGoogleGames.Instance.Logout();
import games.my.mrgs.authentication.google.play.MRGSGooglePlayGames;

MRGSGooglePlayGames.getInstance().logout();

Important

This function only removes the cache, because Google Play Games Services V2 does not support the "logout" function, so the user still remains logged in. The only way to logout is to open Play Games application, select the required application and revoke access.

Additional features of Google Play Games🔗

Also, MRGSAuthenticationGoogleGames (MRGSGooglePlayGames on Android) offers you some additional features

In order to get a user avatar with a given identifier, use the method:

MRGSAuthenticationGoogleGames.Instance.GetAvatar("required_user_id", MRGSAuthenticationPhotoSize.Small, (Texture2D avatar, MRGSError error) => {
    if(error == null){
        // Operation completed successfully
    }
});
import android.graphics.Bitmap;

import androidx.annotation.NonNull;

import games.my.mrgs.MRGSError;
import games.my.mrgs.authentication.MRGSAvatarCallback;
import games.my.mrgs.authentication.MRGSUser;
import games.my.mrgs.authentication.google.play.MRGSGooglePlayGames;

// Call #getCurrentUser() to get current user.
final MRGSUser user = ...
// Get user's avatar.
MRGSGooglePlayGames.getInstance().getUserAvatar(user, new MRGSAvatarCallback() {
    @Override
    public void onSuccess(@NonNull Bitmap bitmap) {
        // Handle result.
    }

    @Override
    public void onError(@NonNull MRGSError error) {
        // Handle error.
    }
});

Work with achievements🔗

Wor with achievements is described in the section Work with player achievements (Achievements).


Last update: 2023-10-25
Created: 2020-05-28