Your source code is now downloading. If does not start download please wait for 10 seconds

Your source code is now downloading. If does not start download please wait for 10 seconds

Created By Nikunj Rola | Last Updated on : 31 May 2016

Objective: The main objective of this tutorial is to give you an idea about how to setup leaderboard for Unity in Android Game.

Introduction:

What is Leaderboard?

Leaderboard is typically used in the gaming industry to signify rank among people who play various titles. Players can be ranked against other players based on their score. Overall, leaderboard can provide an incentive for players to improve as they give many a sense of superiority or accomplishment.

Here is complete tutorial for how to setup Leaderboard in Unity for Android using Google Play Services plugin.

Step 1 Download Leaderboard plugin

Download the latest plugin for Google play services form here. Find the latest version of plugin (.unitypackage file). Import it into your Unity project.

Step 2 Upload APK

To use leaderboard service, you have to upload your signed apk to your Google Play Publisher Account and publish it.

Step 3 Configure your application at Developer Account

To use the leaderboard plugin, you have to configure your game in the Google Play Developer Console first.

Following steps explain this in brief:

1. Open the Games with Game Services page.

2. Select the Google Play games services tab on the left, then click the Add a New Game button.

3. Select appropriate option for your game (new game or existing game).

4 If it is new game then add your game name and game type.

5. Click continue and save to create a new entry for your game in developer console.

6. Now select created application and link your application.

7. Now select leaderboard tab to set up leaderboard for your application.

8. Add all required fields and save it.

9. You will get Application ID and leaderboard ID from this tab.

You can get more idea about Add your game to the Google Play Developer Console from here.

You can get more idea about How to create leaderboard in your application from here.

Step 4 Configure Unity project

1. In your Unity editor, open window >> Google Play Game >> setup >> android to setup.

2. Get Resources from developer account – Copy it

3. Copy resources from developer account to unity editor

Step 5 Code Sample

Code Sample Create Script named GPGDemo.cs and copy following code and assign this script to gameobject.

using UnityEngine; using System.Collections; using GooglePlayGames; using UnityEngine.SocialPlatforms; public class GPGDemo : MonoBehaviour { #region PUBLIC_VAR public string leaderboard; #endregion #region DEFAULT_UNITY_CALLBACKS void Start () { // recommended for debugging: PlayGamesPlatform.DebugLogEnabled = true; // Activate the Google Play Games platform PlayGamesPlatform.Activate (); } #endregion #region BUTTON_CALLBACKS /// <summary> /// Login In Into Your Google+ Account /// </summary> public void LogIn () { Social.localUser.Authenticate ((bool success) => { if (success) { Debug.Log ("Login Sucess"); } else { Debug.Log ("Login failed"); } }); } /// <summary> /// Shows All Available Leaderborad /// </summary> public void OnShowLeaderBoard () { // Social.ShowLeaderboardUI (); // Show all leaderboard ((PlayGamesPlatform)Social.Active).ShowLeaderboardUI (leaderboard); // Show current (Active) leaderboard } /// <summary> /// Adds Score To leader board /// </summary> public void OnAddScoreToLeaderBorad () { if (Social.localUser.authenticated) { Social.ReportScore (100, leaderboard, (bool success) => { if (success) { Debug.Log ("Update Score Success"); } else { Debug.Log ("Update Score Fail"); } }); } } /// <summary> /// On Logout of your Google+ Account /// </summary> public void OnLogOut () { ((PlayGamesPlatform)Social.Active).SignOut (); } #endregion }

Code Description:

In this code I have created 4 methods, lets understand each in detail.

In this code i have created 4 methods:

Login This function is used for login into google account (Authenticate user). Add Score to leaderboard This function is used for update score in leaderboard. Show leaderboard This function is used for showing leaderboard – all player score (scoreboard). Logout This function is used for logout user from google.

Screens:

1. Login to leaderboard

2. Login Success screen

3. Scoreboard

Note Please make sure that your Android SDK is updated.

I hope you found this blog helpful while Setting up leaderboard for Android in Unity. Let me know if you have any questions or concerns regarding leaderboard setup for android, leaderboard setup in unity, please put a comment here and we will get back to you ASAP.

Got an Idea of Game Development ? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Unity 3D Game Development Company in India.