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

5

6

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.

Preschool answered 3/11, 2013 at 18:32 Comment(1)
I'm getting an image , but its scrabbled.Preschool
P
6

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");
Preschool answered 3/11, 2013 at 21:8 Comment(0)
H
8

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

}
Hambletonian answered 13/6, 2015 at 7:29 Comment(1)
Facebook.HttpMethod doesn't existCapua
P
6

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");
Preschool answered 3/11, 2013 at 21:8 Comment(0)
T
4

With Facebook SDK 7.2.2, this works fine if you want/need a square picture of particular size. Read more here on Facebook: https://developers.facebook.com/docs/graph-api/reference/user/picture/

public Image ProfilePicture;
FB.API("me/picture?type=square&height=128&width=128", HttpMethod.GET, FbGetPicture);
private void FbGetPicture(IGraphResult result)
{
    if (result.Texture != null)
        ProfilePicture.sprite = Sprite.Create(result.Texture, new Rect(0, 0, 128, 128), new Vector2());
}
Tortricid answered 5/11, 2015 at 13:58 Comment(0)
G
2

Here is the tested code for Facebook SDK 7.10.1 to download profile picture

StartCoroutine(getFBPicture(AccessToken.CurrentAccessToken.UserId));

public IEnumerator getFBPicture(string facebookId)
    {
        var www = new WWW("http://graph.facebook.com/" + facebookId + "/picture?width=240&height=240&type=square&redirect=true");
        Debug.Log ("http://graph.facebook.com/" + facebookId + "/picture?width=210&height=210&type=normal&redirect=true"+"\t"+www.error);
        yield return www;

        if (www.isDone) {
            Debug.Log ("waiting" + www.bytesDownloaded);
            Texture2D tempPic = new Texture2D (25, 25);
            tempPic = www.texture;
            PlayerImage = tempPic;
        }
Gault answered 25/1, 2018 at 7:59 Comment(0)
D
1

some how i was getting result.Texture was always null..

so i have used the code from https://github.com/fbsamples/friendsmash-unity

even this code have some problems if you compile directly..

LoadPictureAPI(Util.GetPictureURL("me", 100, 100),MyPictureCallback);

delegate void LoadPictureCallback (Texture texture);


IEnumerator LoadPictureEnumerator(string url, LoadPictureCallback callback)    
{
    WWW www = new WWW(url);
    yield return www;
    callback(www.texture);
}
void LoadPictureAPI (string url, LoadPictureCallback callback)
{
    FB.API(url,Facebook.HttpMethod.GET,result =>
           {
        if (result.Error != null)
        {
            Util.LogError(result.Error);
            return;
        }

        var imageUrl = Util.DeserializePictureURLString(result.Text);

        StartCoroutine(LoadPictureEnumerator(imageUrl,callback));
    });
}
void LoadPictureURL (string url, LoadPictureCallback callback)
{
    StartCoroutine(LoadPictureEnumerator(url,callback));

}


void MyPictureCallback(Texture texture)
{
    Util.Log("MyPictureCallback");
    Image im = GetComponent <Image>();
    if (texture ==  null)
    {
        LoadPictureAPI(Util.GetPictureURL("me", 100, 100),MyPictureCallback);

        return;
    }
    Vector2 v = new Vector2 (0, 0);
    Rect r = new Rect (0f,0f,100f,100f);
    im.sprite = Sprite.Create((Texture2D)texture, r, v);
}
Dalmatia answered 24/6, 2015 at 16:42 Comment(1)
Be careful using WWW to access the users profile. Facebook's CDN will often return a URL that has a 302 redirect to the actual image URL, and WWW does not support automatic redirect. You have to detect the 302 then read the redirect URL and use that in WWW again to download it. Or use a HTTP library like Uniweb which does it for you.Liter

© 2022 - 2024 — McMap. All rights reserved.