如何通过 ShareSDK 的 Unity3D 快速接入 Android/iOS 分享与授权 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MobService
V2EX    编程

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

  •  
  •   MobService 2019-06-17 17:56:44 +08:00 4358 次点击
    这是一个创建于 2367 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    目前尚无回复
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5174 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 33ms UTC 07:45 PVG 15:45 LAX 23:45 JFK 02:45
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86