The Unity Code Examples for the Facebook SDK guide explains Unity code examples for initializing the SDK, implement Facebook Login, share to Facebook, and App Event Logging.
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
FB.ActivateApp();
}
}
private void InitCallback ()
{
if (FB.IsInitialized) {
// Signal an app activation App Event
FB.ActivateApp();
// 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;
}
}Use FB.LogInWithReadPermissions to prompt the user to login with Facebook, requesting the public_profile and email 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"};
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
Debug.Log(aToken.UserId);
// Print current access token's granted permissions
foreach (string perm in aToken.Permissions) {
Debug.Log(perm);
}
} else {
Debug.Log("User cancelled login");
}
}Use the FB.Android.RetrieveLoginStatus to automatically log a user into your app when the user switches to a different device or platform. The user must be logged into Facebook on the device and must have previously logged into your app.
FB.Android.RetrieveLoginStatus(LoginStatusCallback);
private void LoginStatusCallback(ILoginStatusResult result) {
if (!string.IsNullOrEmpty(result.Error)) {
Debug.Log("Error: " + result.Error);
} else if (result.Failed) {
Debug.Log("Failure: Access Token could not be retrieved");
} else {
// Successfully logged user in
// A popup notification will appear that says "Logged in as <User Name>"
Debug.Log("Success: " + result.AccessToken.UserId);
}
}Use FB.ShareLink to give the user an opportunity to share content to Facebook.
FB.ShareLink(
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
Debug.Log(result.PostId);
} else {
// Share succeeded without postID
Debug.Log("ShareLink success!");
}
}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 (
AppEventName.CompletedTutorial,
parameters: tutParams
);View these events in Facebook Events Manager.