Unity3D是由Unity Technologies开发的一个让玩家轻松建立诸如三维视频游戏、建筑可视化、实时三维动画等类型互动内容的多平台的综合型游戏开发工具,是一个全面整合的专业游戏引擎;在游戏中使用分享功能可以有效的帮助游戏运营推广,经过分享回流来提升APP安装量。ShareSDK的Unity3D插件能够帮助开发者快速实现分享与受权功能。html
1、应用注册android
获取appkey,操做步骤可参考:《Mob开发者后台使用指南》web
2、快速集成json
下载ShareSDK的Unity3D工具类,双击或导入ShareSDK.unitypackage导入相关文件。
注意该操做可能会覆盖您原来已经存在的文件。app
挂接ShareSDK脚本并配置平台信息编辑器
选择好须要挂接的GameObject(例如Main Camera),在右侧栏中点击Add Component,选择Share SDK 进行挂接。
挂接后会发现提供了当前支持的平台和及其配置信息。能够直接在此处修改你所须要的平台的配置信息。须要注意的是当前的编译环境是Android仍是iOS,其字段名称是不一样的哦!工具
Android编译配置
关键文件:mainTemplate.gradle和proguard-user.txt开发工具
Unity2017及以上版本,在Build Settings > Player Settings下面,有两个开关,新建项目的话打开这两个开关就能够在Plugins>Android生成对应的两个文件;因为这两个文件ShareSDK都有直接提供,只需导入.unitypackage就好,Unity检测到已经存在这两个文件,自动会更新为勾选状态;
Build的时候有一个Buildgradle
System选项,此选项默认选择的Internal,切记要改为选择里面的gradle选项(重要)
图中所示的mainTemplate.gradle文件,即为集成的核心文件,使用编辑器打开此文件,要点内容以下:动画
此处为区分Unity5.6和Unity2017 gradle插件版本的地方,开发时用到哪一个版本就使用哪一个,若使用到其余Unity版本,请随意选择一个,而后build,编译的时候会报错的,Console控制台信息报错时会提示插件版本是多少,根据提示修改为须要的版本就好(只修改后面的数字,好比:2.3.0或者2.1.0)
buildscript
{
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'//Unity2017
//classpath
'com.android.tools.build:gradle:2.1.0'//Unity5.6
// 注册MobSDK
classpath 'com.mob.sdk:MobSDK:+'
}
}
此处为配置签名文件和签名文件的别名和密码(正式发布apk须要的签名文件),能够写绝对路径,也能够写相对路径,相对路径使用”..”跳出一层目录,跳出多层则连续拼接
signingConfigs {
release {
keyAlias 'demokey.keystore' keyPassword '123456' storeFile file('F:\\Unitydemo(CJY)\\MobPushForUnity\\Assets\\Plugins\\Android\\demokey.keystore') storePassword '123456'
}
}
此处为混淆文件的配置,也就是MobPush提供的proguard-user.txt文件,此文件内容不须要更改,按照提供的便可,
如本身代码须要额外增长混淆逻辑,可自行增长混淆规则,若是是Unity2017如下版本,请把注释的代码调换一下便可;
(minifyEnabled属性为是否开启代码混淆:true为开启混淆,false为关闭)
buildTypes {
release {
minifyEnabled true// 是否混淆 //shrinkResources false//
是否去除无效的资源文件
proguardFiles
getDefaultProguardFile('proguard-android.txt'), 'proguard-user.txt'
//Unity2017及以上
//proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt' //Unity2017如下 signingConfig signingConfigs.release
}
debug {
minifyEnabled false signingConfig signingConfigs.release
}
}
配置第三方key信息
ShareSDK提供了一个MobSDK.gradle文件,能够在里面直接将mob的key改为本身的,而且删除不须要的平台,或者修改为本身的第三方key的信息便可;
apply plugin: 'com.mob.sdk'
MobSDK
{
appKey "moba6b6c6d6"
appSecret "b89d2427a3bc7ad1aea1e1e8c1d36bf3"
ShareSDK {
version "3.3.0"
//平台配置信息
devInfo {
SinaWeibo { id 1 sortId 1 appKey "568898243" appSecret "38a4f8204cc784f81f9f0daaf31e02e3" callbackUri "http://www.sharesdk.cn" shareByAppClient true enable true } Wechat { id 4 sortId 4 appId "wx4868b35061f87885" appSecret "64020361b8ec4c99936c0e3999a9f249" userName "gh_afb25ac019c9" path "pages/index/index.html?id=1" withShareTicket true miniprogramType 0 bypassApproval false enable true } WechatMoments { id 5 sortId 5 appId "wx4868b35061f87885" appSecret "64020361b8ec4c99936c0e3999a9f249" bypassApproval false enable true } QQ { id 7 sortId 7 appId "100371282" appKey "aed9b0303e3ed1e27bae87c33761161d" shareByAppClient true bypassApproval false enable true } Facebook { id 8 sortId 8 appKey "1412473428822331" appSecret "a42f4f3f867dc947b9ed6020c2e93558" callbackUri "https://mob.com" shareByAppClient true enable true }
}
}
}
iOS编译配置
初始化与社交平台信息配置
修改ShareSDKDevInfo.cs文件,配置所需的平台信息
1.配置您本身的ShareSDK的AppKey (经过第一步获取)
public class
AppKey
{
//配置ShareSDK
AppKey
public
string appKey = "a5d9150e8348";
}
2.选择所须要的平台,不想要的能够直接注释或删掉
public class
DevInfoSet
{
public
SinaWeiboDevInfo sinaweibo;
public
TencentWeiboDevInfo tencentweibo;
public
QQ qq;
public
QZone qzone;
}
3.配置对应平台的信息(建议直接修改字符串值便可)
public class
SinaWeiboDevInfo : DevInfo
{
#if
UNITY_ANDROID
public
const int type = (int) PlatformType.SinaWeibo;
public
string SortId = "1";
public
string AppKey = "568898243";
public
string AppSecret
= "38a4f8204cc784f81f9f0daaf31e02e3";
public
string
RedirectUrl = "http://www.sharesdk.cn";
public
string
ShareByAppClient = "false";
#elif
UNITY_IPHONE
public
const int type = (int) PlatformType.SinaWeibo;
public
string app_key =
"568898243";
public
string
app_secret ="38a4f8204cc784f81f9f0daaf31e02e3";
public
string
redirect_uri = "http://www.sharesdk.cn";
public
string auth_type
= "both"; //can pass
"both","sso",or "web"
#endif
}
3、接口调用
首先引入命名空间:
using
cn.sharesdk.unity3d;
private ShareSDK
ssdk;
分享
1.定制分享信息
ShareContent content = new
ShareContent();
content.SetText("this
is a test string.");
content.SetImageUrl("https://f1.webshare.mob.com/code/demo/img/1.jpg");
content.SetTitle("test
title");
content.SetTitleUrl("http://www.mob.com");
content.SetSite("Mob-ShareSDK");
content.SetSiteUrl("http://www.mob.com");
content.SetUrl("http://www.mob.com");
content.SetComment("test
description");
content.SetMusicUrl("http://mp3.mwap8.com/destdir/Music/2009/20090601/ZuiXuanMinZuFeng20090601119.mp3");
content.SetShareType(ContentType.Webpage);
分享参数可参考:平台参数说明文档
2.设置分享回调
ssdk.shareHandler = ShareResultHandler;
//如下为回调的定义:
void ShareResultHandler (int
reqID, ResponseState state, PlatformType type, Hashtable result)
{
if
(state == ResponseState.Success)
{
("share
result :");
(MiniJSON.jsonEncode(result));
}
else
if (state == ResponseState.Fail)
{
("fail!
throwable stack = " + result["stack"] + ";
error msg = " + result["msg"]);
}
else
if (state == ResponseState.Cancel)
{
("cancel
!");
}
}
3.进行分享
//经过分享菜单分享
ssdk.ShowPlatformList (null, content, 100, 100);
//直接经过编辑界面分享
ssdk.ShowShareContentEditor
(PlatformType.SinaWeibo, content);
//直接分享
ssdk.ShareContent (PlatformType.SinaWeibo, content);
受权(每次都会跳转到第三方平台进行受权)
设置受权回调
ssdk.authHandler = AuthResultHandler;
//如下为回调的定义:
void AuthResultHandler(int
reqID, ResponseState state, PlatformType type, Hashtable result)
{
if
(state == ResponseState.Success)
("authorize
success !");
} else
if (state == ResponseState.Fail)
{
print ("fail! throwable stack = " + result["stack"] + ";
error msg = " + result["msg"]);
} else
if (state == ResponseState.Cancel)
("cancel
!");
}
}
进行受权
ssdk.Authorize(PlatformType.SinaWeibo);
获取用户信息
(只会在第一次跳转到第三方平台进行受权)
指定获取用户信息的回调
sdk.showUserHandler =
GetUserInfoResultHandler;
//如下为回调的定义:
void GetUserInfoResultHandler (int
reqID, ResponseState state, PlatformType type, Hashtable result)
{
if
(state == ResponseState.Success)
("get
user info result :");
(MiniJSON.jsonEncode(result));
} else
if (state == ResponseState.Fail)
("fail!
throwable stack = " + result["stack"] + ";
error msg = " + result["msg"]);
} else
if (state == ResponseState.Cancel)
("cancel
!");
}
}
获取用户信息
ssdk.GetUserInfo(PlatformType.SinaWeibo);
至此,Unity3D插件部分的工做已经完成。如在操做过程当中遇到任何问题,欢迎联系技术支持QQ4006852216随时与咱们进行讨论哦~