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.
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 )
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 )
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!
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:
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.