Unity 各种平台SDK对接系列文章:Unity 各种平台SDK对接目录
官方文档:MobTech ShareSDK文档
ShareSDK可用于多个平台登录和分享,无需再用服务端,本文主要介绍安卓端使用ShareSDK,进行微信登录和分享。
当时使用的是Unity2019.4.5,gradle版本4.0.0。
一、创建应用:
1、到微信开发平台申请创建应用,申请通过后获得appId,appSecret;
2、到MobTech平台创建应用,获得appKey,appSecret;
二、对接ShareSDK:
按照官方文档步骤对接即可;
三、一些注意事项:
1、launcherTemplate.gradle文件,把需要的各平台的appId和appSecret写在里面,在ShareSDKDevInfo.cs里面更改是无效的。
以微信平台为例:
// 添加ShareSDK相关
apply plugin: 'com.mob.sdk'
MobSDK {
// demo 密钥密码123456,包名:cn.sharesdk.demo
appKey "moba0b0c0d0"
appSecret "5713f0d88511f9f4cf100cade0610a34"
ShareSDK {
loopShare true
devInfo {
// 微信
Wechat {
// demo
appId "wx4868b35061f87885"
appSecret "64020361b8ec4c99936c0e3999a9f249"
userName "gh_afb25ac019c9"
path "pages/index/index.html?id=1"
withShareTicket true
miniprogramType 2
withShareTicket true
bypassApproval false
enable true
}
// 微信朋友圈
WechatMoments {
// demo
appId "wx4868b35061f87885"
appSecret "64020361b8ec4c99936c0e3999a9f249"
bypassApproval false
enable true
}
}
}
}
2、如果微信应用正在申请,测试可使用Mob官方demo进行测试:
demo应用包名:cn.sharesdk.demo;
demokey.keystore密钥密码和别名密码,均为123456;
demo应用,Mob的appKey:moba0b0c0d0,appSecret:5713f0d88511f9f4cf100cade0610a34;
demo应用,微信的appId:wx4868b35061f87885,appSecret:64020361b8ec4c99936c0e3999a9f249;
3、不需要的平台,可以在ShareSDKDevInfo.cs里的DevInfoSet中注释掉;
四、使用ShareSDK:
1、场景中挂载ShareSDK.cs和MobSDK.cs;
2、使用前,必须要提交ShareSDK隐私协议的授权接口:
MobSdk.submitPolicyGrantResult(true);
3、微信登录,使用ShareSDK的获取用户信息接口即可(只会在第一次跳转到第三方平台进行授权),返回结果包含openId,unionId,nickName等信息:
private void Start()
{
// 微信登录,注册获取用户信息回调
ShareSDK.showUserHandler = OnGetUserInfoResultHandler;
// 获取微信用户信息
ShareSDK.GetUserInfo(PlatformType.WeChat);
}
///
/// 微信登录,获取用户信息回调
///
///
///
///
///
private void OnGetUserInfoResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
{
Debug.Log("微信登录,用户信息回调结果");
if (state == ResponseState.Success)
{
Debug.Log("获取用户信息成功");
if (result == null || result.Count <= 0)
{
return;
}
Debug.Log(MiniJSON.jsonEncode(result));
// MiniJSON.jsonEncode(result)内容如下:
// {"privilege":[], "userTags":"", "sex":0, "openid":"xxx", "unionid":"xxx", "nickname":"xxx", "city":"", "province":"", "headimgurl":"xxx", "country":"", "language":""}
string openId = (string)result["openid"]; // openId
string unionId = (string)result["unionid"]; // unionId
string nickName = (string)result["nickname"]; // 昵称
string headImgUrl = (string)result["headimgurl"]; // 头像地址
return;
}
if (state == ResponseState.Fail)
{
// 失败
Debug.Log("获得用户信息失败");
if (result != null)
{
Debug.Log(MiniJSON.jsonEncode(result));
}
}
else if (state == ResponseState.Cancel)
{
// 取消
Debug.Log("获得用户信息取消");
if (result != null)
{
Debug.Log(MiniJSON.jsonEncode(result));
}
}
}
4、微信分享:
///
/// 开始分享
///
/// WeChat:微信好友;WeChatMoments:朋友圈
public static void StartShare(PlatformType platformType)
{
ShareContent content = new ShareContent();
content.SetTitle("测试标题");
content.SetUrl("测试的url");
if (platformType == PlatformType.WeChat)
{
// 分享好友,设置文案
content.SetText("测试描述");
}
// 设置Icon缩略图
content.SetImageUrl("测试Icon的url");
// 分享类型:链接
content.SetShareType(ContentType.Webpage);
//通过分享菜单分享
// ShareSDK.ShowPlatformList(null, content, 100, 100);
// 直接分享
ShareSDK.ShareContent(platformType, content);
}