Unity 接入Facebook。登录、分享、邀请、好友列表等功能。详细记录
来源:互联网 发布:mac版qq群相册在哪儿 编辑:程序博客网 时间:2024/05/22 02:20
写一篇长博文不容易啊。
转载请说明本文出处: http://blog.csdn.net/blog2015/article/details/70691696
Unity版本 5.3.4f1
Facebook版本 7.9.4
Facebook 提供了Unity SDK, 这比原生的方式接要方便很多。
1、在开发者后台创建应用。
后台地址:https://developers.facebook.com/apps/
2、下载Unity版本的SDK, 导入Unity。
下载地址:https://developers.facebook.com/docs/unity/
3、配置。如下图
可以从菜单栏打开EditSetting,也可以在Project视图中直接找到FacebookSettings进行配置。
在Inspector中可以看到详细的需要配置信息。
第一部分,应用的基本信息,可以在facebook后台中找到,具体位置可以鼠标移动到 “?”处查看。
第二部分,FB.Init,使用默认设置即可,当然也可根据自己的实际需要配置。
第三部分,安卓/iOS的配置,因为没有发布IOS包,并没有做IOS的处理。这里只说安卓的。
需要注意的有三点:
1),并不是要在这里配置什么,而是要把这里的信息配置到Facebook后台的中(设置-基本设置-下方)。
2),这里显示的信息并不正确,需要配置真实值。那真实值在哪里?
package Name : 包名,你应用打包使用的包名
Class Name:启动类名,你游戏的启动Acitivity。可以在AndroidManifest.xml中查看。
Facebook将根据你配置的包名和启动类名,在你好友点击邀请链接时启动你的游戏。
Key Hash : 邀请功能必须配置的参数。
可以google或baidu一下,有很多获取方式,甚至有查看Facebook Key Hash的工具存在。
最方便的方法是先填这里不正确的,等报错的时,填报错中显示的Key Hash。这个下边再说。
3),配置完后记得执行 GenertateAndroidManifest.
生成之后把其中 需要的权限、Acitivity,meta-data等配置Copy到你的游戏AndroidManifest.xml中
第四部分,App Links Settings, 邀请功能的第一个参数,可不填。这个在之后说邀请功能的时候再说。
4、接下来就是写代码和做具体的功能了。
文档地址:https://developers.facebook.com/docs/unity/reference/current
游戏中,用到的比较常见的功能有,
登录、分享、获取自己的信息、获取好友列表(好友信息列表)、邀请、分数排行榜等。
还提供了应用内打点统计等其他功能。
一、初始化
FB.Init()
初始化会返回 IsInitialized 的标志。
二、登录:出于安全性考虑,Facebook的功能都必须在一定的权限下进行, 所以也可以说登录就是一种授权行为。
它提供了两种方式的登录, LogInWithReadPermissions 和 LogInWithPublishPermissions。
你可以根据自己需要的接入的功能选择登录方式,并传入权限参数。
注意: 不能在LogInWithReadPermissions 中传入Publish权限,也不能在LogInWithPublishPermissions中传入
Read权限。
否则会报错: Cannot pass a publish or manage
permission (xxx) to a request for ead authorization 或 Cannot pass a read permission (xxx) to a request for publish authorization
权限参考:https://developers.facebook.com/docs/facebook-login/permissions
登录会返回IsLoggedIn的标志 AccessToken,
AccessToken中包含UserId、TokenString、请求的权限列表等信息。
个人理解:需要登录才能做的功能(比如邀请、取好友列表)在执行前 会将AccessToken中的信息传入Facebook服务器做验证,验证成功才能进行下一步操作。
三、分享:分享比较简单,调用FB.ShareLink 传入 跳转链接、标题、内容、图片链接 即可
public static void ShareLink(Uri contentURL = null, string contentTitle = "", string contentDescription = "", Uri photoURL = null, FacebookDelegate<IShareResult>
callback = null);
分享可以在没有登录的时候调用,会自己调起登录授权。
四、获取自己的信息、好友信息列表。
重点!!! Facebook 通过 Graph API(图谱API) 来获取用户数据,或发布内容(如分数)。
你必须先了解Graph API是什么!
文档地址:https://developers.facebook.com/docs/graph-api
两个重载的方法:
public static void API(string query, HttpMethod method, FacebookDelegate<IGraphResult> callback = null, IDictionary<string, string> formData = null);
public static void API(string query, HttpMethod method, FacebookDelegate<IGraphResult> callback, WWWForm formData);
其中 第一个参数必须满足FQL(facebook Query Language)的语法。可以传入具体的查询参数,查找自己需要的信息
第二个参数是 选择获取Get还是发布Post。
第三个参数是 结果回调。
第四个参数是 参数二选择Post时附带的发布信息。
1),什么是FQL?
文档地址:https://developers.facebook.com/docs/technical-guides/fql/
2),数据的结构是怎样的?
参考:https://developers.facebook.com/docs/graph-api/reference/user/
3),怎么样快速测一下我传的query参数对不对?
Graph API探索工具:https://developers.facebook.com/tools/explorer
4),如何处理返回结果?
IGraphResult 中的 ResultList 是返回的结果,
但推荐直接使用其父类IResult 中的RawResult。
RawResult是一个Json字符串,可以方便的在各种语言下解析,因为我们更多的用lua写业务逻辑。
Facebook官方也提供了在C#中解析的工具:
文档参考:https://developers.facebook.com/docs/unity/reference/current/Json
注意:
1),"me/friends" 查询到的结果是 已经登录过该游戏/应用的facebook好友列表。
2),"me/friends" 需要在登录时加入 "user_friends"
权限。
五、邀请: 邀请和分享看起来差不多,但实际差别挺大的。
public static void AppInvite(Uri appLinkUrl, Uri previewImageUrl = null, FacebookDelegate<IAppInviteResult>
callback = null);
其中 第一个参数 是一个可以通过Facebook定制的 DeepLink。
在好友点击你的邀请信息时,Facebook通过该深链接根据不同的平台或不同情形做不同的操作,
比如在未安装游戏时点击会打开游戏下载地址,安装游戏时会直接启动游戏。
介绍:https://developers.facebook.com/products/app-links
创建地址:https://developers.facebook.com/quickstarts/?platform=app-links-host
第二个参数 是一个可为空的参数,但建议传入,否则不能在邀请发送时预览。
第三个参数 是结果回调
注意:邀请功能需要在后台配置正确的Key Hash,否则会报错:
error: Invalid key hash. The key hash(此处显示了正确的、需要配置的Key Hash)does
not match any stored key hashes. Configure your app key hashes at https://developers.facebook.com/apps/(游戏的appid)/
将提示的Key hash 配到后台即可。
奇怪的是,
public static void GetAppLink(FacebookDelegate<IAppLinkResult> callback);
Facebook貌似提供了该API来获取AppLink,但实际上我在配置的第四部分,App Links Settings 中填了,也无法获取到。
返回的结果,result.Ref、result.TargetUrl、result.Url均为 空字符串 ""。
result.RawResult 为: {"did_complete":true,"callback_id":"3"}
如果有人遇到并解决了这个问题,请留言告诉我,非常感谢。
下面贴一下完整的代码:
using System;using UnityEngine;using System.Collections;using System.Collections.Generic;using UnityEngine.UI;using Facebook.Unity;using Facebook.MiniJSON;[SLua.CustomLuaClass]public class MyFB{ public delegate void OnFBLoginSucced(Facebook.Unity.AccessToken token); 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 OnFBInvitedSucceed(string resultJsonStr); private static string appLinkUrl; public static void Init() { FB.Init(() => { Debug.Log("FB OnInitComplete!"); Debug.Log("FB.AppId: " + FB.AppId); Debug.Log("FB.GraphApiVersion: " + FB.GraphApiVersion); //获取应用链接 FBGetAPPLinkUrl(); }, (isUnityShutDown) => { Debug.Log("FB OnHideUnity: " + isUnityShutDown); }); } public static void FBLogin(OnFBLoginSucced onFBLoginSucced = null, OnFBLoginFaild onFBLoginFaild = null) { var perms = new List<string>() { "public_profile", "email", "user_friends" }; FB.LogInWithReadPermissions(perms, (result) => { if (FB.IsLoggedIn) { Debug.Log("FBLoginSucceed"); if (onFBLoginSucced != null) { onFBLoginSucced(Facebook.Unity.AccessToken.CurrentAccessToken); } } else { Debug.Log("FBLoginFaild"); Debug.Log(result.RawResult); if (onFBLoginFaild != null) { onFBLoginFaild(result.Cancelled, result.Error); } } }); } //分享, 例: //uri = "https://developers.facebook.com/"; //contentTitle = "ShareLink"; //contentDesc = "Look I'm sharing a link"; //picUri = "https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/holiday/habo/res/doodle/3.png"; public static void FBShareLink(string uri, string contentTitle, string contentDesc, string picUri, OnFBShareLinkSucced onFBShareLinkSucced = null, OnFBShareLinkFaild onFBShareLinkFaild = null) { FBShareLink(new Uri(uri), contentTitle, contentDesc, new Uri(picUri), onFBShareLinkSucced, onFBShareLinkFaild); } private static void FBShareLink(Uri uri, string contentTitle, string contentDesc, Uri picUri, OnFBShareLinkSucced onFBShareLinkSucced = null, OnFBShareLinkFaild onFBShareLinkFaild = null) { FB.ShareLink(uri, contentTitle, contentDesc, picUri, (result) => { if (result.Cancelled || !String.IsNullOrEmpty(result.Error)) { Debug.Log("ShareLink Faild"); if (onFBShareLinkFaild != null) { onFBShareLinkFaild(result.Cancelled, result.Error); } } else { Debug.Log("ShareLink success!"); if (onFBShareLinkSucced != null) { onFBShareLinkSucced(String.IsNullOrEmpty(result.PostId) ? "" : result.PostId); } } }); } //获取自己的信息 public static void GetMyInfo(OnGotFBMyInfo onGotFBMyInfo) { if (FB.IsLoggedIn == false) { UIFloatTip.Create("Not Login in"); return; } FB.API("me?fields=id,name,picture", HttpMethod.GET, (result) => { Debug.Log(result.RawResult); if (onGotFBMyInfo != null) { onGotFBMyInfo(result.RawResult); } }); } //获取游戏好友 public static void GetFBFriendInGame(OnGotFBFriendInGame onGotFBFriendInGame = null) { Debug.Log("GetFBFriendInGame"); if (FB.IsLoggedIn == false) { UIFloatTip.Create("Not Login in"); return; } FB.API("me/friends?fields=id,name,picture", HttpMethod.GET, (result)=> { Debug.Log(result.RawResult); if (onGotFBFriendInGame != null) { onGotFBFriendInGame(result.RawResult); } }); } //获取可邀请好友, 获取失败 TODO public static void GetFBFriendInvitable() { if (FB.IsLoggedIn == false) { UIFloatTip.Create("Not Login in"); return; } FB.API("/me/invitable_friends?fields=id,name,picture", HttpMethod.GET, (result) => { Debug.Log("result: "); Debug.Log(result.RawResult); }); } //邀请, public static void FBInvite(string assignedLink, string previewImageUrl, OnFBInvitedSucceed onFBInvitedSucceed = null) { if (String.IsNullOrEmpty(assignedLink)) { assignedLink = appLinkUrl; } Debug.Log("appLinkUrl: " + appLinkUrl); Debug.Log("assignedLink: " + assignedLink); FBInvite(new Uri(assignedLink), new Uri(previewImageUrl), onFBInvitedSucceed); } private static void FBInvite(Uri appLinkUrl, Uri previewImageUrl = null, OnFBInvitedSucceed onFBInvitedSucceed = null) { FB.Mobile.AppInvite(appLinkUrl, previewImageUrl, (result)=> { Debug.Log("rawResult: " + result.RawResult); }); } //获取APPLink, 获取失败,TODO 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; }); }}
欢迎留言交流。
不对的地方欢迎指正。
- Unity 接入Facebook。登录、分享、邀请、好友列表等功能。详细记录
- iOS对接Facebook:登录,分享,邀请,游戏排行榜功能
- Facebook 登录接入记录
- facebook 邀请好友
- 接入google SDK登录、支付,facebook SDK登录详细教程
- facebook 获取好友列表
- Facebook登录服务器端接入
- 【Unity3d】unity接入sharesdk分享本地图片失败记录
- Android平台接入Facebook登录
- 系统关闭了好友邀请功能
- 新版php获取MSN好友列表,邀请MSN好友
- Android之微信接入分享,登录功能。
- 个人在app中接入BaiduMapSdk的详细分享记录
- 手游接入Facebook功能
- Facebook 登录和分享
- Facebook SDK 登录分享
- facebook登录和分享
- Facebook接入ios,要点记录(不回调,info.plist写法等问题)
- 《统计学习方法》学习笔记(5)-- 朴素贝叶斯法
- 文章标题
- java selenium (四) 使用浏览器调试工具
- python中的模块
- 从零开始学Makefile(五)
- Unity 接入Facebook。登录、分享、邀请、好友列表等功能。详细记录
- 新建一个maven spring boot项目中遇到的问题
- 稀疏矩阵的创建【严蔚敏】
- BlueROV-1: Meeting Notes and List of Things We Need
- 指数分布族 和 广义线性模型
- Memcached学习笔记(1)
- Qt实现天气预报与PM2.5监测系统(9)空气质量
- redis.conf 简介
- 51nod 2级算法题-1050