Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get user's profile picture with Facebook's Unity SDK?

I'm trying to get the profile pic of the user of the game using this-

void MyPictureCallback(FBResult result) // store user profile pic
{
        if (FB.IsLoggedIn)
        {
            WWW url = new WWW("http" + "://graph.facebook.com/" + FB.UserId + "/picture");

            Texture2D textFb2 = new Texture2D(128, 128, TextureFormat.ARGB32, false); //TextureFormat must be DXT5

            url.LoadImageIntoTexture(textFb2);
            profilePic.renderer.material.mainTexture = textFb2;
        }

But it isn't working. I am getting no errors.

like image 303
Jason Pietka Avatar asked Nov 03 '13 18:11

Jason Pietka


People also ask

What is Facebook SDK for Unity?

The Facebook SDK for Unity complements Unity Technologies' cross-platform support, providing a pure-Unity write-once, run-everywhere experience across the key gaming platforms of WebGL, Unity Web Player, Android and iOS.


2 Answers

Jason Pietka's answer is OK but a bit old. Today we us FB.API:

FB.API("me/picture?type=med", Facebook.HttpMethod.GET, GetPicture);

GetPicture is a callback method so:

private void GetPicture(FBResult result)
{
    if (result.Error == null)
    {          
        Image img = UIFBProfilePic.GetComponent<Image>();
        img.sprite = Sprite.Create(result.Texture, new Rect(0,0, 128, 128), new Vector2());         
    }

}
like image 136
No Idea For Name Avatar answered Oct 20 '22 01:10

No Idea For Name


I fixed it with this-

WWW url = new WWW("https" + "://graph.facebook.com/" + userId + "/picture?type=large"); //+ "?access_token=" + FB.AccessToken);

            Texture2D textFb2 = new Texture2D(128, 128, TextureFormat.DXT1, false); //TextureFormat must be DXT5

            yield return url;
            profilePic.renderer.material.mainTexture = textFb2;
            url.LoadImageIntoTexture(textFb2);
            Debug.Log("Working");
like image 43
Jason Pietka Avatar answered Oct 20 '22 03:10

Jason Pietka