简体   繁体   中英

Cannot Get Facebook Profile Picture Using www in Unity Facebook SDK

I'm trying to get profile picture from graph.facebook.com using Facebook SDK for Unity 7.3.0. My Unity version is 5.3.

This is my function,

public static IEnumerator GetFBProfilePicture (){
    WWW url = new WWW (System.Uri.EscapeUriString("https://graph.facebook.com/" + someUserID + "/picture?type=large"));
    yield return url;
    Debug.Log("Completed.");
    Texture2D texture = new Texture2D (180, 180, TextureFormat.DXT1, false);
    url.LoadImageIntoTexture (texture);
    // ...

}

and I call this function like

    StartCoroutine (GetFBProfilePicture ());

It works fine in Unity Player and also in Android devices. But in iOS devices, "Completed." line doesn't show up. And there is no error log. It just keep waiting in the url line.

I tried it with iOS 7 and 9 with wireless connection and mobile data. Problem still occurs.

I just had this same problem, and finally found a solution (albeit, not an ideal one). Making the request with the parameter redirect=false seems to do the trick, but then it only returns the url for the image, not the image itself. So you'll have to make a second request for the image. The following code outlines the solution:

//passed in URL looks like https://graph.facebook.com/some_id/picture?type=large
IEnumerator GetDownloadURL(string url, float pixelsPerUnit = 100.0f ) {
    url = url + "&redirect=false";
    WWW www = new WWW(url);
    yield return www;
    Dictionary<string,object> dict = fastJSON.JSON.Parse(www.text) as Dictionary<string,object>;
    Dictionary<string,object> dataDict = dict["data"] as Dictionary<string,object>;
    DownloadImage(dataDict["url"])
}

IEnumerator DownloadImage(string url ) {
    WWW www = new WWW(url);
    yield return www;
    Texture2D texture = newDownload.www.texture;
    ///...
}

the known issue that recommends the solution: https://developers.facebook.com/x/bugs/364606280347510/

I use this and it works, try it

IEnumerator getPicture(string url, SpriteRenderer spritex) {
    WWW www = new WWW (url);
    yield return www;
    Sprite sprite = new Sprite ();
    sprite = Sprite.Create (www.texture, new Rect (0, 0, 50, 50), new Vector2 (0.5f, 0.5f), 100.0f);
    spritex.sprite = sprite;
    www.LoadImageIntoTexture (spritex.sprite.texture);
}

the thing is to create a new sprite, not sure why but it works like that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM