Integrating Unity to Facebook

The day has finally come! Today we have an article with some code in it. It resulted from a two-week struggle with Facebook SDK and Facebook API. I hope that information I provide here will do some good to you. According to my experience there’s no solid guarantee that stuff will remain the same after the next Facebook SDK update, so follow along while it’s still up-to-date.

Plan:

  1. Integrating Facebook SDK (reading now)
  2. Authorization and news sharing

Disclaimer. Just like the rest of my articles, this one can include some non-optimal choices so if you’d like to point out some better solutions, by all means, do so in the comment section below — I will be glad to hear from other Unity developers.

So, Viva Facebook!

The most popular social network in the world hardly needs any introduction or me listing advantages of attracting players and encouraging them to share their gaming achievements with their Facebook friends. All in all, there are two possible ways of integrating Unity to Facebook:

  1. Using Facebook Graph API (low-level API based on HTTP-requests)
  2. Using Facebook SDK for Unity (developer kit providing platform-specific opportunities)

In the first case you will be able to communicate with Facebook server on a lower level. It’s useful when you work with web applications or websites but communicating with the remote server via REST architecture and parsing its requests you can achieve more control over communication with Facebook. You application will be a little bit more lightweight too (only a couple of classes comparing to the full library of them in SDK).

In the second case a buffer library is used. It includes the most popular methods and scenarios used when working with Facebook.

Here’s an example of authorization via SDK:

FB.LogInWithReadPermissions (
    new List<string>(){"public_profile", "email", "user_friends"},
    AuthCallback
);

You only need to specify permissions and the callback defined earlier that will allow you to receive and process the answer after the logics behavior is returned to your application. By the way, when working with SDK the library itself is responsible for finding an available Facebook app and if it finds none, it will redirect you to the page where you will be able to perform all the necessary actions. Apart from that, there’s a nice method FB.Api() that allows to call out all other necessary methods in GraphApi via SDK.

Please note: for successful Unity Facebook integration, check out the official website (since Zuckerberg’s team just loves to implement small changes all the time). Look here first:

  1. Getting started
  2. Getting started with Android
  3. Getting started with iOS

Now when we know how to integrate Unity with Facebook, I suggest you to integrate this social network into your project. In order to do it, click this link, download the package and import an existing project by double-clicking it.

Importing package window

When we are done, a new menu option named Facebook will appear. Open Facebook/Edit Settings:

Window for editing Facebook settings

Now we can setup our application in the Inspector:

Вікно інспектора

As you can see, we need to enter Name and App Id. That’s why now it’s time to create it in Facebook using this link.

Creating new app id

After specifying the name of our game (for purposes of this tutorial I assumed we are creating a game), we will be redirected to its page.

Hint. It’s worth mentioning that Facebook doesn’t allow you to include word “face” in the name of the apps. Why “face” and not “facebook”? I have no idea. But mind it, when choosing a name for your app.

SDK implementation

After copying AppID in the corresponding field of the editor and specifying the name of your app you can try to test samples provided with SDK by simply drag-and-dropping all available scenes:

Drag-and-dropping scenes

After pressing Play (current scene should be Main menu) we’ll see:

Main menu

Here we can try all the SDK capabilities.

Android

Note that we need to set some additional parameters for mobile platforms. Let’s see how it works with Android OS.

To create an Android build, simply change the target platform. You can do it in File/Build Settings…/Android/Player Settings…

Creating build for Android

Press Player Settings to edit build settings for the chosen platform. Here we need to specify Bundle Identifier (you can use any name you want) and a minimum API level (Facebook SDK requires 15).

Let’s return to Facebook/Edit Settings — here we see that all our changes have been saved.

Saved Facebook settings

Now we need to return to the game settings page in Facebook and enter Package name, Class name and Debug name. In order to do it, let’s open Settings tab and add platform (if it’s not already available) and copy data from the editor into the corresponding fields you see on the image below.

App settings page in Facebook

The panel with all the necessary options (like Package Number, Class Name and Key Hashes) appears after we choose the right platform (it’s Android in our case):

Panel with Google Play Package Name and Key Hashes

That’s about all you need to know about how to integrate Facebook in Unity. Good luck with your Facebook builds!

“So long and thanks for all the fish”

Part 2. Integrating Unity to Facebook: authorization and news sharing

Need MVP development, iOS and Android apps or prototyping? Check out our portfolio and make an order today!