Hey, I am trying to get the Profile Picture of the Facebook friends of the user who are not yet playing the game. I have retrieved their usernames successfully and just stuck on getting the profile picture. here is the code I am using (I followed this tutorial here )
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using Facebook.Unity;
using System.Collections.Generic;
public class FBLogin : MonoBehaviour {
public GameObject login;
public GameObject notLogin;
public GameObject friend;
void Awake()
{
if (!FB.IsInitialized) {
FB.Init (InitCallBack);
}
}
void InitCallBack()
{
print ("FB has Been Initialised");
}
public void Login()
{
if (!FB.IsLoggedIn) {
FB.LogInWithReadPermissions(new List<string>{"user_friends"}, LoginCallBack);
}
}
void LoginCallBack(ILoginResult result){
if (result.Error == null) {
Debug.Log ("Fb logged in!");
login.SetActive (true);
notLogin.SetActive (false);
FB.API ("me/picture?width=100&height=100", HttpMethod.GET, PictureCallBack);
FB.API ("me?fields=first_name", HttpMethod.GET, NameCallBack);
FB.API ("me/invitable_friends", HttpMethod.GET, FriendCallBack);
} else {
login.SetActive (false);
notLogin.SetActive(true);
}
}
void PictureCallBack(IGraphResult result)
{
Texture2D image = result.Texture;
login.transform.FindChild ("Image").GetComponent<Image> ().sprite = Sprite.Create (image, new Rect (0, 0, 100, 100), new Vector2 (.5f, .5f));
}
void NameCallBack(IGraphResult result)
{
IDictionary<string,object> profile = result.ResultDictionary;
login.transform.GetChild (0).GetComponent<Text> ().text = "Hello, " + profile ["first_name"];
}
public void LogOut()
{
FB.LogOut ();
login.SetActive (false);
notLogin.SetActive (true);
}
public void Invite (){
FB.AppRequest (message: "You Really should try this game.", title: "Covens");
}
public void Share(){
FB.ShareLink (new System.Uri ("http://www.raincrowstudios.com/"), "This game is awesome!", "Discription about the game", new System.Uri ("http://www.raincrowstudios.com/images/covens_mast825.png"));
}
void FriendCallBack(IGraphResult result)
{
IDictionary<string, object> data = result.ResultDictionary;
List<object> friends = (List<object>)data ["data"];
foreach (object obj in friends) {
Dictionary<string,object> dictio = (Dictionary<string,object>)obj;
CreateFriend(dictio ["name"].ToString () , dictio ["id"].ToString());
}
}
void CreateFriend(string name, string id){
GameObject myFriend = Instantiate (friend);
Transform parent = login.transform.FindChild("users");
myFriend.transform.SetParent (parent);
myFriend.transform.localScale = new Vector3 (1, 1, 1);
myFriend.GetComponentInChildren<Text> ().text = name;
FB.API ("me/invitable_friends/access_token="+id+"?fields=picture?width=100&height=100", HttpMethod.GET, delegate(IGraphResult result) {
myFriend.GetComponent<Image> ().sprite = Sprite.Create (result.Texture, new Rect (0, 0, 100, 100), new Vector2 (.5f, .5f));
});
}
}
I tried a bunch of ways to get the profile pictures none worked, I can’t find the right syntax for it, would really appreciate it if someone can help me with this.
Thanks,
Mridul