Facebook SDK
排行榜
1) 導入Facebook SDK for Unity至Unity專案。
2) 至Facebook Developers新增一個App
- Google Play Package Name/Bundle ID:tw.tonypai.appname (舉例)
- Class Name:com.facebook.unity.FBUnityPlayerActivity
- Key Hashes:DebugAndroidKeyHash、KeyHash
3) 常用語法如下:
// 初始化
FB.Init(
delegate() {
Debug.Log ("FB SDK Inited!");
},
delegate(bool isGameShown) {
Time.timeScale = (!isGameShown)?0:1;
});
// 登入
FB.Login(
"publish_actions,user_friends",
delegate(FBResult r) {
if(FB.IsLoggedIn) {
OnLoggedIn ();
}
}
);
// 更新分數
var query = new Dictionary<string, string>();
query["score"] = score;
FB.API(
"/me/scores",
Facebook.HttpMethod.POST,
delegate(FBResult r) {
Debug.Log (r.Text);
},
query
);
// 讀取自己的分數
FB.API(
"/me/scores",
Facebook.HttpMethod.GET,
delegate(FBResult r) {
Debug.Log (r.Text); // return json array
}
);
// 讀取所有玩家分數
FB.API(
"/"+FB.AppId+"/scores",
Facebook.HttpMethod.GET,
delegate(FBResult r) {
Debug.Log (r.Text); // return array
}
);
// 分享螢幕截圖
var snap = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
snap.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
snap.Apply();
var screenshot = snap.EncodeToPNG();
var wwwForm = new WWWForm();
wwwForm.AddBinaryData("image", screenshot, "screenshot.png");
wwwForm.AddField("message", "Check out my best record!");
FB.API("/me/photos", Facebook.HttpMethod.POST, delegate(FBResult r) { Debug.Log(r.Text); }, wwwForm);
4) 在Editor測試時,按下Login按鈕後會跳出一視窗,點擊左下角按鈕取得Access Token,填入後即可GET資料,POST需到手機上才能測試。
權限請求
debug.keystore file is missing (for windows)
生成 debug.keystore
keytool -exportcert -alias androiddebugkey -keystore %homepath%\.android\debug.keystore | openssl sha1 -binary | openssl base64
修改 debugkeystorepath
facebookandroidutil.cs
get
{
return (application.platform == runtimeplatform.windowseditor) ?
system.environment.getenvironmentvariable("homedrive") + system.environment.getenvironmentvariable("homepath") + @"\.android\debug.keystore" :
system.environment.getfolderpath(system.environment.specialfolder.personal) + @"/.android/debug.keystore";
}
keyhash for facebook android application
- 將打包好的APK檔解壓縮並找到檔案 meta-inf\cert.rsa
- 執行指令"keytool -printcert -file cert.rsa"
- 會印出像是29:37:f1:cb:06…的sha1 bytes
- 將其複製起來貼至hex to base64 converter轉換
- 就可以取得base64 keyhash了
unable-to-verify-assembly-data-you-must-provide-an-authorization-key-when-loading-this-assembly
這應該在後面的改版就會修正了,因為當時Unity 4.6版還在Beta,所以FBSDK並沒有這麼快跟上。
FB.cs:381
// 修改前
#if UNITY_4_5
// 修改後
#if UNITY_4_5 || UNITY_4_6