Facebook Unity SDK Tutorial – Share, Invite, Get Friends (Code)

8  comments

Learn how to integrate Facebook SDK into your Unity game!

In this tutorial you will learn how to share, send game requests and invites and how to get friends who are also playing your game. Most importantly though, you will learn how to set everything up so that your game can be up and running in no time!

This post contains all the code that’s been written in this YouTube video.

 

FacebookScript.cs

using System.Collections.Generic;
using UnityEngine;
using Facebook.Unity;
using UnityEngine.UI;

public class FacebookScript : MonoBehaviour {

    public Text FriendsText;

    private void Awake()
    {
        if (!FB.IsInitialized)
        {
            FB.Init(() =>
            {
                if (FB.IsInitialized)
                    FB.ActivateApp();
                else
                    Debug.LogError("Couldn't initialize");
            },
            isGameShown =>
            {
                if (!isGameShown)
                    Time.timeScale = 0;
                else
                    Time.timeScale = 1;
            });
        }
        else
            FB.ActivateApp();
    }

    #region Login / Logout
    public void FacebookLogin()
    {
        var permissions = new List<string>() { "public_profile", "email", "user_friends" };
        FB.LogInWithReadPermissions(permissions);
    }

    public void FacebookLogout()
    {
        FB.LogOut();
    }
    #endregion

    public void FacebookShare()
    {
        FB.ShareLink(new System.Uri("https://resocoder.com"), "Check it out!",
            "Good programming tutorials lol!",
            new System.Uri("https://resocoder.com/wp-content/uploads/2017/01/logoRound512.png"));
    }

    #region Inviting
    public void FacebookGameRequest()
    {
        FB.AppRequest("Hey! Come and play this awesome game!", title: "Reso Coder Tutorial");
    }

    public void FacebookInvite()
    {
        FB.Mobile.AppInvite(new System.Uri("https://play.google.com/store/apps/details?id=com.tappybyte.byteaway"));
    }
    #endregion

    public void GetFriendsPlayingThisGame()
    {
        string query = "/me/friends";
        FB.API(query, HttpMethod.GET, result =>
        {
            var dictionary = (Dictionary<string, object>)Facebook.MiniJSON.Json.Deserialize(result.RawResult);
            var friendsList = (List<object>)dictionary["data"];
            FriendsText.text = string.Empty;
            foreach (var dict in friendsList)
                FriendsText.text += ((Dictionary<string, object>)dict)["name"];
        });
    }
}

About the author 

Matt Rešetár

Matt is an app developer with a knack for teaching others. Working as a freelancer and most importantly developer educator, he is set on helping other people succeed in their Flutter app development career.

You may also like

  • hi,
    I tried this but the other end,my friend, didn’t get notification or anything.
    thanks in advance.

  • Hello, thanks for the post
    i tried the code but it doesnt work, the button doesnt react on the android device (samsung s6). In the unity Editor, the dialog shows up but on the device nothing works.
    I use the facebook sdk v 7.10.1 and the new version of unity 5.4.5
    In roles on the facebook page, i have only my account as administrator. I ve paste all the informations from unity.
    Can you help me please ?
    Thanks

  • I am searching for, how to implement requesting in game items (coins, locked items such as specific weapons etc), I’ve spend a lot of time to find a way, but no luck,
    can you please guid me on it.
    thanks
    Pramod Kumar

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    >