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.

Fabian Barney
  • 14,219
  • 5
  • 40
  • 60
Jason Pietka
  • 181
  • 1
  • 3
  • 16

5 Answers5

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

}
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
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");
Jason Pietka
  • 181
  • 1
  • 3
  • 16
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());
}
Johan Danforth
  • 4,469
  • 6
  • 37
  • 36
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;
        }
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);
}
Amarnath
  • 51
  • 1
  • 3
    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. – peterept Jun 25 '15 at 01:16