Facebook SDK for Unity - Examples

Read our Getting Started guide to learn how to load and configure the Facebook SDK for Unity. Next try these examples using the SDK:

Initialize the SDK

Use FB.Init to initialize the Facebook SDK for Unity. Consider using the Awake function from Unity's Monobehavior class as a starting place. In the callback from FB.Init, check FB.IsInitialized to verify FB.Init succeeded and if so, make a call to FB.ActivateApp to signal an app activation.

// Include Facebook namespace
using Facebook.Unity;

// Awake function from Unity's MonoBehavior
void Awake ()
    if (!FB.IsInitialized) {
        // Initialize the Facebook SDK
        FB.Init(InitCallback, OnHideUnity);
    } else {
        // Already initialized, signal an app activation App Event

private void InitCallback ()
    if (FB.IsInitialized) {
        // Signal an app activation App Event
        // Continue with Facebook SDK
        // ...
    } else {
        Debug.Log("Failed to Initialize the Facebook SDK");

private void OnHideUnity (bool isGameShown)
    if (!isGameShown) {
        // Pause the game - we will need to hide
        Time.timeScale = 0;
    } else {
        // Resume the game - we're getting focus again
        Time.timeScale = 1;

Facebook Login

Use FB.LogInWithReadPermissions to prompt the user to login with Facebook, requesting the email and user_friends permissions. In the callback, check FB.IsLoggedIn to see if the login succeeded and if so, print information about the logged in session from the current AccessToken.

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

private void AuthCallback (ILoginResult result) {
    if (FB.IsLoggedIn) {
        // AccessToken class will have session details
        var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
        // Print current access token's User ID
        // Print current access token's granted permissions
        foreach (string perm in aToken.Permissions) {
    } else {
        Debug.Log("User cancelled login");

Share to Facebook

Use FB.ShareLink to give the user an opportunity to share content to Facebook.

    new Uri("https://developers.facebook.com/"),
    callback: ShareCallback

private void ShareCallback (IShareResult result) {
    if (result.Cancelled || !String.IsNullOrEmpty(result.Error)) {
        Debug.Log("ShareLink Error: "+result.Error);
    } else if (!String.IsNullOrEmpty(result.PostId)) {
        // Print post identifier of the shared content
    } else {
        // Share succeeded without postID
        Debug.Log("ShareLink success!");

Log an App Event

Use FB.LogAppEvent to log the completion of a step in your tutorial.

var tutParams = new Dictionary<string, object>();
tutParams[AppEventParameterName.ContentID] = "tutorial_step_1";
tutParams[AppEventParameterName.Description] = "First step in the tutorial, clicking the first button!";
tutParams[AppEventParameterName.Success] = "1";

FB.LogAppEvent (
    parameters: tutParams

View these events reported in aggregate with valuable demographic info about your players on Facebook Analytics for Apps.