Search Unity

Unity Facebook SDK build malfunctioning - Unity 5.1, FB SDK 6.2.2

Discussion in 'Editor & General Support' started by kenmarold, Jul 17, 2015.

  1. kenmarold

    kenmarold

    Joined:
    Jun 11, 2015
    Posts:
    27
    I'm having some trouble with a Facebook Login/Share POC I've put together for the Unity Web Player. When I run it in the editor everything runs fine. I can login, retrieve profile name and picture and share to wall. When I create a build for Web Player however, nothing functions. Clicking login does nothing and all UI elements show up at start even though in code I've set many to be inactive until the login button is clicked. Would really appreciate some help on this, I have no idea why it's getting built incorrectly. https://locomoku.com/projects/facebook/

    public GameObject UIFBIsLoggedIn;
    public GameObject UIFBNotLoggedIn;
    public GameObject UIFBAvatar;
    public GameObject UIFBUserName;

    private Dictionary<string, string> profile = null;


    void Awake()
    {
    FB.Init(SetInit, OnHideUnity);
    }

    private void SetInit()
    {
    Debug.Log("FB Init Done");

    if(FB.IsLoggedIn)
    {
    Debug.Log("FB Logged in");
    DealWithFBMenus(true);
    }
    else
    {
    DealWithFBMenus(false);
    }
    }

    private void OnHideUnity(bool isGameShown)
    {
    if(!isGameShown)
    {
    Time.timeScale = 0;
    }
    else
    {
    Time.timeScale = 1;
    }
    }

    public void FBLogin()
    {
    FB.Login("public_profile, user_friends", AuthCallback);
    }

    void AuthCallback(FBResult result)
    {
    if(FB.IsLoggedIn)
    {
    Debug.Log("FB Login Worked");
    DealWithFBMenus(true);
    }
    else
    {
    Debug.Log("FB Login Fail");
    DealWithFBMenus(false);
    }
    }

    void DealWithFBMenus(bool isLoggedIn)
    {
    if(isLoggedIn)
    {
    UIFBIsLoggedIn.SetActive(true);
    UIFBNotLoggedIn.SetActive(false);

    // Get Profile Picture Code
    FB.API(Util.GetPictureURL("me",128,128),Facebook.HttpMethod.GET,DealWithProfilePicture);

    // Get User Name Code
    FB.API("/me?fields=id,first_name", Facebook.HttpMethod.GET, DealWithUserName);

    }
    else
    {
    UIFBIsLoggedIn.SetActive(false);
    UIFBNotLoggedIn.SetActive(true);
    }
    }

    void DealWithProfilePicture(FBResult result)
    {
    if (result.Error == null)
    {
    Image img = UIFBAvatar.GetComponent<Image>();
    img.sprite = Sprite.Create(result.Texture, new Rect(0,0, 128, 128), new Vector2());
    }
    else{
    Debug.Log("Error with Profile Picture");
    }
    }

    void DealWithUserName(FBResult result)
    {
    if (result.Error == null)
    {
    profile = Util.DeserializeJSONProfile(result.Text);
    Text userMsg = UIFBUserName.GetComponent<Text>();
    userMsg.text = "Hello, " + profile["first_name"];
    }
    else{
    Debug.Log("Error with Profile Name");
    }

    }

    public void ShareWithFriends()
    {
    FB.Feed(
    linkCaption: "Test Captions",
    picture: "https://locomoku.com/projects/facebook/shot.jpg",
    linkName: "Test Link Name",
    link: "http://locomoku.com"
    );
    }

    -------------------------------------------------------------------------------------------------------------------
    The Util.cs code
    -------------------------------------------------------------------------------------------------------------------

    public static string GetPictureURL(string facebookID, int? width = null, int? height = null, string type = null)
    {
    string url = string.Format("/{0}/picture", facebookID);
    string query = width != null ? "&width=" + width.ToString() : "";
    query += height != null ? "&height=" + height.ToString() : "";
    query += type != null ? "&type=" + type : "";
    if (query != "") url += ("?g" + query);
    return url;
    }

    public static void FriendPictureCallback(FBResult result)
    {
    if (result.Error != null)
    {
    Debug.LogError(result.Error);
    return;
    }

    //GameStateManager.FriendTexture = result.Texture;
    }

    public static Dictionary<string, string> RandomFriend(List<object> friends)
    {
    var fd = ((Dictionary<string, object>)(friends[Random.Range(0, friends.Count - 1)]));
    var friend = new Dictionary<string, string>();
    friend["id"] = (string)fd["id"];
    friend["first_name"] = (string)fd["first_name"];
    return friend;
    }

    public static Dictionary<string, string> DeserializeJSONProfile(string response)
    {
    var responseObject = Json.Deserialize(response) as Dictionary<string, object>;
    object nameH;
    var profile = new Dictionary<string, string>();
    if (responseObject.TryGetValue("first_name", out nameH))
    {
    profile["first_name"] = (string)nameH;
    }
    return profile;
    }

    public static List<object> DeserializeScores(string response)
    {

    var responseObject = Json.Deserialize(response) as Dictionary<string, object>;
    object scoresh;
    var scores = new List<object>();
    if (responseObject.TryGetValue ("data", out scoresh))
    {
    scores = (List<object>) scoresh;
    }

    return scores;
    }

    public static List<object> DeserializeJSONFriends(string response)
    {
    var responseObject = Json.Deserialize(response) as Dictionary<string, object>;
    object friendsH;
    var friends = new List<object>();
    if (responseObject.TryGetValue("friends", out friendsH))
    {
    friends = (List<object>)(((Dictionary<string, object>)friendsH)["data"]);
    }
    return friends;
    }



    public static void DrawActualSizeTexture (Vector2 pos, Texture texture, float scale = 1.0f)
    {
    Rect rect = new Rect (pos.x, pos.y, texture.width * scale , texture.height * scale);
    GUI.DrawTexture(rect, texture);
    }
    public static void DrawSimpleText (Vector2 pos, GUIStyle style, string text)
    {
    Rect rect = new Rect (pos.x, pos.y, Screen.width, Screen.height);
    GUI.Label (rect, text, style);
    }
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I don't think the Facebook SDK works with the WebPlayer platform.
    What we did was create our own wrapper around the Javascript Facebook SDK and call into that from Unity.
     
  3. kenmarold

    kenmarold

    Joined:
    Jun 11, 2015
    Posts:
    27
    Hmm...It says it does here https://developers.facebook.com/docs/unity

    "The Facebook SDK for Unity complements Unity Technologies' seamless cross-platform support, providing a pure-Unity write-once, complete-everywhere experience across the key gaming platforms of Unity Web Player, Android and iOS. By maintaing a single codebase, you'll be able to deploy socially integrated gaming experiences to your players, regardless of platform."

    Am I misreading this? I'm developing an app to run in the Unity Web Player. I was under impression that the Facebook SDK would work in this situation?

    How did you go about creating the wrapper? Is that something you'd be willing to share?
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I don't recall why we created that wrapper then. It must've come up because of some limitation with the FB SDK on the web.

    Unfortunately, i cannot share the code for this wrapper as it's owned by our company...
     
  5. kenmarold

    kenmarold

    Joined:
    Jun 11, 2015
    Posts:
    27
    Would your company be available to hire to integrate some basic facebook functionality into some web player apps I'm developing?
     
  6. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Contact me in private and we can discuss what you need exactly.