简体   繁体   中英

How to display a Facebook friends profile picture in Unity C#

So, I've been working on implementing leaderboards into my mobile app practically all day, and I've got everything working successfully except for displaying the profile pictures of your friends next to their scores.

In V6.x you could use FB.GetPictureUrl but now I expect there to be some sort of FB.API implementation that allows me to do something similar?

Anyway, here's how I'm doing things

    private void ScoresCallBack(IGraphResult result)
{
    int num = -1;

    var dataList = result.ResultDictionary ["data"] as List<object>;

    foreach (Transform child in leaderboardPanel.transform) 
    {
        GameObject.Destroy (child.gameObject);
    }

    foreach (object player in dataList) 
    {
        num++;
        var dataDict = dataList [num] as Dictionary<string, object>;

        long score = (long)dataDict ["score"];
        var user = dataDict ["user"] as Dictionary<string, object>;

        string userName = user ["name"] as string;
        string userID = user ["id"] as string;

        GameObject ScorePanel;
        ScorePanel = Instantiate (scoreEntryPanel) as GameObject;
        ScorePanel.transform.SetParent (leaderboardPanel.transform, false);
        ScorePanel.SetActive (true);

        ScorePanel.transform.GetChild(1).GetComponent<Text>().text = userName;
        ScorePanel.transform.GetChild (2).GetComponent<Text> ().text = score.ToString ();
    }
}

Oh, and the API call I'm making is

FB.API ("/app/scores?fields=score,user.limit(30)", HttpMethod.GET, ScoresCallBack);

So, thanks! Any ideas?

You can to get profile picture by

FB.Api("{facebook_id}?fields=picture", HttpMethod.GET, PictureCallBack)

Then, you need to made the http request for download texture and update it.

Hope this help!!

private void PictureCallBack(IGraphResult result) {
  JSONObject json = new JSONObject(result.RawResult);

  StartCoroutine(DownloadTexture(json["picture"]["data"]["url"].str, profile_texture));
}

IEnumerator DownloadTexture(string image_url, Image profile_picture) {
    WWW www = new WWW(url);
    yield return www;
    profile_picture = = www.texture;
}

PS. I'm not testing this code yet.

Here's how I've handled something similar. After loading all basic data(user_name, score) to prefab list items, each list item is responsible for loading its own profile picture.

In FacebookScript

public void downloadProfilePic(string user_id, FacebookDelegate<IGraphResult> callback){
     FB.API("https" + "://graph.facebook.com/" + user_id + "/picture? 
         type=square&height=128&width=128", HttpMethod.GET, callback);  
}

Then from the ListViewItem script

private void downloadProfilePic(string user_id){
    FindObjectOfType<FacebookScript>().downloadProfilePic(user_id, delegate (IGraphResult result){
        if(result.Texture){
            profileImage.sprite = Sprite.Create(result.Texture, new Rect(0, 0, 128 , 128), new Vector2());
        }
    });
}

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