Unity实现第三方登录及分享
Posted mufei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity实现第三方登录及分享相关的知识,希望对你有一定的参考价值。
最近因为公司的项目需要添加一些实用性的功能,需要添加第三方登录及分享,采用的是Mob的SDK,可以先到其官网下载对应的SDK 点击这里,为了方便后期进行数据统计和分析,所以可以先添加一个应用,添加成功后,就可以获取到对应的AppKey和App Secret,这两个东西稍后需要用到。
准备工作
打开Unity,创建一个空项目,然后把我们刚刚下载的Share SDK导入。然后在Project下就会出现Plugins文件,然后选择一个物体,点击AddComponent添加Share SDK脚本,然后将Mob官网上申请的应用的App Key及AppSecret替换组建上原有的数据。然后搭建一个简单的UI,用来实现登录及分享功能。因为要发布到移动端测试,所以我做了一个假的控制台用来输出信息,
1、首先先说最简单的QQ分享及第三方登录
进行QQ第三方登录时,需要先到QQ互联官网上申请一个自己的应用,如果只是为了测试功能,也可以不申请,直接使用Mob默认的Appid及AppSecret。接下来就是写代码了,很简单。新建一个用来测试的脚本,我这里叫Login,先在Start中获取到对应的按钮引用,QQ登录的代码如下。
/// <summary>
/// 退出按钮
/// </summary>
private Button exitBtn;
/// <summary>
/// QQ按钮
/// </summary>
private Button QQBtn;
/// <summary>
/// 微信按钮
/// </summary>
private Button weixinBtn;
/// <summary>
/// 微博按钮
/// </summary>
private Button weiboBtn;
/// <summary>
/// 分享按钮
/// </summary>
private Button ShareBtn;
/// <summary>
/// 用户名输入框
/// </summary>
private InputField userName;
/// <summary>
/// 密码输入框
/// </summary>
private InputField passWord;
/// <summary>
/// SDK
/// </summary>
public ShareSDK ssdk;
/// <summary>
/// 控制台
/// </summary>
public Text text;
/// <summary>
/// 获取到的用户信息保存本地的文件名
/// </summary>
private string fileName;
/// <summary>
/// 获取到用户的头像保存到本地的文件名
/// </summary>
private string iconName;
/// <summary>
/// 可以分享的平台
/// </summary>
private PlatformType[] platforms;
public void Start()
{
Debug.Log(ssdk);
platforms = new PlatformType[] { PlatformType.WeChat,PlatformType.WeChatFavorites,PlatformType.WeChatMoments,PlatformType.WechatPlatform,
PlatformType.QQ,PlatformType.QZone,PlatformType.SinaWeibo};
userName = transform.Find("UserName").GetComponentInChildren<InputField>();
passWord = transform.Find("PassWord").GetComponentInChildren<InputField>();
exitBtn = transform.Find("ExitBtn").GetComponent<Button>();
QQBtn = transform.Find("QQBtn").GetComponent<Button>();
weixinBtn = transform.Find("WeiXinBtn").GetComponent<Button>();
weiboBtn = transform.Find("WeiBoBtn").GetComponent<Button>();
ShareBtn = transform.Find("QQShareBtn").GetComponent<Button>();
exitBtn.onClick.AddListener(ExitButtonHandle);
QQBtn.onClick.AddListener(QQButtonHandle);
weixinBtn.onClick.AddListener(WeiXinButtonHandle);
weiboBtn.onClick.AddListener(WeiBoButtonHandle);
ShareBtn.onClick.AddListener(ShareButtonHandle);
}
/// <summary>
/// QQ登录
/// </summary>
private void QQButtonHandle()
{
Debug.Log("点击了QQ登录");
fileName = "/qq.json";
iconName = "/qqIcon.jpg";
if (File.Exists(Application.persistentDataPath + fileName))
return;
//注册登录回调事件
ssdk.authHandler = AuthHandler;
//确定向哪个平台进行第三方验证
ssdk.Authorize(PlatformType.QQ);
}
/// <summary>
/// 登录回调
/// </summary>
/// <param name="reqID"></param>
/// <param name="state"></param>
/// <param name="type"></param>
/// <param name="data"></param>
private void AuthHandler(int reqID, ResponseState state, PlatformType type, Hashtable data)
{
Debug.Log("回调函数");
if (state == ResponseState.Success)
{
JsonData userData = JsonMapper.ToObject(JsonMapper.ToJson(data));
SaveUserInfo(JsonMapper.ToJson(data));
string icon = userData["icon"].ToString();
StartCoroutine(DownUserIcon(icon));
text.text += "
userid : " + userData["userID"] + "
username : " + userData["nickname"] + "
icon : " + userData["icon"];
text.text += "
授权成功!!!";
userName.text = userData["nickname"].ToString();
}
else if (state == ResponseState.Fail)
{
text.text += "
授权失败!!!";
}
}
/// <summary>
/// 分享
/// </summary>
private void ShareButtonHandle()
{
//分享的内容
ShareContent content = new ShareContent();
if (iconName != null)
content.SetImagePath(Application.persistentDataPath + iconName);
content.SetTitle(" 分享 ");
content.SetTitleUrl("https://www.baidu.com/");
content.SetText(" wecome ");
content.SetSite("Mob-ShareSDK");
content.SetSiteUrl("https://www.baidu.com/");
content.SetUrl("https://www.baidu.com/");
content.SetComment("test description");
content.SetMusicUrl("http://fjdx.sc.chinaz.com/Files/DownLoad/sound1/201807/10300.mp3");
content.SetShareType(ContentType.Webpage);
Debug.Log(" ******* 001 ");
//注册分享回调事件
ssdk.shareHandler = ShareHandler;
//传递需要分享的平台及分享内容
ssdk.ShowPlatformList(platforms, content, 100, 100);
}
/// <summary>
/// 分享回调
/// </summary>
/// <param name="reqID"></param>
/// <param name="state"></param>
/// <param name="type"></param>
/// <param name="data"></param>
private void ShareHandler(