因為最近有工作需求要在Unity使用fb sdk,才發現目前最新版的sdk7.10.1是有問題的無法使用,所以花了一些時間下載相關版本,目前找到比較好用的版本,是facebook-unity-sdk-7.2.0
下載網址:https://developers.facebook.com/docs/unity/downloads
下載facebook-unity-sdk-7.2.0後解壓縮後於Unity中匯入FacebookSDK後,還是會有錯誤產生,網路上已有解法,以下簡單說明一下
Assets/Facebook/Editor/android/ManifestMod.cs(29,20): error CS0234: The type or namespace name `Unity' does not exist in the namespace `UnityEditor.Facebook'. Are you missing an assembly reference?
因為namespace衝突所造成
請將namespace UnityEditor.FacebookEditor換成另一個名字,網路上是換成Whatever.FacebookEditor, 請尋找專案中所有檔案全部取代成Whatever.FacebookEditor,一共有9個檔案
PS: 若跳出API Update Required請選No Thanks
Assets/Facebook/Editor/android/FacebookAndroidUtil.cs(105,20): error CS0103: The name `EditorPrefs' does not exist in the current context
點擊此錯誤會跳到該行程式碼,顯示可能的修正選using UnityEditor即可
Assets/Facebook/Editor/FacebookPostprocess.cs(54,36): error CS0619: `UnityEditor.PlayerSettings.bundleIdentifier' is obsolete: `Use PlayerSettings.applicationIdentifier instead (UnityUpgradable) -> UnityEditor.PlayerSettings.applicationIdentifier'
由此錯誤可知,將PlayerSettings.bundleIdentifier更換成UnityEditor.PlayerSettings.applicationIdentifier即可
如此一來就可以正常使用fb sdk了
於Facebook Menu中設定你在https://developers.facebook.com中app的檔案名稱和應用程式編號
於Android Build Facebook Settings中點擊Regenerate Android Manifest
將Assets/Plugins/Android/AndroidMaifest.xml中的package換成Unity Build Setting中Player Settings的Package Name
於手機上測試時,請注意手機網路是否有開啟,最近怎麼測試都無法成功,最後才發現是網路問題Orz...
附上測試程式碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Facebook.Unity;
using System;
public class FBTest : MonoBehaviour {
public void Init()
{
if (!FB.IsInitialized)
{
// Initialize the Facebook SDK
FB.Init(InitCallback, OnHideUnity);
}
else
{
// Already initialized, signal an app activation App Event
FB.ActivateApp();
}
}
private void InitCallback()
{
Debug.Log("InitCallback");
if (FB.IsInitialized)
{
// Signal an app activation App Event
FB.ActivateApp();
Debug.Log("Continue with Facebook SDK");
// Continue with Facebook SDK
// ...
}
else
{
Debug.Log("Failed to Initialize the Facebook SDK");
}
}
private void OnHideUnity(bool isGameShown)
{
Debug.Log("OnHideUnity");
if (!isGameShown)
{
// Pause the game - we will need to hide
Time.timeScale = 0;
}
else
{
// Resume the game - we're getting focus again
Time.timeScale = 1;
}
}
public void Login()
{
var perms = new List<string>() { "public_profile", "email", "user_friends" };
Debug.Log("FB login");
FB.LogInWithReadPermissions(perms, AuthCallback);
}
private void AuthCallback(ILoginResult result)
{
Debug.Log("AuthCallback");
if (FB.IsLoggedIn)
{
// AccessToken class will have session details
var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
// Print current access token's User ID
Debug.Log(aToken.UserId);
// Print current access token's granted permissions
foreach (string perm in aToken.Permissions)
{
Debug.Log(perm);
}
}
else
{
Debug.Log("User cancelled login");
}
}
public void Share()
{
Debug.Log("FB Share");
FB.ShareLink(
new Uri("https://developers.facebook.com/"),
callback: ShareCallback
);
}
private void ShareCallback(IShareResult result)
{
Debug.Log("ShareCallback");
if (result.Cancelled || !String.IsNullOrEmpty(result.Error))
{
Debug.Log("ShareLink Error: " + result.Error);
}
else if (!String.IsNullOrEmpty(result.PostId))
{
// Print post identifier of the shared content
Debug.Log(result.PostId);
}
else
{
// Share succeeded without postID
Debug.Log("ShareLink success!");
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}