Facebook Open Graph Objects in Unity3d

Introduction

In this second post of the series of how to integrate Facebook into our Unity3d game, we are going to be looking on how to send our own custom Facebook Open Graph Objects in Unity3d to our friends from inside our game.

Send Life Example

If you haven’t already, be sure to check out our first part of these series of posts, on how to Integrate Facebook Achievements In Unity3d.

If you are a bit lost on WHAT is an Open Graph (OG) Object, it’s really simple to understand: Almost anything inside our game can be modeled as a OG object, for example a life you can ask a friend for, or sending a cool power up to as a gift. When they are inside Facebook, we reference them as Facebook Open Graph Objects.

OK, enough talking. Let’s get started on how to do this. For this example, we will send a life to our friends! As with our other post, this will be divided into two parts: One explaining the Facebook side configuration we need to do, and after we have that all set up, the appropriate C# code to make the actual call.

Facebook & Server side

Head on to the Facebook Developer Console , and under the menu Apps, select your game (If you don’t have your game in the dropdown menu, go and create it before continuing). Now, from all the options on the left-side menu, click on Open Graph. This opens up the tab where you can manage everything Open Graph related.

Click on Object Types, and then Add Object Type. For this example, as we said, we are going to create a life.

Now we entered the editing screen of the new object. Just fill in the appropriate names for plural and singular, and scroll down to save the changes. (Keep in mind, that you can always come back to this page by going to the Facebook Developer Console , clicking on Open Graph and then Object Types.

Now that we have the type created, we have to crate and instance of that type, to use in our game. Head on back to the Developer Console of your game, and on Open Graph -> Object Types, you should now see your new created type life (Or you can just use backspace ;) )

Open Graph Example

Click on the right hand side, where it says Get Code. Now you will be prompted with a new popup, that has examples of all the calls we can do with that object type (We can create a new object, update an existing one, delete, etc etc).
Since what we want to do is create one, just click on the link that is shown there (As it says there, keep in mind you need publish_actions permissions to perform this. To get these permissions, in the Graph API Explorer page, we can click on Access Token and select the permissions we would like to have )

Example of the Get Code screen

After following the link, we arrive at the Graph API Explorer, with some data already filled in for us. The only thing we have to do, is change a few things in the parameter of the POST call. I suggest you copy and paste it into your favorite text editor, for easier editing.

Now, before we fill it in with the proper values, there is a detail to keep in mind. There is an error on this code already (Yea, if you worked with Facebook already you wouldn’t be surprised). The values of those properties, SHOULD NOT be arround ” ” . Don’t ask me why it comes like this by default, but take a moment now and remove them. File should look as follows after clearing them out:

Now, lets fill in the corresponding values for the properties:

⇛ app_id – Leave it as is. This is the ID of your game.
⇛ type – Leave it as is. This will be the identifier of the type of Object you are creating.
⇛ url – This should be the url of your game. Keep in mind to escape (\) all special characters
⇛ title – The title to display.
⇛ image – The url to the image that you want to display.
⇛ description – A short description of the object.

Here is a final look at how my object looked:

Now, go back to the Graph API Explorer page where you were, copy the contents of your text editor, and replace the value that you copied to start off with this new one, with all the appropriate values.
If everything is correct, you should see an ID returned as a response. SAVE THIS ID! You will need this later on!

Id returned after creating a new object

Unity Integration

Now, all that’s left to do is make the appropriate call to the Facebook API from inside Unity. To do this, we will use the newly implemented structured App Request, that looks something as follows:

And now we are done. Test out sending a request to your friend, and they will receive a notification similar to the one below:

Life Received

And that’s it! On the next post, we will see how to combine this with custom actions, and create engaging custom stories to post on your users walls whenever they do something significant inside your game.

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

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.

12 thoughts on “Facebook Open Graph Objects in Unity3d

  1. I’ve tried altering the existing code from FB’s Graph API Explorer, as well as altering and using the example code that you provide, but with either I’m receiving an error message “(#100) Parameter should be a JSON-encoded object: {“title”:”My Title”, …,}”. I’m not entirely sure why, because everything appears to be formatted correctly, but I must be doing something wrong. In any case, I thought I’d ask in case you might know where I’ve gone awry. Thanks!

    1. Oddly, if I just use the default code that they provide, I get an ID, but any alterations to the code result in the error… Weird…
      Here’s what they provide, which works:
      {“og:url”:”http:\/\/samples.ogp.me\/524277964392027″,”og:title”:”Sample Life”,”og:type”:”zombie-match-three:life”,”og:image”:”https:\/\/fbstatic-a.akamaihd.net\/images\/devsite\/attachment_blank.png”,”og:description”:””,”fb:app_id”:501113573375133}

      And here’s what I’ve tried, which doesn’t:
      {“og:url”:”https:\/\/goodfootgames.com\/games\/zombi3″,”og:title”:”Life”,”og:type”:”zombie-match-three:life”,”og:image”:”https:\/\/goodfootgames.com\/games\/zombi3\/fb\/images\m/life.png”,”og:description”:””,”fb:app_id”:501113573375133}

      Please let me know if you see anything amiss. Thanks!

      1. Doh! Figured it out. I was using the url: for the game on my website, not the Facebook app. Works like a charm. Thanks again for the great tutorials! I look forward to seeing what’s in store for the rest of the series :)

  2. Dang! Managed to get a proper Object ID from the FB OG, but now I’m having issues with errors in the FB AppRequest code. I’ll go check out the docs & FriendSmash tutorial / project to see what I’m missing.

    In the meantime, I was also curious as to why/how the App Request itself doesn’t specify a receiver at any point that I can see, only the sender. How are you specifying who recieves the Life you’re sending?

  3. Alright, with a bit more fiddling, I was able to get things working correctly. Your tutorial, however, doesn’t cover how to handle receiving the life from the other user’s end, and I’m not sure just how to go about setting up an in-game message system to handle it. I’ll be looking into it elsewhere, but it might make a good addition to your tutorial as well.

      1. I’m using a Facebook AppRequest call to check a given user’s messages when I log them in and request permissions, etc., then parsing out the relevant messages & sender IDs from the JSON info I receive in the response. Here’s an example of the AppRequest:
        FB.API(“/me/apprequests?access_token=” + FB.AccessToken, Facebook.HttpMethod.GET, YourCallBackFunction);

  4. I have noticed you don’t monetize your website, don’t
    waste your traffic, you can earn extra bucks every month because you’ve got high quality content.
    If you want to know how to make extra $$$, search
    for: best adsense alternative Wrastain’s tools

Leave a Reply to Sid Cancel 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>