Unity3D是由Unity Technologies開發的一個讓玩家輕鬆建立諸如三維視頻遊戲、建築可視化、實時三維動畫等類型互動內容的多平臺的綜合型遊戲開發工具,是一個全面整合的專業遊戲引擎;在遊戲中使用分享功能可以有效的幫助遊戲運營推廣,經過分享迴流來提升APP安裝量。ShareSDK的Unity3D插件能夠幫助開發者快速實現分享與受權功能。html
1、應用註冊android
獲取appkey,操做步驟可參考:《Mob開發者後臺使用指南》git
2、快速集成github
下載ShareSDK的Unity3D工具類,雙擊或導入ShareSDK.unitypackage導入相關文件。 注意該操做可能會覆蓋您原來已經存在的文件。web
掛接ShareSDK腳本並配置平臺信息json
選擇好須要掛接的GameObject(例如Main Camera),在右側欄中點擊Add Component,選擇Share SDK 進行掛接。app
掛接後會發現提供了當前支持的平臺和及其配置信息。能夠直接在此處修改你所須要的平臺的配置信息。須要注意的是當前的編譯環境是Android仍是iOS,其字段名稱是不一樣的哦!編輯器
Android****編譯配置工具
關鍵文件:mainTemplate.gradle和proguard-user.txt開發工具
此處爲區分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
}
}
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)
{
print ("share result :");
print (MiniJSON.jsonEncode(result));
}
else if (state == ResponseState.Fail)
{
print ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
}
else if (state == ResponseState.Cancel)
{
print ("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)
{
print ("authorize success !");
}
else if (state == ResponseState.Fail)
{
print ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
}
else if (state == ResponseState.Cancel)
{
print ("cancel !");
}
}
ssdk.Authorize(PlatformType.SinaWeibo);
獲取用戶信息 (只會在第一次跳轉到第三方平臺進行受權)
sdk.showUserHandler = GetUserInfoResultHandler;
//如下爲回調的定義:
void GetUserInfoResultHandler (int reqID, ResponseState state, PlatformType type, Hashtable result)
{
if (state == ResponseState.Success)
{
print ("get user info result :");
print (MiniJSON.jsonEncode(result));
}
else if (state == ResponseState.Fail)
{
print ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
}
else if (state == ResponseState.Cancel)
{
print ("cancel !");
}
}
ssdk.GetUserInfo(PlatformType.SinaWeibo);
至此,Unity3D插件部分的工做已經完成。如在操做過程當中遇到任何問題,歡迎聯繫技術支持QQ 4006852216隨時與咱們進行討論哦~