如何通过 ShareSDK 的 Unity3D 快速接入 Android/iOS 分享与授权

2019-06-17 17:56:44 +08:00
 MobService

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

Unity2017 及以上版本,在 Build Settings > Player Settings 下面,有两个开关,新建项目的话打开这两个开关就可以在 Plugins>Android 生成对应的两个文件;由于这两个文件 ShareSDK 都有直接提供,只需导入.unitypackage 就好,Unity 检测到已经存在这两个文件,自动会更新为勾选状态; Build 的时候有一个 Build 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

}

三、接口调用

首先引入命名空间:

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 随时与我们进行讨论哦~

3981 次点击
所在节点    编程
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/574809

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX