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.

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.

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.

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.

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.

Related

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.

Alejandro Foronda

Awesome post, really helpful I always thought you needed a backend for a high score system.
THANK YOU!

Naresh

Thank you!!

Rungroj Keawpapai

Help me please!
– can not post score ( SaveScore(int score) )
– can not get me score ( GetMyScore() )
– can not get score ( GetLeaderboard() )

I’m sorry man, but reposting the code that I wrote isn’t of much help. Were you able to get the Prime31 plugins working correctly? Did you configure the FB ID and everything? Without FB configuration the rest won’t work.

I can’t help you unless you can explain what’s happening to cause an error. I’m not going to sort through your edits of my code.

Debug.Log( “you must first retrieve the app access token before posting a score” );

completionHandler( false );

return;

}

if( userId == null )

{

Debug.Log( “a valid userId is required to post a score” );

completionHandler( false );

return;

}

// post the score to the proper path

var path = userId + “/scores”;

var parameters = new Dictionary()

{

{ “score”, score.ToString() },

{ “app_access_token”, appAccessToken },

{ “access_token”, appAccessToken }

} ;

post( path, parameters, ( error, obj ) =>

{

if( error == null && obj is bool )

{

var result = (bool)obj;

completionHandler( result );

}

else

{

completionHandler( false );

}

} );

}

// Retrieves the scores for your app

public void getScores( string userId, Action onComplete )

{

var path = userId + “/scores”;

get( path, onComplete );

}

How can I understand your code?

liveagain

Hi, there.
You are very brilliant.
I have done post and get score.
It’s reason is due to Hashtable.
It cannot receive from object.
I’m wondering It is running on your game?
Maybe it’s reason is due to any platform or OS?.
Anyhow I’m very thanks.
The rest is the leaderboard.
I’m starting just now.
I’ll ask you again if I have problem.
It’s OK?

Sorry you’re having problems with this. I can’t offer much help since I haven’t looked at this in years and I know how constantly Facebook SDK and the plugins update so it’s hard to keep up to date with changes.