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.
OAuth Consent Screen
Scopes
Test User
Summary
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
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.
}
Showing the Leaderboard and Achievements
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.

Yorum Gönder