Integrate Facebook achievements in Unity


In the first post of these series, we are going to learn how to integrate Facebook achievements in Unity. Many developers overlook this feature, but what they don’t know is that they are missing out on a great way to attract new players into the games, and to generate a lot of engagement.

Example of finished achievement

Facebook & Server side

The first step is to create and register the achievement as a Facebook Open Graph object. Open Graph is a strongly typed API provided by Facebook consisting of objects and actions. Objects are the ‘nouns’ or targets for actions taken by people in your application.

Open your favorite text editor, and create a new HTML file. We need to create a file that contains in the head meta tags all the information needed to create an achievement for Facebook. The file needs to contain the following properties:

⇛ og:type – This is a fixed value. It needs to be “game.achievement”.
⇛ title – The title of the achievement.
⇛ og:url – Leave this field blank for now. We will update it later on!
⇛ og:image – The url to the image that you want to be displayed in the achievement.
⇛ game:points – The amount of points this achievement gives if completed.
⇛ fb:app_id – The App ID that owns this achievement.

So far, the file you are creating should be looking something like this:

TIP: If you want the users to be redirected to your game when they click on the achievement, you need to add a little ‘hack’ to the above code. Include after the </head> tag the following:

After the file is created, we need to upload it to a server (You can use Dropbox Public folder if you don’t own a server). After the file is uploaded, you need to get the link of the file as a public accessible URL. Once you have this URL, you need to edit the file again, and put it in the og:url property, that we left empty on purpose, and RE-UPLOAD the file. (make sure that after you re-upload the file, the link stays the same!).

To make sure this is all working properly, you can navigate to Facebook Debug Tool and put in the final URLof the file (the one we used to complete the og:url property). If the Open Graph Object is a valid one, we should see all the information of the meta tags displayed below as follows:

Facebook Debug Tool Parse Result

Once the file is created and uploaded without errors, we need to register the achievement in Facebook. First, we need to navigate to the Open Graph Explorer

Open Graph Result

1. Select the application to which you want to register the achievement.

2. Click on Get App Token, to use the token of the app, and not the users access token.

3. Fill in the following information:

⇛ Type: POST
⇛ URL: {app-id}/achievements
⇛ Add a new field with the key ‘achievement‘ and the value the url of the achievement in the server

If all the information above is correct, we should the console return us a ‘true’ as a result.

Unity Integration

Now that we have our achievement uploaded and registered, we need to make the appropriate call from inside Unity to reward the player the achievement. For this tutorial, we assume that you already have the Facebook SDK up and running inside your project (the user must also grant publish_actions to be able to post achievements to his wall).

With this class inside your project, with a call to GiveAchievement with the correct URL, you can have your game post beautiful achievements in no time!

Make sure to leave us a comment with any doubts, questions or future topics to these series of post about how to integrate Facebook within your Unity3d project.

Gaston Claret

Gaston Claret

Hard worker, truly passionate, and really efficient programmer. Problem solver by nature, and with the coding philosophy: the simpler, the better.

5 thoughts on “Integrate Facebook achievements in Unity

  1. Hi there, nice tutorial!! I’m facing problems with facebook sdk by publishing user’s status, it doesn’t do nothing and I can’t find a way to debug the whole proccess, can you give me some advice please ;-(

    1. Hello David!

      I’m not sure I quite understand what you want to achieve. What do you mean by ‘publishing the user’s status’? Do you want to post something in his timeline?

      If so, take a look at FB.Feed(..)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>