Wednesday, December 23, 2015

unity - FB.Init() is not accepting in my c# Script



I am trying to connect fb with my game. its first time i am connecting fb with game. i downloaded fb SDk 7.3.0 and i am using unity 5.2.1f1. i imported the sdk in my game asset. then i created a new c# script "FBholder". in Awake() i tried to use FB.Init(). but its not accepting. all other scripts used inside the game are calling correctly, only the scripts came with fb sdk is not working.


 using UnityEngine;
using System.Collections;

public class Fbholder : MonoBehaviour {

// Use this for initialization
void Awake() {
FB.Init (SetInit, OnHideUnity);// This line is not accepting
}

}

i Used this Video for my referance



Answer



You have not included the correct namespace for FB.


Write using Facebook.Unity; at top of your class.


Here is the sample script for using Facebook with common features, such as posting score, with latest SDK.


using Facebook.Unity;

List perms = new List (){"public_profile", "email", "user_friends"};


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;
}
}

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


// On Facebook login button
public void OnFacebook ()
{
FB.LogInWithReadPermissions (perms, AuthCallback);
}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...