More GameSparks and Unity Goodness

So if you’ve read the previous post and have a GameSparks project set up with leaderboards and achievements and a Unity project that handles adding achievements and scores you’re all set to complete the basics by adding functionality to display the high scores and the achievements a player has earned.

Add a new scene and set it up like the following:

leaderboards

All that’s needed is a few lines of code to display the scores in the leaderboard. Open the Leaderboards class and add the following:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using GameSparks.Api;
using GameSparks.Api.Messages;
using GameSparks.Api.Requests;
using GameSparks.Api.Responses;
using GameSparks.Core;

public class Leaderboards : MonoBehaviour {

	// Use this for initialization
	void Start () {

        LeaderboardDataResponse response = new LeaderboardDataRequest().SetLeaderboardShortCode("HighScoreLeaderboard").SetEntryCount(10).Send();

        if(!response.HasErrors)
        {
            foreach (var entry in response.Data)
            {
                Text player = GameObject.Find("Player" + entry.Rank.ToString()).GetComponent<Text>();
                player.text = entry.UserName;
                Text score = GameObject.Find("Score" + entry.Rank.ToString()).GetComponent<Text>();
                score.text = entry.GetNumberValue("Score").ToString();
            }
        }
        else
        {
            Text player = GameObject.Find("Player1").GetComponent<Text>();
            player.text = "No Scores";
        }
	}

    public void OKButtonClick()
    {
        Application.LoadLevel("MainMenuScene");
    }
}

If you don’t have any scores other than the one from the previous post you should add some players through the GameSparks Test Harness and log some scores. To do so use the RegistrationRequest and AuthenticationRequest calls:

new player2

 

authentication

then add scores using the PostHighScoreEarned call under LogEvent:

posthighscore

Make sure you make all three calls in this order or your scores won’t be posted correctly.

On to achievements. I created a couple of additional achievements from the one created in the previous post:

achievements

Of course, we’ll need a scene to display them in:

achievementsscene

The graphic for each achievement is a UI Image object. We set the tag for the graphic to the achievement’s shortcode so we can find it when the scene loads.

Create a script and attach it to the scene’s Canvas as usual. Add the following to it:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using GameSparks.Api;
using GameSparks.Api.Messages;
using GameSparks.Api.Requests;
using GameSparks.Api.Responses;
using GameSparks.Core;


public class Achievements : MonoBehaviour {

	// Use this for initialization
	void Start ()
    {
        new ListAchievementsRequest().Send((response) =>
        {
            if (!response.HasErrors)
            {
                foreach (ListAchievementsResponse._Achievement achievement in response.Achievements)
                {
                    //if achievement hasn't been earned grey out icon
                    if (achievement.Earned.HasValue && !achievement.Earned.Value)
                    {
                        Image graphic = GameObject.FindGameObjectWithTag(achievement.ShortCode).GetComponent<Image>();
                        graphic.color = new Color(1f, 1f, 1f, .1f);
                    }
                }
            }
        });
	}

    public void OKButtonClick()
    {
        Application.LoadLevel("MainMenuScene");
    }

}

The ListAchievementsRequest call gets all the achievements set up in the system with a member for each achievement that tells if the player has earned it. If he hasn’t we change the alpha component of the color for the achievement’s graphic to indicate that the player hasn’t earned it.

That’s all it takes to get basic achievements and leaderboards working in your game. The project for this post is located here.

There is a lot more functionality in GameSparks that I’d like to cover. I’m thinking I might dig into some of the social functionality like friends and messages next. If you’ve looked into GameSparks and there’s something that you’d like to see covered let me know.

Leave a Reply

You must be logged in to post a comment.