海外項(xiàng)目需要facebook登錄等功能,國內(nèi)有一些集成sdk特別簡單好用绰精,但是游戲項(xiàng)目需要邀請功能撒璧,所以只能集成原生SDK了,在此記錄一下笨使。
ShareSDK http://www.mob.com/mobService/sharesdk集成sdk卿樱,包挺小的,服務(wù)也很好硫椰,客服隨時都能聯(lián)系上繁调,但是facebook打不開好友列表,很郁悶靶草,只能接原生的了
Facebook 開發(fā)者后臺地址 https://developers.facebook.com/apps/ 需在此注冊應(yīng)用
Facebook原生sdk下載地址 https://developers.facebook.com/docs/unity/downloads/ sdk需接入工程
第一步蹄胰,注冊應(yīng)用
1.注冊facebook賬戶,注冊開發(fā)者奕翔,創(chuàng)建應(yīng)用進(jìn)入下圖頁面(facebook老是被封裕寨,怎么解決...)
2.點(diǎn)擊基本填寫應(yīng)用基本信息
3.翻到頁面最下面,點(diǎn)擊添加平臺
4.以Android為例糠悯,添加完平臺帮坚,包名類名秘鑰都都在項(xiàng)目中自動生成
獲取密鑰方式在此:http://www.reibang.com/p/9557a469bcf2
第二步妻往,導(dǎo)入SDK
1.下載sdk導(dǎo)入工程
2.點(diǎn)擊設(shè)置
填寫項(xiàng)目名稱,編號
將包名试和,類名讯泣,秘鑰填寫到后臺到此sdk接入完成,剩下的就是API的應(yīng)用
下面上代碼
public delegate void OnFBLoginFaild(bool isCancel, string errorInfo);
public delegate void OnFBShareLinkSucced(string postId);
public delegate void OnFBShareLinkFaild(bool isCancel, string errorInfo);
public delegate void OnGotFBFriendInGame(string resultJsonStr);
public delegate void OnGotFBMyInfo(string resultJsonStr);
public delegate void OnGetFBInfoFaild();
internal static void GetMyInfo(Action<string> p, object v)
{
throw new NotImplementedException();
}
public delegate void OnFBInvitedSucceed(string resultJsonStr);
private static string appLinkUrl;
/// <summary>
/// 初始化
/// </summary>
public static void Init(Action action)
{
if (!FB.IsInitialized)
{
FB.Init(() =>
{
if (FB.IsInitialized)
{
// Signal an app activation App Event
FB.ActivateApp();
// Continue with Facebook SDK
// ...
FBGetAPPLinkUrl();
if (action != null)
{
action();
}
}
else
{
Debug.Log("Failed to Initialize the Facebook SDK");
}
}, OnHideUnity);
}
else
{
FB.ActivateApp();
if (action != null)
{
action();
}
}
}
private static void OnHideUnity(bool isGameShown)
{
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;
}
}
/// <summary>
/// 登錄
/// </summary>
public static void LoginResult(Action action)
{
List<string> perms = new List<string>() { "public_profile", "email", "user_friends" };
FB.LogInWithReadPermissions(perms, (result) =>
{
if (FB.IsLoggedIn)
{
// AccessToken class will have session details
var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
if (action != null)
{
action();
}
}
else
{
Debug.Log("User cancelled login");
}
});
}
public static void Share()
{
// UpdateManager.Ins.verData.data.iosId = "1459965214";
if (PlayerManager.Ins.IsFaceBook)
{
#if UNITY_ANDROID && !UNITY_EDITOR
FB.ShareLink(new Uri("https://play.google.com/store/apps/details?id=" + UpdateManager.Ins.verData.data.googleId), callback: ShareCallback);
#elif UNITY_IOS && !UNITY_EDITOR
FB.ShareLink(new Uri("https://apps.apple.com/us/app/face-meme-emoji-gif-maker/id" + UpdateManager.Ins.verData.data.iosId), callback: ShareCallback);
#endif
}
else
{
PlayerManager.Ins.FackBookInit(() =>
{
PlayerManager.Ins.Player.Inquire();
#if UNITY_ANDROID && !UNITY_EDITOR
FB.ShareLink(new Uri("https://play.google.com/store/apps/details?id=" + UpdateManager.Ins.verData.data.googleId), callback: ShareCallback);
#elif UNITY_IOS && !UNITY_EDITOR
FB.ShareLink(new Uri("https://apps.apple.com/us/app/face-meme-emoji-gif-maker/id" + UpdateManager.Ins.verData.data.iosId), callback: ShareCallback);
#endif
});
}
}
private static void ShareCallback(IShareResult result)
{
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!");
}
}
/// <summary>
/// 邀請
/// </summary>
public static void Invite()
{
//string message, IEnumerable<string> to = null, IEnumerable<object> filters = null, IEnumerable<string> excludeIds = null, int? maxRecipients = default(int?), string data = "", string title = "", FacebookDelegate<IAppRequestResult> callback = null
// FB.AppRequest(APP.facebookID,null/*to*/,null/**/,null);
//FB.AppRequest("Come play this great game!", null, null, null, null, null, null,
// delegate (IAppRequestResult result) {
// Debug.Log(result.RawResult); Debug.Log(result.RawResult);
//});
}
/// <summary>
/// 獲取自己的信息
/// </summary>
/// <param name="onGotFBMyInfo"></param>
public static void GetMyInfo(OnGotFBMyInfo onGotFBMyInfo = null, OnGetFBInfoFaild onGetFaild = null)
{
//Logger.LogUI("GetMyInfo");
//沒有授權(quán)
if (FB.IsLoggedIn == false)
{
if (onGetFaild != null)
{
onGetFaild();
}
return;
}
//Logger.LogUI("API");
FB.API("me?fields=id,name,picture", HttpMethod.GET, (result) =>
{
//Logger.LogUI(result.RawResult);
if (onGotFBMyInfo != null)
{
onGotFBMyInfo(result.RawResult);
}
});
}
/// <summary>
/// 獲取APPLink, 獲取失敗阅悍,TODO
/// </summary>
public static void FBGetAPPLinkUrl()
{
FB.GetAppLink((result) =>
{
Debug.Log(result.RawResult);
Debug.Log("Ref: " + result.Ref);
Debug.Log("TargetUrl: " + result.TargetUrl);
Debug.Log("Url: " + result.Url);
appLinkUrl = result.Url;
//Logger.LogUI(appLinkUrl);
});
}
DeepLink實(shí)現(xiàn)好渠,基于ShareSdk實(shí)現(xiàn)
深度鏈接,我們分享邀請生成的鏈接节视,要實(shí)現(xiàn)的目的是用戶點(diǎn)擊鏈接拳锚,如果已經(jīng)安裝應(yīng)用,拉起應(yīng)用寻行,并攜帶參數(shù)霍掺,如果沒有安裝跳轉(zhuǎn)第三方,去下載應(yīng)用