Facebook Unity SDK小雜記:
記錄一些關於Facebook接SDK時的小心得

大頭照抓取:
https://graph.facebook.com/你要抓取大頭照的FBID/picture
如果嫌圖片太小,可以在補上?type=圖片型別
圖片型別有:small, normal, album, large, square
依需求去使用吧
範例:
https://graph.facebook.com/500199733523598/picture?type=normal
可以直接貼到網誌列上面看看歐!!!


抓取App好友ID與名字:
需特別注意的是!
我的好友不是有一萬多個嗎?
怎麼拿到的Data裡面空空如也...
難道我被大家黑名單了...(覺得難過..
後來研究了一下,
下面方法只能抓到同樣有授權給App的好友,

需要在登入時要求user_friends的權限,
FB.API("me/friends?id,name", Facebook.HttpMethod.GET,OnGetAppFriendInfo);
//部分程式碼:
void OnGetAppFriendInfo(FBResult result)
{
    if (result.Error != null)
    {
        Debug.Log("Problem with getting user data");
        return;
    }
    JSONObject jo = new JSONObject(result.Text);
    List<JSONObject> l = jo.GetField("data").list;
    for(int i =0;i<l.Count;i++)
    {

        l[i].GetField("id").str; //ID
        UnicodeNameToString( l[i].GetField("name").str); //Name
    }
}
//----------

題外話:當名字是中文時,傳下來的資訊為unicode...
沒意外如果名字為中文字時,
拿到的name長類似這樣:"\u60b2\u5287\u554a"
因為unity會將\轉成\\,
也就是說...
程式碼裡讀到的會是:"\\u60b2\\u5287\\u554a"
因此...
參考下面程式碼再將unicode轉回中文字吧!

string UnicodeNameToString(string unicode)
{
    string[] sp = new string[]{"\\u"};
    string[] s = unicode.Split(sp,System.StringSplitOptions.None);
    string name ="";
    for(int i =0;i<s.Length;i++)
    {
        if(s[i].Length ==4)
            name += ((char)System.Convert.ToInt32(s[i], 16)).ToString();
        else
            name +=s[i];
    }
    return name;
}

arrow
arrow

    WilsonYo 發表在 痞客邦 留言(0) 人氣()