海外项目需要facebook登录等功能,国内有一些集成sdk特别简单好用,但是游戏项目需要邀请功能,所以只能集成原生SDK了,在此记录一下。
ShareSDK http://www.mob.com/mobService/sharesdk集成sdk,包挺小的,服务也很好,客服随时都能联系上,但是facebook打不开好友列表,很郁闷,只能接原生的了
Facebook 开发者后台地址 https://developers.facebook.com/apps/ 需在此注册应用
Facebook原生sdk下载地址 https://developers.facebook.com/docs/unity/downloads/ sdk需接入工程
第一步,注册应用
1.注册facebook账户,注册开发者,创建应用进入下图页面(facebook老是被封,怎么解决...)

image
2.点击基本填写应用基本信息

image
3.翻到页面最下面,点击添加平台

image
4.以Android为例,添加完平台,包名类名秘钥都都在项目中自动生成

image
获取密钥方式在此:https://www.jianshu.com/p/9557a469bcf2
第二步,导入SDK
1.下载sdk导入工程

image
2.点击设置

image
填写项目名称,编号
将包名,类名,秘钥填写到后台到此sdk接入完成,剩下的就是API的应用

image
下面上代码
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");
//没有授权
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实现,基于ShareSdk实现
深度链接,我们分享邀请生成的链接,要实现的目的是用户点击链接,如果已经安装应用,拉起应用,并携带参数,如果没有安装跳转第三方,去下载应用