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

自作多情 提交于 2019-12-04 07:12:11

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

}

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

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

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

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!