はじめに
- Unity, NGUI(v3.5.6), FacebookSDK, JSONObject がインストールされている環境で、Facebook のフレンドのプロフィール画像をダウンロードして表示したメモ
コード
public void ShowFBFriendsProfileImageOnClick () {
string query = "SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1";
string url = "/fql?q=" + WWW.EscapeURL (query);
FB.API (url, Facebook.HttpMethod.GET, (result) => {
if (result.Error != null) {
Debug.Log(result.Error);
return;
}
Debug.Log(result.Text);
JSONObject json = new JSONObject(result.Text);
List<JSONObject> friends = json.GetField("data").list;
foreach (var friend in friends) {
string uid = friend.GetField("uid").str;
string name = friend.GetField("name").str;
string picture = friend.GetField("pic_square").str;
picture = picture.Replace("\\/", "/");
Debug.Log(string.Format("== FRIEND == UID:{0} NAME:{1} PICTURE:{2}", uid, name, picture));
StartCoroutine(DoWWW(new WWW(picture), (www) => {
Texture2D t = www.texture;
GameObject go = new GameObject("PROFILE Texture");
UITexture uit = go.AddComponent<UITexture>();
uit.mainTexture = t;
}));
}
});
}
private IEnumerator DoWWW (WWW www, WWWCallback callback) {
yield return www;
bool error = false;
if (!string.IsNullOrEmpty (www.error) || string.IsNullOrEmpty (www.text)) {
error = true;
}
if (!error && callback != null)
callback (www);
}
private delegate void WWWCallback (WWW www);