如何通過ShareSDK的 Unity3D快速接入Android/iOS分享與授權


Unity3D是由Unity Technologies開發的一個讓玩家輕松創建諸如三維視頻游戲、建築可視化、實時三維動畫等類型互動內容的多平台的綜合型游戲開發工具,是一個全面整合的專業游戲引擎;在游戲中使用分享功能能夠有效的幫助游戲運營推廣,通過分享回流來提高APP安裝量。ShareSDK的Unity3D插件可以幫助開發者快速實現分享與授權功能。

 

一、應用注冊

獲取appkey,操作步驟可參考:《Mob開發者后台使用指南

二、快速集成

下載ShareSDK的Unity3D工具類,雙擊或導入ShareSDK.unitypackage導入相關文件。 注意該操作可能會覆蓋您原來已經存在的文件。

掛接ShareSDK腳本並配置平台信息

選擇好需要掛接的GameObject(例如Main Camera),在右側欄中點擊Add Component,選擇Share SDK 進行掛接。

掛接后會發現提供了當前支持的平台和及其配置信息。可以直接在此處修改你所需要的平台的配置信息。需要注意的是當前的編譯環境是Android還是iOS,其字段名稱是不同的哦!

Android編譯配置

關鍵文件:mainTemplate.gradle和proguard-user.txt

  1. Unity2017及以上版本,在Build Settings > Player Settings下面,有兩個開關,新建項目的話打開這兩個開關就可以在Plugins>Android生成對應的兩個文件;由於這兩個文件ShareSDK都有直接提供,只需導入.unitypackage就好,Unity檢測到已經存在這兩個文件,自動會更新為勾選狀態;
  2. Build的時候有一個Build System選項,此選項默認選擇的Internal,切記要改成選擇里面的gradle選項(重要
  3. 圖中所示的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

}

}

  1. 配置第三方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

}

三、接口調用

首先引入命名空間:

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);

授權(每次都會跳轉到第三方平台進行授權)

  1. 設置授權回調

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 !");

}

}

  1. 進行授權

ssdk.Authorize(PlatformType.SinaWeibo);

獲取用戶信息 (只會在第一次跳轉到第三方平台進行授權)

  1. 指定獲取用戶信息的回調

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 !");

}

}

  1. 獲取用戶信息

ssdk.GetUserInfo(PlatformType.SinaWeibo);

 

 

至此,Unity3D插件部分的工作已經完成。如在操作過程中遇到任何問題,歡迎聯系技術支持QQ 4006852216隨時與我們進行討論哦~

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM