How fast access to Android / iOS share and authorized by the Unity3D ShareSDK

Unity3D by Unity Technologies developed a allows players to easily create, such as a three-dimensional video games, architectural visualization, multi-platform type of interactive content real-time 3D animation and other integrated game development tool, is a fully integrated professional game engine; used in the game sharing can effectively help operators to promote the game to increase installed capacity by sharing APP reflux. ShareSDK of Unity3D plugin can help developers quickly implement share and authorization functions.

 

First, the application registration

Get appkey, steps can refer to: " Mob Developer Guide backstage "

Second, the rapid integration

Download ShareSDK of Unity3D tools , double-click or import ShareSDK.unitypackage import documents. Note that this operation may overwrite your original file already exists.

Mount ShareSDK script and platform configuration information

Select that you need to mount the GameObject (eg Main Camera), click Add Component in the right column, select Share SDK mount it.

It will be found attached to provide a current supported platforms and their configuration information. You can modify the configuration information you need directly on the platform here. Note that the current compilation environment is Android or iOS, whose field names are different Oh!

Android build configuration

Key documents: mainTemplate.gradle and proguard-user.txt

  1. Unity2017 and above, the Build Settings> Player Settings below, if there are two switches, the new project is opened two switches can be generated in two files corresponding Plugins> Android; ShareSDK since both files are provided directly, simply introducing .unitypackage like, Unity detect the presence of these two files has been automatically updated with the checked state;
  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随时与我们进行讨论哦~



Guess you like

Origin www.cnblogs.com/mobservice/p/11041411.html