问题
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.
回答1:
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());
}
}
回答2:
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");
回答3:
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());
}
回答4:
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;
}
回答5:
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);
}
来源:https://stackoverflow.com/questions/19756453/how-to-get-users-profile-picture-with-facebooks-unity-sdk