Some Hints on Unity Facebook Integration

by

Social media has nowadays become a key aspect of every application, especially when it comes to games. Below you can find an easy tutorial on how to speed up development when integrating Facebook into your Unity project.The first step is downloading the last Facebook SDK for Unity, which can be found here:

https://developers.facebook.com/docs/unity/downloads/

The installation and configuration parts are pretty straightforward. They consist of creating a Facebook application, importing the FB SDK package in Unity, and specifying your App Id.

These steps are well documented in the following official tutorial:

https://developers.facebook.com/docs/unity/getting-started/canvas/

Facebook SDK comes with a Unity scene called InteractiveConsole.unity that exposes the most basic Facebook functions. You’ll find this scene in your Assets/Example folder.

One thing that you’ll notice when you try to start the InteractiveConsole from the Unity Editor is that the Login functionality won’t work even if everything was setup correctly. If the same scene is built and executed in a real device though, the Login functionality will work just fine.

If logging in is required in some other part of your application, and you don’t want to go through the hassle of making a build every time you have to test some FB related operations, these are the few steps you’ll want to take to make your life and your development easier.

1)   Login on Facebook as the user who owns the application and visit https://developers.facebook.com/tools/explorer

2)   In the Application dropdown select your Facebook application and then click on Get Access Token. From here you can select also the permissions that you want to grant to your application.

3)   Once you click on “Get Access Token”, copy the string generated in the Access Token text box.

4)   Open a new tab in your browser and visit the URL: https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=fb_exchange_token&fb_exchange_token=THE_TOKEN_YOU_JUST_COPIED . Make sure you change the three upper-case parameter values (YOUR_APP_ID, YOUR_APP_SECRET, THE_TOKEN_YOU_JUST_COPIED).

5)   You’ll get a response that looks like:

access_token=YOUR_NEW_ACCESS_TOKEN&expires=518400

Copy the access token value (The access token you just generated, will last for 60 days)

6)   In your project, open the file Assets/Script/Facebook/EditorFacebook.cs and look for the Login function. Here change the userId to be the Facebook id of the user you just obtained the token with. Also assign to the accessToken variable the value you copied in step 5.

 Once this is done you can proceed with the authentication process described here:

 https://developers.facebook.com/docs/unity/tutorials/games/authenticate/#step2

 and start using the Facebook APIs with the token that can be accessed everywhere in your application through the static property FB.AccessToken.

 Your game is now ready to go VIRAL!!!

Leave a Reply

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