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"];
});
}
}

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
thnx, you saved my job! <3
Hello Sir
help me please
not working!!
It shows FB.Mobile’ does not contain a definition for ‘AppInvite, How can I fix this problem?
Did you get it to work?
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you. https://www.binance.info/en/register?ref=JHQQKNKN
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.