如何使用Facebook的Unity SDK获取用户的个人资料照片?

时间:2013-11-03 18:32:35

标签: android unity3d facebook-unity-sdk

我正在尝试使用此功能获取游戏用户的个人资料照片 -

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

但它不起作用。我没有错误。

5 个答案:

答案 0 :(得分:7)

Jason Pietka的答案还可以,但有点旧。 今天我们FB.API

FB.API("me/picture?type=med", Facebook.HttpMethod.GET, GetPicture);

GetPicture是一种回调方法,所以:

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

}

答案 1 :(得分:6)

我用它修复了它 -

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

答案 2 :(得分:2)

使用Facebook SDK 7.2.2,如果您需要/需要特定大小的方形图片,这可以正常工作。在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());
}

答案 3 :(得分:1)

以下是用于下载个人资料图片的Facebook SDK 7.10.1的测试代码

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

答案 4 :(得分:0)

我得到了一些结果。纹理总是空的..

所以我使用了https://github.com/fbsamples/friendsmash-unity

中的代码

如果直接编译,即使这段代码也有一些问题。

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