Facebook Plugin Implementation For Unity

READ WHOLE ARTICLE

Facebook plugin implementation for Unity

Facebook plugin for Unity is a must in integrating social features into Unity multi-platform games quickly and simply. It enables using a wide range of Facebook social options, no matter it’s a desktop, iOS or Android device. Maintaining a single codebase, developers create socially integrated incredible player experiences with sharing information with friends, logging in and out of the game and publishing game materials on Facebook. Also, it’s easy to track the effectiveness of application advertising, the installation and opening of applications, implement publishing tools and integrate the function of entering the application through Facebook.

Facebook offers official SDKs for iOS, Android, Unity, JavaScript and PHP, however, besides them, there are many unofficial third-party SDKs for other languages ​​and frameworks. In this article, you will see a step-by-step guideline for implementing Facebook SDK for Unity 3D.

The implementation of facebook plugin into your app consists of 3 major steps:

  1. Creating facebook application;
  2. Installing facebook SDK into your Unity app;
  3. Using facebook plugin in your app.

Application creation

Firstly, you need to navigate to facebook for developers (https://developers.facebook.com) and click on My Apps -> Create App..

create app facebook id

Here, in your facebook app dashboard, you need to fill in all information about your app: link your payment information, register your business, etc. To set up Facebook SDK to Unity, however, we need only ID and client token, from settings -> advanced.

facebook for developers

Sdk implementation

Now let’s refer to Unity. After project is created, we need to install Facebook SDK. Navigate to Docs->Unity SDK in facebook developers, from there you can download Facebook plugin.

Double click on plugin file with already opened Unity on background and click Import button.

import facebook unity sdk

After plugin is imported, navigate to Facebook -> Edit Settings and fill out App name, ID and client token fields.

token facebook unity sdk

Example of using facebook sdk

Let me show how I work with Facebook plugin on 2 examples. In our test project we will get photo and name of logged user and display them ingame.

Here is a small scene I prepared:

small scene facebook app

Here we have – a button, a text field and an image. On button click our application needs to authenticate user on Facebook, then retrieve photo and the name of the logged user and display them on the screen.

Let’s navigate to our script and analyze first block:

first bock facebook sdk

Here we define Image and Text objects, that are needed to display players information. Also, we init Facebook SDK in method, called Awake. Awake is a Monobehaviour method, that is called as soon as script is loaded ingame. This is first method that will be called in this script, so we init SDK here.

init sdk facebook

Method Login, showed here, is called when a player clicks on Login To Facebook button. 

Here we check wherever FB SDK is initialized and ready to use and the using FB.LogInWithReadPermissions to login into facebook. The null statement can be replaced with list of specific permissions, that a player needs to grant for application. This is used in case of wanting to retrieve some information like friends list, etc. In our case, permissions are not needed. Facebook Response is a callback – method that will be called after FB.LogInWithReadPermissions send information back to our game.

back to game facebook sdk

Here is that callback. After login is successful (checking that in IF statement), we call LoadPhoto coroutine to retrieve account photo and using FB.API method to retrieve name.

On a side note, after player is logged to facebook he retrieves specific AccessToken, that can be stored in-game to autologin next times until that token expires. This token and some other info (e.x. like facebook user ID and expiration time) are stored in AcessToke.CurrentAccessToken. facebook SDK class.

Let’s navigate to coroutine. When you call a function, it runs to completion before returning. This effectively means that any action taking place in a function must happen within a single frame.

load photo facebook sdk

A coroutine is like a function that has the ability to pause execution and return control to Unity but then to continue where 

it left off on the following frame. We need a user coroutine here, because we are waiting for a callback from retrieving photo from Facebook. After it’s retrieved and no errors are present, we load that photo into image.

And now we will retrieve player’s name:

single frame facebook sdk

Facebook responses to our request in JSON strings. Via debug log of response, we can see what exactly Facebook sends us and prepare specific Class to retrieve information from that answer.

facebook sdk data
Here we can see that Facebook responds with first_name field containing name “Vadim” and ID filed, containing facebook ID of logged user (me).

Keeping this in mind, we create class with two string fields (string because in JSON they are displayed in “”), one called first_name, other id. Note that these names should be the same as in JSON, or deserialization will fail.

Now let’s use Unity’s JsonUtility class to retrieve info we need in Facebook’s response and display it in text Field.

Let’s try to log in:

login facebook sdk

On button press we have login prompt (note in android and iOS they will be displayed correctly, this strange window is Unity only).

access token facebook sdk

Lets click Find Access Token:

find token facebook sdk

Let’s get back to facebook developers. We need to copy User Token and place it back into Facebook prompt, after that – press Send Success.

send success facebook sdk

As a result we have successfully retrieved photo and name from Facebook!

success login facebook sdk

Final Words

Facebook plugin gives many benefits to your Unity game. Players aren’t annoyed by fulfilling the login name, etc., can share their results via Facebook and invite people from their friends list. This is a helpful way to promote your game and attract more players. This Facebook SDK tutorial is appropriate for implementing the plugin on any platform, including Android and Iphone devices. It consists of 3 simple steps which are personally experienced and used in various Multi-Programming Solutions Unity games. And I ensure that it’s easy and fast method to implement Facebook plugin to your Unity game. 

 

TechnologiesAugust 19, 2019
Have a project in mind?
We are ready to do it!
LET’S TALK ABOUT IT
Do you like this article?Please rate
4 stars
E-Learning
Top 10 Key Reasons to Outsource eLearning Content
Every self-respecting business owner, at some point, thinks of improving employees' knowledge, mastery, and confidence. They may already succeed in creating an eLearning platform for corporate training. However, it still needs constant updating to keep the staff up with time and deliver the most relevant data. While developing online lessons is a lengthy process requiring experience and tech power, distributing tasks can become the only solution for companies searching for quick and efficient results.
Technologies
How To Develop Mobile-Friendly LMS Platforms: Well-Known Practices with Examples
Today’s busy and tech-savvy online customers are interested in mobile-friendly educational courses suitable for smartphones, tablets, and other gadgets. User-centric and accessible, gamified and bite-sized, mLearning can happen anytime, anywhere. You should learn how to implement it in the organization to make it a part of your corporate culture.
Technologies
Top-10 Learning Management System Trends for 2024
If you practice edtech in your organization, then you might already understand the crucial role of digital learning platforms in constant employee growth. Corporate training has a direct impact on your company’s productivity.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More