本文档使用Unity2019进行演示
下载unitypackage
从Mob的github地址下载ShareSDK.unitypackage:Git地址,如下图所示
)![image.png]//download.sdk.mob.com/2022/06/22/15/165588252810937.61.png)
下载完成后得到一个.unitypackage结尾的文件,如下图:
导入unitypackage
双击导入到unity项目中并勾选,如下图:
Demo.cs是demo的示例页面,可通用
Android (只需要勾选SSDKU3D.jar)
demokey.keystore:demo的打包签名文件
SSDKU3D.jar:java桥接层文件
iOS (全部勾选)
MOBTool:自动打包工具
ShareSDK:SDK必要的文件
按图选好之后,点击import导入
挂载ShareSDK.cs脚本文件
在unity界面左侧选择要使用的场景(例如Main Camera),点击Add Component 按钮添加ShareSDK.cs(ShareSDK核心文件),或者直接拖进去。如果要测试效果,也可以添加Demo.cs(ShareSDK示例UI),如下图所示:
挂接后会发现提供了当前支持的平台和及其配置信息,需要注意的是当前的编译环境是Android还是iOS,其字段名称是不同的哦!
修改编译环境
Platform设置,在unity中选择菜单栏-File-Build Settings,会打开Build Settings操作框,根据自己所编译的环境选择,如果是iOS的请选择iOS环境,如果是Android请选择Android环境,然后点击Switch Platform切换环境,如下图:
Android端集成编译配置
资源修改
1.把demokey.keystore和SSDKU3D.jar文件移动到上层目录下,也就从ShareSDK文件夹移动到Android文件夹;
2.删掉ShareSDK文件夹;
3.删掉Android目录下的baseProjectTemplate.gradle、launcherTemplate.gradle等文件;
最终Android目录下文件内容为下图:
修改Unity设置
生成Gradle文件
保持Build Settings操作框中左侧Platform列表中Android项的选中状态,点击Player Settings,会打开Project Settings操作框,如下图:
左侧切换到Player标签,然后在右侧选择Publishing Settings卡片,勾选Custom Launcher Gradle Template和Custom Base Gradle Template选项,如下图:
该操作会在"工程\Assets\Plugins\Android目录下"生成baseProjectTemplate.gradle、launcherTemplate.gradle文件。
修改gradle文件
修改baseProjectTemplate.gradle文件
打开baseProjectTemplate.gradle文件后有两处需要操作:
1.在classpath 'com.android.tools.build:gradle'后添加mob的classpath
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-java"><span style="color:#cc7832">classpath</span> "<span style="color:#cc7832">com</span><span style="color:#e8bf6a">.mob</span><span style="color:#e8bf6a">.sdk</span>:MobSDK:2018.0319.1724"</code></span></span>
2.添加mob自己的maven仓库
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-java"><span style="color:#ffc66d">maven</span> { <span style="color:#6a8759">url</span> <span style="color:#6a8759">"https://mvn.mob.com/android/"</span>}</code></span></span>
最终baseProjectTemplate.gradle文件的参考结果为:
修改launcherTemplate.gradle文件
打开launcherTemplate.gradle,在文件的最下方加入以下代码:
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code><span style="color:#6a8759">apply</span> plugin: <span style="color:#6a8759">'com.mob.sdk'</span>
MobSDK {
<span style="color:#6a8759">appKey</span> <span style="color:#6a8759">"moba0b0c0d0"</span>
appSecret <span style="color:#6a8759">"5713f0d88511f9f4cf100cade0610a34"</span>
spEdition <span style="color:#6a8759">"fp"</span>
ShareSDK {
<span style="color:#ffc66d">devInfo</span> {
<span style="color:#ffc66d">Wechat</span> {
<span style="color:#6a8759">appId</span> <span style="color:#6a8759">"wx4868b35061f87885"</span>
appSecret <span style="color:#6a8759">"64020361b8ec4c99936c0e3999a9f249"</span>
withShareTicket <span style="color:#6897bb">true</span>
bypassApproval <span style="color:#6897bb">false</span>
enable <span style="color:#6897bb">true</span>
}
}
}
}</code></span></span>
这里的配置和Android原生配置无异,可参考Mob官方链接
配置签名文件
1.菜单打开Build Settings,左侧Platform列表选中Android,点击Player Settings,在打开的Project Settings操作框中,左侧选择Player标签,右侧选择Publishing Settings卡片;
2.勾选Project Keystore下的Custom Key store;
3.点击Project Keystore下的Select选择一个keystore文件;
4.填写Project Keystore下的Password;
5.如Project Keystore下的Password填写正确,则Project Key下的Alias下拉框亮起,选择自己的别名;
6.填写Project Key下的Password;
iOS端编译配置
默认所有平台都已经初始化,客户需要做的操作:
修改初始化我们SDK的AppKey和AppSecret
配置您自己的ShareSDK的AppKey和AppSecret (获取可以点击查看注册流程)
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-c#"><span style="color:#cc7832">public</span> <span style="color:#cc7832">class</span> <span style="color:#ffc66d">ShareSDK</span> : <span style="color:#ffc66d">MonoBehaviour</span>
{
<span style="color:#cc7832">private</span> <span style="color:#cc7832">int</span> reqID;
<span style="color:#bbb529">#elif UNITY_IPHONE</span>
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> appKey = <span style="color:#6a8759">"3276d3e413040"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> appSecret = <span style="color:#6a8759">"4280a3a6df667cfce37528dec03fd9c3"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> mobNetLater = <span style="color:#6a8759">"2"</span>;
<span style="color:#cc7832">public</span> List<<span style="color:#cc7832">string</span>> customAssociatedDomains = <span style="color:#cc7832">new</span> List<<span style="color:#cc7832">string</span>>();
<span style="color:#bbb529">#endif</span>
<span style="color:#6a8759">''</span><span style="color:#6a8759">' '</span><span style="color:#6a8759">''</span>
}</code></span></span>
修改第三方社交平台初始化信息配置
可以找到ShareSDKDevInfo.cs文件,进行修改。配置所需的平台信息
选择所需要的平台,不想要的可以直接注释或删掉
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-c#"><span style="color:#cc7832">public</span> <span style="color:#cc7832">class</span> <span style="color:#6897bb">DevInfoSet</span>
{
<span style="color:#cc7832">public</span> SinaWeiboDevInfo sinaweibo;
<span style="color:#cc7832">public</span> WeChat wechat;
<span style="color:#cc7832">public</span> QQ qq;
<span style="color:#cc7832">public</span> QZone qzone;
}</code></span></span>
配置对应平台的信息(直接修改字符串值即可),例如微信平台修改
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-c#"><span style="color:#cc7832">public</span> <span style="color:#cc7832">class</span> <span style="color:#ffc66d">WeChat</span> : <span style="color:#ffc66d">DevInfo</span>
{
<span style="color:#bbb529">#if UNITY_ANDROID</span>
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> SortId = <span style="color:#6a8759">"5"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">const</span> <span style="color:#cc7832">int</span> type = (<span style="color:#cc7832">int</span>) PlatformType.WeChat;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> AppId = <span style="color:#6a8759">"wx4868b35061f87885"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> AppSecret = <span style="color:#6a8759">"64020361b8ec4c99936c0e3999a9f249"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> UserName = <span style="color:#6a8759">"gh_afb25ac019c9@app"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> Path = <span style="color:#6a8759">"/page/API/pages/share/share"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">bool</span> BypassApproval = <span style="color:#6897bb">false</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">bool</span> WithShareTicket = <span style="color:#6897bb">true</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> MiniprogramType = <span style="color:#6a8759">"0"</span>;
<span style="color:#bbb529">#elif UNITY_IPHONE</span>
<span style="color:#cc7832">public</span> <span style="color:#cc7832">const</span> <span style="color:#cc7832">int</span> type = (<span style="color:#cc7832">int</span>) PlatformType.WeChat;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> app_id = <span style="color:#6a8759">"wx617c77c82218ea2c"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> app_secret = <span style="color:#6a8759">"c7253e5289986cf4c4c74d1ccc185fb1"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> app_universalLink = <span style="color:#6a8759">"https://70imc.share2dlink.com/"</span>;
<span style="color:#bbb529">#endif</span>
}
[<span style="color:#bbb529">Serializable</span>]
<span style="color:#cc7832">public</span> <span style="color:#cc7832">class</span> <span style="color:#ffc66d">WeChatMoments</span> : <span style="color:#ffc66d">DevInfo</span>
{
<span style="color:#bbb529">#if UNITY_ANDROID</span>
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> SortId = <span style="color:#6a8759">"6"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">const</span> <span style="color:#cc7832">int</span> type = (<span style="color:#cc7832">int</span>) PlatformType.WeChatMoments;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> AppId = <span style="color:#6a8759">"wx4868b35061f87885"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> AppSecret = <span style="color:#6a8759">"64020361b8ec4c99936c0e3999a9f249"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">bool</span> BypassApproval = <span style="color:#6897bb">true</span>;
<span style="color:#bbb529">#elif UNITY_IPHONE</span>
<span style="color:#cc7832">public</span> <span style="color:#cc7832">const</span> <span style="color:#cc7832">int</span> type = (<span style="color:#cc7832">int</span>) PlatformType.WeChatMoments;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> app_id = <span style="color:#6a8759">"wx617c77c82218ea2c"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> app_secret = <span style="color:#6a8759">"c7253e5289986cf4c4c74d1ccc185fb1"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> app_universalLink = <span style="color:#6a8759">"https://70imc.share2dlink.com/"</span>;
<span style="color:#bbb529">#endif</span>
}
[<span style="color:#bbb529">Serializable</span>]
<span style="color:#cc7832">public</span> <span style="color:#cc7832">class</span> <span style="color:#ffc66d">WeChatFavorites</span> : <span style="color:#ffc66d">DevInfo</span>
{
<span style="color:#bbb529">#if UNITY_ANDROID</span>
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> SortId = <span style="color:#6a8759">"7"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">const</span> <span style="color:#cc7832">int</span> type = (<span style="color:#cc7832">int</span>) PlatformType.WeChatFavorites;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> AppId = <span style="color:#6a8759">"wx4868b35061f87885"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> AppSecret = <span style="color:#6a8759">"64020361b8ec4c99936c0e3999a9f249"</span>;
<span style="color:#bbb529">#elif UNITY_IPHONE</span>
<span style="color:#cc7832">public</span> <span style="color:#cc7832">const</span> <span style="color:#cc7832">int</span> type = (<span style="color:#cc7832">int</span>) PlatformType.WeChatFavorites;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> app_id = <span style="color:#6a8759">"wx617c77c82218ea2c"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> app_secret = <span style="color:#6a8759">"c7253e5289986cf4c4c74d1ccc185fb1"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> app_universalLink = <span style="color:#6a8759">"https://70imc.share2dlink.com/"</span>;
<span style="color:#bbb529">#endif</span>
}
<span style="color:#cc7832">public</span> <span style="color:#cc7832">class</span> <span style="color:#ffc66d">WechatSeries</span> : <span style="color:#ffc66d">DevInfo</span>
{
<span style="color:#bbb529">#if UNITY_ANDROID</span>
<span style="color:grey">//for android,please set the configuraion in class "Wechat" ,class "WechatMoments" or class "WechatFavorite"</span>
<span style="color:grey">//对于安卓端,请在类Wechat,WechatMoments或WechatFavorite中配置相关信息↑</span>
<span style="color:#bbb529">#elif UNITY_IPHONE</span>
<span style="color:#cc7832">public</span> <span style="color:#cc7832">const</span> <span style="color:#cc7832">int</span> type = (<span style="color:#cc7832">int</span>) PlatformType.WechatPlatform;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> app_id = <span style="color:#6a8759">"wx617c77c82218ea2c"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> app_secret = <span style="color:#6a8759">"c7253e5289986cf4c4c74d1ccc185fb1"</span>;
<span style="color:#cc7832">public</span> <span style="color:#cc7832">string</span> app_universalLink = <span style="color:#6a8759">"https://70imc.share2dlink.com/"</span>;
<span style="color:#bbb529">#endif</span>
}</code></span></span>
确定文件里修改了AppKey等信息之后,要去挂载脚本那边同步看下AppKey是否修改,可以点击Reset,刷新,同步下修改后的信息,也可以自己直接在挂载脚本里面改,最后要确定脚本文件里和挂载脚本里都已经改为了自己的信息
接口调用
回传用户隐私授权结果(submitPolicyGrantResult)
调用示例
传入的第一个参数为Boolean类型的,true 代表同意授权、false代表不同意授权
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code>mobsdk.submitPolicyGrantResult(true);</code></span></span>
注意:该接口必须接入,否则可能造成无法使用MobTech各SDK提供的相关服务。
分享(ShowPlatformList)
首先引入命名空间:
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-c#">using cn.sharesdk.unity3d<span style="color:grey">;</span>
public ShareSDK ssdk<span style="color:grey">;</span></code></span></span>
构造分享信息
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-c#">ShareContent content = new ShareContent();
content.SetText(<span style="color:#6a8759">"this is a test string."</span>);
content.SetImageUrl(<span style="color:#6a8759">"https://f1.webshare.mob.com/code/demo/img/1.jpg"</span>);
content.SetTitle(<span style="color:#6a8759">"test title"</span>);
content.SetTitleUrl(<span style="color:#6a8759">"http://www.mob.com"</span>);
content.SetSite(<span style="color:#6a8759">"Mob-ShareSDK"</span>);
content.SetSiteUrl(<span style="color:#6a8759">"http://www.mob.com"</span>);
content.SetUrl(<span style="color:#6a8759">"http://www.mob.com"</span>);
content.SetComment(<span style="color:#6a8759">"test description"</span>);
content.SetMusicUrl(<span style="color:#6a8759">"http://mp3.mwap8.com/destdir/Music/2009/20090601/ZuiXuanMinZuFeng20090601119.mp3"</span>);
content.SetShareType(ContentType.Webpage);</code></span></span>
常用的几个传参数方法说明(不常用的方法请看Assets/Plugins/ShareSDK/ShareContent.cs脚本):
Text :分享的文字
Title: 分享的标题
TitleUrl :标题的网络链接(QQ和QQ空间使用 )
SetImageUrl :iOS平台,本地以及网络图片都使用此方法
image:android平台分享本地图片与网络图片都用此方法
Url: 分享的链接(微信,微博,易信,Facebook等平台)
ShareType:分享类型(微信,易信)
FilePath:分享文件路径 (微信,易信)
MusicUrl :分享的音乐链接(微信,QQ,易信)
设置分享回调
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-c">ssdk.shareHandler = ShareResultHandler;
//以下为回调的定义:
void ShareResultHandler (int reqID, ResponseState <span style="color:#cc7832">state</span>, PlatformType type, Hashtable result)
{
if (<span style="color:#cc7832">state</span> == ResponseState.Success)
{
print (<span style="color:#6a8759">"share result :"</span>);
print (MiniJSON.jsonEncode(result));
}
else if (<span style="color:#cc7832">state</span> == ResponseState.Fail)
{
print (<span style="color:#6a8759">"fail! throwable stack = "</span> + result[<span style="color:#6a8759">"stack"</span>] + <span style="color:#6a8759">"; error msg = "</span> + result[<span style="color:#6a8759">"msg"</span>]);
}
else if (<span style="color:#cc7832">state</span> == ResponseState.Cancel)
{
print (<span style="color:#6a8759">"cancel !"</span>);
}
}</code></span></span>
进行分享
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-c#"><span style="color:grey">//通过分享菜单分享</span>
ssdk<span style="color:#e8bf6a">.ShowPlatformList</span> (null, <span style="color:#6a8759">content</span>, <span style="color:#6897bb">100</span>, <span style="color:#6897bb">100</span>);
<span style="color:grey">//直接通过编辑界面分享</span>
ssdk<span style="color:#e8bf6a">.ShowShareContentEditor</span> (PlatformType<span style="color:#e8bf6a">.SinaWeibo</span>, <span style="color:#6a8759">content</span>);
<span style="color:grey">//直接分享</span>
ssdk<span style="color:#e8bf6a">.ShareContent</span> (PlatformType<span style="color:#e8bf6a">.SinaWeibo</span>, <span style="color:#6a8759">content</span>);</code></span></span>
授权(Authorize)
(每次都会跳转到第三方平台进行授权)
设置授权回调
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-c">ssdk.authHandler = AuthResultHandler;
//以下为回调的定义:
void AuthResultHandler(int reqID, ResponseState <span style="color:#cc7832">state</span>, PlatformType type, Hashtable result)
{
if (<span style="color:#cc7832">state</span> == ResponseState.Success)
{
print (<span style="color:#6a8759">"authorize success !"</span>);
}
else if (<span style="color:#cc7832">state</span> == ResponseState.Fail)
{
print (<span style="color:#6a8759">"fail! throwable stack = "</span> + result[<span style="color:#6a8759">"stack"</span>] + <span style="color:#6a8759">"; error msg = "</span> + result[<span style="color:#6a8759">"msg"</span>]);
}
else if (<span style="color:#cc7832">state</span> == ResponseState.Cancel)
{
print (<span style="color:#6a8759">"cancel !"</span>);
}
}</code></span></span>
进行授权
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code><span style="color:#cc7832">ssdk</span><span style="color:#e8bf6a">.Authorize</span>(<span style="color:#cc7832">PlatformType</span><span style="color:#e8bf6a">.SinaWeibo</span>);</code></span></span>
获取用户信息(GetUserInfo)
(只会在第一次跳转到第三方平台进行授权)
指定获取用户信息的回调
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-c#">sdk.showUserHandler = GetUserInfoResultHandler;
//以下为回调的定义:
void GetUserInfoResultHandler (int reqID, ResponseState <span style="color:#cc7832">state</span>, PlatformType type, Hashtable result)
{
if (<span style="color:#cc7832">state</span> == ResponseState.Success)
{
print (<span style="color:#6a8759">"get user info result :"</span>);
print (MiniJSON.jsonEncode(result));
}
else if (<span style="color:#cc7832">state</span> == ResponseState.Fail)
{
print (<span style="color:#6a8759">"fail! throwable stack = "</span> + result[<span style="color:#6a8759">"stack"</span>] + <span style="color:#6a8759">"; error msg = "</span> + result[<span style="color:#6a8759">"msg"</span>]);
}
else if (<span style="color:#cc7832">state</span> == ResponseState.Cancel)
{
print (<span style="color:#6a8759">"cancel !"</span>);
}
}</code></span></span>
获取用户信息
<span style="background-color:#42475a"><span style="color:#a9b7c6"><code class="language-c#"><span style="color:#cc7832">ssdk</span><span style="color:#e8bf6a">.GetUserInfo</span>(<span style="color:#cc7832">PlatformType</span><span style="color:#e8bf6a">.SinaWeibo</span>);</code></span></span>
注意:auth和getuser接口都可以实现授权登录功能,可以任意调用一个
打包使用
进行上述所有修改后,已经可以执行打包操作。
如集成过程中遇到问题,可详询MobTech技术支持,点击立即QQ咨询