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. 

 

For ClientsAugust 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
For Clients
Why you should use Google Trends when creating new POD product designs
If you've ever felt uncomfortable with plenty of options, then know you aren't alone. After all, the more options there are, the more likely a person won't choose anything. Print on demand products have incredible opportunities. Many things stay relevant for decades. A successful design is everything you need for creating a bestseller. But often, searching for popular products and analyzing them takes a lot of time and effort. Google Trends is a handy tool to make it easier for entrepreneurs. It allows you to find out certain product trends for key phrases. Take into account seasonality, territorial affiliation, and other parameters. We hope that the print on demand business tips collected in this article will inspire you to create original products and sales ideas [...]
For Clients
How owner’s personality influences the development of print on demand business?
Print on demand is a kind of business where a brand's recognition is everything. As a result, plenty of teams' efforts go into establishing successful print on demand stores. The team's unity, common values, shared vision, and good management equally contribute to the successful print on demand t-shirt companies. The personality highly influences the occupational choices, managerial efficiency, and flexibility of the business, as the Journal of Economic Management Sciences states. One of the attributes of successful entrepreneurs is iron nerves, which makes an individual more stress-resistant. Accordingly, print on demand business model relies heavily on its CEO and depends on the person in charge. The following material aims at estimating what makes a person an entrepreneur and how communication in the creative teams influences business success [...]
For Clients
Learning Management Systems
New technologies are the mandatory indicator of development that make all processes more efficient. Learning is not an exception. Modern learning management systems (LMS) are convenient tools for students, teachers, coaches, and other people interested in getting new knowledge [...]
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