Using Unity Google Play Achievements and Leaderboards

Using Unity Google Play Achievements and Leaderboards


Hello everyone, in this article I will explain how you can use Google Play Game Services in Unity. I will show you how to add achievements to your game and how to create a leaderboard using Google Play Game Services. Happy reading.

Setting Up Google Play Services

We will address the setup process in two parts; the first will be the setup process on Google Play, and the other will be the setup process on the Unity side.

First, we select Play Game Services from the menu on the left in the Google Play Console, and you will see a page like the one below. Choose the one that suits you.


Play Game Services Setup

After this process, we need to authenticate our app so that users can log in to their accounts with OAuth.

OAuth

Click "Configure" here, and a page like the one below will open.

OAuth Page

Click the Google Cloud Platform link on this page. On the following pages, perform the steps shown below.

OAuth Consent Screen


1

On the first page, select External and click the "Create" button.

On the next page, there will be many fields to fill in. You must fill in the application name, application support email address, and developer email address fields. (Fields marked with "*" are mandatory.)
Note: It will be advantageous for you to fill in the other fields as well.

Scopes

On the next page, press "Save and Continue" to proceed.

Test User

If you want to add a test user, enter the email addresses of the test users and proceed.

Summary

The information you entered will be displayed on this page. If you don't see any errors, you are done. Now, click the "Verify Configuration" button on the Google Play Console and wait. Your OAuth system will be set up with the information you entered.

On the Play Console, go to the add credential section and create a credential.

Credential

When you click the button, a page like the one below will appear.

3

If it says there is no OAuth client at the bottom, click the create OAuth client button and create an OAuth client.

4

After clicking, the credential information for your application/game will appear on the opened page. Click the "Create OAuth Client ID" button at the top of the page, fill out the necessary fields according to your app info in the newly opened window, then click the "Create" button.

Our Code

Once setup is complete, the only thing left to do is to access and update the achievements and leaderboard values with our code and display them to the user.

You will need to edit some parts of the code below for your own project. I will note the parts you need to edit according to your project.

User Login

For a player to add achievements or appear on the leaderboard, they must be logged in with their Google Play account. We use the following code for user login.

You can use the code below directly in your project without making any changes.

    private void Start()
    {
        UserGooglePlayLogin();
    }

    void UserGooglePlayLogin() // We create a function to allow the user to log in.
    {
        try // With the Try - Catch structure, even if we get an error, we prevent the game from crashing.
        {
            PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().Build(); // Connecting the client with Google Play.
            PlayGamesPlatform.InitializeInstance(config); // Adding settings while connecting.
            PlayGamesPlatform.DebugLogEnabled = true; // Enable debugging logs.
            PlayGamesPlatform.Activate(); // Activate Google Play.
            Social.localUser.Authenticate((bool success) => { }); // Perform user login.
        }
        catch (Exception exception) // Catch errors
        {
            Debug.Log(exception); // Print the error to the console.
        }
    }

Adding Score to Leaderboard

Yes, friends, here we have our code to add a score to the leaderboard. You need to edit 2 variables here to match your own project.

    public void AddScoreToScoreBoard()
    {
        string scoreBoardID = ""; // Insert the Leaderboard ID you got from Google Play Services here.
        int score = 0; // This is our score variable to be sent to the leaderboard. You need to replace this with the score variable from your own game project.
        
        if (Social.localUser.authenticated) // If the user is logged in
            Social.ReportScore(score, scoreBoardID, success => { }); // Send our score to the leaderboard. 
        
    }

As you can see, we pass 3 values to the Social.ReportScore function. Let's examine them;

In order: score, scoreBoardID, success..

  • score is the score we will send.
  • scoreBoardID is the ID of the leaderboard we got from Google Play Services.
  • success => {} is a function that's called when the process is successful. If you have used Do.Tween before, it's similar to the "OnComplete(() => function)" command.

Adding Achievements

Let's check out our code to keep track of users' achievement progress. You also need to edit this part for your own project
  
     public void AddAchievments()
    {
    string achievmentsID = ""; // The Google Play Services ID of the achievement
    float progress = ""; // The progress of the achievement.
        if(Social.localUser.authenticated) // If the user is logged in
            Social.ReportProgress(achievmentsID, progress, success => { }); // We update the achievement info.
    }

  
Just like the leaderboard, here too there are 3 values. Only instead of "score" there is "progress", which corresponds to the achievement's progress level.

The progress part takes a value between 0 and 100, and when it reaches 100, the achievement is fully unlocked. 

Showing the Leaderboard and Achievements

Here is the code we use to display the achievements earned by users and the leaderboard.
    public void ShowLeaderBoard()
    {
        if (Social.localUser.authenticated) // If the user is logged in
            Social.ShowLeaderboardUI(); // Show the leaderboard
    }

    public void ShowAchievments()
    {
        if(Social.localUser.authenticated) // If the user is logged in
            Social.ShowAchievementsUI(); // Show the achievements
    }
  

To show the achievement or leaderboard, it's enough to call the above functions with a button. You can also use this directly in your project without editing.

Yes friends, we have come to the end of this article. If there is a part you're stuck on or have a question you're curious about, you can ask me in the comments section below.