General Features

This document provides the API details for most common Facebook SDK methods. The architecture follows, as much as possible within the constraints of the supported platforms and of Mono. Most data exchanged over the network is marshaled as strings containing JSON, and a utility class, Json, is provided to help manage that data within Unity.


Sets the state of the Facebook SDK, and initializes all platform-specific data structures and behaviors. This function can only be called once during the lifetime of the object; later calls lead to undefined behavior. Init() relies on properties that are set in the editor using the Facebook | Edit settings... menu option.

Note: After initialization is complete, user can already be authenticated, in case user have passed the Facebook authentication with your app in previous game sessions.  So you should keep this in mind and check is user is authenticated after initialization is complete.


SPFacebook.instance.OnInitCompleteAction += OnInit;
SPFacebook.instance.OnFocusChangedAction += OnFocusChanged;

private void OnInit() {
    Debug.Log("FB init finished");
	if(SPFacebook.instance.IsLoggedIn) {
		//User alreayd authenticated, update your UI accordingly
private void OnFocusChanged(bool focus) {
	if (!focus)  {                                                                                  
		// pause the game - we will need to hide                                             
		Time.timeScale = 0;                                                                  
	} else  {                                                                                       
		// start the game back up - we're getting focus again                                
		Time.timeScale = 1;                                                                  



Prompts the user to authorize your application using the Login Dialog appropriate to the platform. If the user is already logged in and has authorized your application, checks whether all permissions in thescope parameter have been granted, and if not, prompts the user for any that are newly requested. Usually, you'll call it once to ask the user for authentication, then again to request additional permissions as required.

Note: OnAuthCompleteAction contains  FBResult object. But the best way to check if the user logged in successfully is to check SPFacebook.instance.IsLoggedIn flag.


SPFacebook.OnAuthCompleteAction += OnAuth;

private void OnAuth(FB_Result result) {
	if(SPFacebook.instance.IsLoggedIn) {
		IsAuntificated = true;
		SA_StatusBar.text = "user Login -> true";
	} else {
		Debug.Log("Failed to log in");



Logout is much simpler, just call this method:



Note: We can also get current FB API state using following properties:

public bool IsInited {get;}
public bool IsLoggedIn {get;}
public string UserId {get;}
public string AccessToken {get;}
public string AppId {get;}
public string userInfo {get;}
public string userInfo {get;}


Retrieving user data

Following code snippet will show user data retrieving example:

SPFacebook.instance.OnUserDataRequestCompleteAction += OnUserDataLoaded;

private void OnUserDataLoaded(FB_Result result) {
	SPFacebook.instance.OnUserDataRequestCompleteAction -= OnUserDataLoaded;

	if (result.Error == null)  { 
		SA_StatusBar.text = "User data loaded";
		IsUserInfoLoaded = true;

		//user data avaliable, we can get info using
		//SPFacebook.instance.userInfo getter
		//and we can also use userInfo methods, for exmple download user avatar image

	} else {
		SA_StatusBar.text ="Opps, user data load failed, something was wrong";
		Debug.Log("Opps, user data load failed, something was wrong");


The user data is represented as FB_UserInfo class.

public string Id {get;}
public string Name {get;}
public string FirstName {get;}
public string LastName {get;}
public string UserName{get;}
public string ProfileUrl {get;}
public string Email {get;}
public string Locale {get;}
public string Location {get;}
public FB_Gender Gender {get;}
public string RawJSON {get;}
public DateTime Birthday {get;}

public string GetProfileUrl(FacebookProfileImageSize size)
public Texture2D  GetProfileImage(FacebookProfileImageSize size)
public void LoadProfileImage(FacebookProfileImageSize size)

public enum FacebookProfileImageSize {



The code snippet bellow will shop example of retrieving user friends list.

SPFacebook.OnFriendsDataRequestCompleteAction += OnFriendsDataLoaded;

int limit = 5;

private void OnFriendsDataLoaded(FB_Result res) {
	SPFacebook.OnFriendsDataRequestCompleteAction -= OnFriendsDataLoaded;

	if(res.Error == null) {
		//friednds data avaliable, we can get it using
		//SPFacebook.instance.friendsList getter
		//and we can also use FacebookUserInfo methods, for exmple download user avatar image
		foreach(FB_UserInfo friend in SPFacebook.instance.friendsList) {
		IsFrindsInfoLoaded = true;
	} else {
		Debug.Log("Opps, friends data load failed, something was wrong");


After friends data is loaded we can use following methods of SPFacebook class to get it.

public FacebookUserInfo GetFriendById(string id)
public List<FB_UserInfo> friendsList {get;}



To post the feed from the user you can use SPFacebook.instance.Post(.....) method it prompts a user to post a feed story. It can be posted to either his/her own timeline (if toId is blank, or set to the user's own Facebook user ID), or to that of one friend (if toID is set to the user ID of that friend). The mettod is similard to FB.Feed method of unity Facebook SDK.

The SPFacebook.instance.PostWithAuthCheck(....) will do the same, but it will run auth request if the user wasn't authed when the PostWithAuthCheck was called. If user was authored it will work the same as  Post  method.

Code snippet:

private void PostWithAuthCheck() {
		SPFacebook.instance.FeedShare (
			link: "",
			linkName: "The Larch",
			linkCaption: "I thought up a witty tagline about larches",
			linkDescription: "There are a lot of larch trees around here, aren't there?",
			picture: ""


As you noticed we can provide image only as URL.  If you wnat to post Texture2D to the user wall, there is 2 options for this.

1) With me/photos Graph API. 

Code snippet:

SPFacebook.instance.PostImage("My app ScreehShot", tex);

The posting will be made in the background, so if you want to ask user permission for this you need to implement posting dialog by yourself.

Note: A user access token with user_photos permission is required.


2) With Native system API

SPShareUtility.FacebookShare("This is my text to share", ImageToShare);

This will create native device FB post dialog. But do not forget to read Coding Guidelines and Platform Behavior differences before using native system API.


To find out posting result, we can subscribe to the OnPostingCompleteAction action.

SPFacebook.OnPostingCompleteAction += OnPost

private void OnPost(FB_PostResult res) {
	if(res.IsSucceeded) {
		Debug.Log("Posting complete");
		Debug.Log("Posy id: " + res.PostId);
		SA_StatusBar.text = "Posting complete";
	} else {
		SA_StatusBar.text = "Oops, post failed, something was wrong";
		Debug.Log("Oops, post failed, something was wrong");


if the result.Error == null it means that no issue occurred during posting flow. And even if the user will cancel the post, we will not get the error. If you want to be sure that the user has posted a feed on his wall to need to parse the result.Text JSON data and check if it contains the post id.


App Request

Prompts the user to send app requests, short messages between users. Please see the facebook official documentation for details.

Code snippet:

SPFacebook.instance.AppRequest("Come play this great game!");

Warning: The recipient will be able to get the notification only if you also have canvas version of your app, which mean your app will have canvas URL since this is the URL where the notification links to. You just need to go to the Facebook developer and select your app and goes into the setting of your app and click "Add Platform" and select "App On Facebook" and then add canvas URL in it. now try to send the invitation and it will show the notification.

Note for IOS:

To ensure this works correctly, you need to supply an iPhone and/or iPad Store ID, and you need to ensure that both Single Sign On and Deep Linking are enabled for iOS under App Settings for your app.

Full API use example can be found under the Facebook example scene.