thecrazyprogram_icon thecrazyprogram_leaderboard
Facebook leaderboard as seen in The Crazy Program on Android.

 I got a lot of feedback from my friends testing our new mobile game The Crazy Program. One thing that came up a few times was a request to have a high scores table to compete with your friends. I created a quick Facebook App version of Donut Get! last Fall and knew it wouldn’t be too much work with Facebook’s built in high scores functionality.

Facebook allows you to save high scores for your app without needing any backend. The caveat is that you can only save one score per user. So this works decently for a global high score for your game, but not so well if you have different levels and different modes. Facebook’s scores API will also return a list of your friends that are playing, in order of rank. This makes it very easy to hit the ground running with some social features.

Prime31 Social Networking Plugins for Unity

I decided to purchase the Prime31 Social Networking plugins to handle the communication between Facebook and Unity. I had a good experience with their in-app purchase plugins and the support was good. There were other options for Facebook plugins but they either weren’t for both Android and iOS or I couldn’t tell whether or not they could handle posting high scores. Some plugins seemed to just handle basic Facebook connect features, or at least this was the impression I got.

prime31social_demo
Prime31 Social Networking demo scene

I started development on Android. The example scene is straightforward and I got connected with my Facebook App fairly quickly.

Prime31 Social Networking Configuration

So by now you should have a Facebook App created. If you haven’t, create one at http://apps.facebook.com .

You’ll need to setup your Facebook App to allow your Unity app to connect to it. Additionally, you’ll need to make iOS and Android specific configurations within your app.

Android:

The Official Android Plugin Documentation: http://prime31.com/docs#androidSocial

Inputting App id

For Android apps, you’ll have to generate a Key Hash for the Facebook control panel. It’s a string that’s generated with the command-line “keytool.”

1
2
3
4
5
6
7
// enter this command into your command-line
// replace "yourappreleasekeyalias" with the alias name from your keystore
// replace the path to the keystore you're using to sign your application

$ keytool -exportcert -alias yourappreleasekeyalias -keystore ~/.your/path/release.keystore | openssl sha1 -binary | openssl base64

//

This Stackoverflow post helps explain how to generate the Key Hash. You need to input the Key Hash on this screen in the Facebook app settings.

iOS:

The Official iOS Plugin Documentation: http://prime31.com/docs#iosSocial

Follow this YouTube video to figure out how to configure the plugin on iOS.

 

Coding in Unity

The first step is logging the user into Facebook. This will bring up Facebook login window.

1
2
3
4
5
6
public void Login() {

var permissions = new string[] { "email" };
FacebookAccess.loginWithReadPermissions( permissions );

}

Once logged in, you will need to ask for “publish_actions” permissions to be able to post scores. Apparently you are not allows to ask for this all at once, so it’s supposed to be requested after connecting.

To handle this, I listened for the “sessionOpenedEvent” and when that occurs, check to see if the proper permissions are available.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public bool CheckPermissions() {

if (!loggedIn) return false;

bool permissionsValid = false;
int permissionCount = 0;

List

foreach(string permission in permissions) {
if (permission == "email" || permission == "publish_actions") {
permissionCount++;
}
}

if (permissionCount >= 2) {
permissionsValid = true;
}

return permissionsValid;

}

 

Submitting Scores to Facebook

The Facebook scores system will accept any score that you send to it. So if you’re trying to display just the highest score, you’ll have to determine what the previously highest score was and determine if the new one surpasses it. This logic need to be handled on the front-end.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public void SaveScore(int score) {

// check if logged in
var isSessionValid = FacebookAccess.isSessionValid();
if (!isSessionValid) {
Debug.Log("not logged in, time to login");
Login();
return;
}

bool validPermissions = CheckPermissions();
if (!validPermissions) {
Debug.Log("not valid permissions in, request them!");
return;
}

if (score > bestScore) {
_bestScore = (int) score;
} else {
return;
}

string request = "me/scores";

var parameters = new Dictionary<string,object>()
{
{ "score", score.ToString() }
};

Facebook.instance.graphRequest( request, HTTPVerb.POST, parameters, OnPostGraphComplete );

}

 

Displaying Scores in Unity

Calling GameFacebook.instance.GetLeaderboard() will load data from the users Facebook friends that are using the app. This will return a hashtable containing user id’s, names, scores. You can retrieve avatars with the id’s, this is demonstrated in the LoadAvatar() method.

My Facebook Interface class

Here’s the complete class I used for The Crazy Program, no warranty or guarantees! It should provide a good starting point for Facebook high scores implementation.

 

Play The Crazy Program!

See how the high scores work yourself! 😉 The Crazy Program is free on Android and coming soon to iOS and OUYA.

thecrazyprogram_icon_150

android_app_on_play_logo_small

I think that’s it!

About the author:
Bryson Whiteman (http://www.sonofbryce.com)
Bryson is the guy behind all of the Sokay creations. Heading artwork and development, he's determined to make sure each game has a "distinctively Sokay" quality to them. He's always looking forward for a chance to experiment with new technologies to explore exciting ways to achieve fun.