Cocos2DX 简介
Cocos2d-x是一套成熟的开源跨平台游戏开发框架。其引擎提供了图形渲染、GUI、音频、网络、物理、用户输入等丰富的功能,被广泛应用于游戏开发及交互式应用的构建。引擎的核心采用C++ 编写,支持使用C++、Lua或者JavaScript进行开发。同时Cocos2d-x可以适配IOS、Android、HTML5、Windows和Mac系统。
Cocos2d-x在中国及全球都有一定的市场份额,为了给开发出来的游戏增加知名度,更好的进行营销,社交分享功能是必不可少的。但是所要分享的平台针对全球不同的区域有不同的侧重点,这就需要短时间内接入多个社交平台,但是每个平台的接入时间成本按天计算,多个平台叠加在一起的时间就很不乐观。所以使用Cocos2d-x引擎的开发人员急需一种能够让开发者快速接入授权、分享功能的SDK。
Cocos2d-x集成ShareSDK过程如下:
- 产品主页网址:http://www.mob.com/product/sharesdk
- 官方插件地址:https://github.com/MobClub/New-C2DX-For-ShareSDK
Android 集成
首先下载官方插件,下载完毕之后需要做如下几步工作
-
把ShareSDK的cocos2dx的proj.android-studio项目里的libs包复制到你的项目里,除了armeabi目录不用复制其他都要。
-
把ShareSDK的cocos2dx的New-C2DX-For-ShareSDK项目里的Classes目录下的文件都复制到你的项目里的Classes目录下。
-
在jni/Android.mk添加下面代码来引入相关文件,如:
../../Classes/AppDelegate.cpp ../../Classes/HelloWorldScene.cpp ../../Classes/C2DXShareSDK/Android/ShareSDKUtils.cpp ../../Classes/C2DXShareSDK/C2DXShareSDK.cpp ../../Classes/C2DXShareSDK/Android/JSON/CCJSONConverter.cpp ../../Classes/C2DXShareSDK/Android/JSON/cJSON/cJSON.c LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes $(LOCAL_PATH)/../../Classes/C2DXShareSDK $(LOCAL_PATH)/../../Classes/C2DXShareSDK/Android $(LOCAL_PATH)/../../Classes/C2DXShareSDK/Android/JSON $(LOCAL_PATH)/../../Classes/C2DXShareSDK/Android/JSON/cJSON
-
在proj.android-studio工程下,app目录下,build.gradle文件内,寻找到dependencies标签,配置如下的代码
compile project(‘:libcocos2dx‘)
-
在主activity里(一般在cocos项目的此目录下面:\proj.android-studio\src\org\cocos2dx\cpp\AppActivity.java)的onCreate方法里添加ShareSDKUtils.prepare()方法。
protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); ShareSDKUtils.prepare(); }
-
各个平台的配置方式:平台的配置在libcocos2dx这个依赖工程内,工程根目录下边有一个MobSDK.gradle文件,写法示例如下边的代码所示,用户需要用自己申请到的Key来替换下文中的key。平台的选择也根据自己的需要来进行增加删除。
apply plugin: ‘com.mob.sdk‘ MobSDK { appKey "moba6b6c6d6" appSecret "b89d2427a3bc7ad1aea1e1e8c1d36bf3" ShareSDK { //平台配置信息 devInfo { SinaWeibo { id 1 sortId 1 appKey "568898243" appSecret "38a4f8204cc784f81f9f0daaf31e02e3" callbackUri "http://www.sharesdk.cn" shareByAppClient true enable true } QZone { id 3 sortId 3 appId "100371282" appKey "aed9b0303e3ed1e27bae87c33761161d" shareByAppClient true bypassApproval false enable true } QQ { id 7 sortId 7 appId "100371282" appKey "aed9b0303e3ed1e27bae87c33761161d" shareByAppClient true bypassApproval false enable true } Wechat { id 4 sortId 4 appId "wx4868b35061f87885" appSecret "64020361b8ec4c99936c0e3999a9f249" userName "gh_b0c6a9ca668a" path "pages/index/index?id=mob" withShareTicket true miniprogramType 2 bypassApproval false enable true } WechatMoments { id 5 sortId 5 appId "wx4868b35061f87885" appSecret "64020361b8ec4c99936c0e3999a9f249" bypassApproval false enable true } WechatFavorite { id 6 sortId 6 appId "wx4868b35061f87885" appSecret "64020361b8ec4c99936c0e3999a9f249" bypassApproval false enable true } Facebook { id 8 sortId 8 appKey "1412473428822331" appSecret "a42f4f3f867dc947b9ed6020c2e93558" callbackUri "https://mob.com" shareByAppClient true enable true } Twitter { id 9 sortId 9 appKey "viOnkeLpHBKs6KXV7MPpeGyzE" appSecret "NJEglQUy2rqZ9Io9FcAU9p17omFqbORknUpRrCDOK46aAbIiey" callbackUri "http://mob.com" shareByAppClient true enable true } } } }
iOS 集成
-
将下面红色方框的资源文件拖到Cocos2d-x项目中
-
将SDK文件夹拖进项目(下载地址) 注意:请务必在上述步骤中选择“Create groups for any added folders”单选按钮组。如果你选择“Create folder references for any added folders”,一个蓝色的文件夹引用将被添加到项目并且将无法找到它的资源。
-
添加必须的依赖库
- libc++.tbd
- libz.tbd
- libsqlite3.tbd
各社交平台所需依赖库:
新浪微博:
- ImageIO.framework
- Photos.framework
Instagram:
- AssetsLibrary.framework
- Photos.framework
美拍:
- AssetsLibrary.framework
添加依赖库的方法如下
-
配置UrlScheme和白名单等,各个社交平台需要的配置可以参考快速集成文档中的URL Scheme和白名单配置项。
-
在项目工程的Info.plist 中如图增加 MOBAppKey 和 MOBAppSecret 两个字段
在 MOBAppKey中 设置ShareSDK的appKey,如果尚未在ShareSDK官网注册过App,请移步到 登录后台 进行应用注册也可以点击 链接 看里面的操作步骤。
代码实现
-
初始化: 打开Classes目录下边的AppDelegate.cpp文件,直接拷贝粘贴代码进行初始化工作。
导入头文件
# include "C2DXShareSDK.h"
初始化
bool AppDelegate::applicationDidFinishLaunching() { //初始化ShareSDK this->initShareSDKConfig(); } void AppDelegate::initShareSDKConfig() { //设置平台配置 //Platforms __Dictionary *totalDict = __Dictionary::create(); //新浪微博 __Dictionary *sinaWeiboConf= __Dictionary::create(); sinaWeiboConf->setObject(__String::create("568898243"), "app_key"); sinaWeiboConf->setObject(__String::create("38a4f8204cc784f81f9f0daaf31e0 2e3"), "app_secret"); sinaWeiboConf->setObject(__String::create("http://www.sharesdk.cn"), "re direct_uri"); stringstream sina; sina << cn::sharesdk::C2DXPlatTypeSinaWeibo; totalDict->setObject(sinaWeiboConf, sina.str()); //微信 __Dictionary *wechatConf = __Dictionary::create(); wechatConf->setObject(__String::create("wx4868b35061f87885"), "app_id"); wechatConf->setObject(__String::create("64020361b8ec4c99936c0e3999a9f249 "), "app_secret"); stringstream wechat; wechat << cn::sharesdk::C2DXPlatTypeWechatPlatform; totalDict->setObject(wechatConf, wechat.str()); //QQ __Dictionary *qqConf = __Dictionary::create(); qqConf->setObject(__String::create("100371282"), "app_id"); qqConf->setObject(__String::create("aed9b0303e3ed1e27bae87c33761161d"), "app_key"); stringstream qq; qq << cn::sharesdk::C2DXPlatTypeQQPlatform; totalDict->setObject(qqConf, qq.str()); //Facebook __Dictionary *fbConf = __Dictionary::create(); fbConf->setObject(__String::create("107704292745179"), "api_key"); fbConf->setObject(__String::create("38053202e1a5fe26c80c753071f0b573"), "app_secret"); stringstream facebook; facebook << cn::sharesdk::C2DXPlatTypeFacebook; totalDict->setObject(fbConf, facebook.str()); //Twitter __Dictionary *twConf = __Dictionary::create(); twConf->setObject(__String::create("LRBM0H75rWrU9gNHvlEAA2aOy"), "consumer_key"); twConf->setObject(__String::create("gbeWsZvA9ELJSdoBzJ5oLKX0TU09UOwrzdGfo9Tg7DjyGuMe8G"), "consumer_secret"); twConf->setObject(__String::create("http://www.mob.com"), "redirect_uri" ) ; stringstream twitter; twitter << cn::sharesdk::C2DXPlatTypeTwitter; totalDict->setObject(twConf, twitter.str()); cn::sharesdk::C2DXShareSDK::registerAppAndSetPlatformConfig("moba6b6c6d6","b89d2427a3bc7ad1aea1e1e8c1d36bf3",totalDict); }
以上平台的app_key、app_secret等字段不同分享平台可能不同,详情可参考:《统一表》。
-
授权功能:
C2DXShareSDK::authorize(C2DXPlatTypeSinaWeibo, authResultHandler);
authResultHandler是默认定制的一个回调类,也可以自己写一个新的回调类来替代此类
//授权回调 void authResultHandler(int seqId, cn::sharesdk::C2DXResponseState state, cn::sharesdk::C2DXPlatType platType, __Dictionary *result) { switch (state) { case cn::sharesdk::C2DXResponseStateSuccess: { log("Success"); //输出信息 try { __Array *allKeys = result -> allKeys(); allKeys->retain(); for (int i = 0; i < allKeys -> count(); i++) { __String *key = (__String *)allKeys -> getObjectAtIndex(i); Ref *obj = result -> objectForKey(key -> getCString()); log("key = %s", key -> getCString()); if (dynamic_cast<__String *>(obj)) { log("value = %s", dynamic_cast<__String *>(obj) -> getCString()); } else if (dynamic_cast<__Integer *>(obj)) { log("value = %d", dynamic_cast<__Integer *>(obj) -> getValue()); } else if (dynamic_cast<__Double *>(obj)) { log("value = %f", dynamic_cast<__Double *>(obj) -> getValue()); } } allKeys->release(); } catch(...) { log("==============error"); } } break; case cn::sharesdk::C2DXResponseStateFail: { log("Fail"); //回调错误信息 __Array *allKeys = result->allKeys(); allKeys->retain(); for (int i = 0; i < allKeys-> count(); i++) { __String *key = (__String*)allKeys->getObjectAtIndex(i); Ref *obj = result->objectForKey(key->getCString()); log("key = %s", key -> getCString()); if (dynamic_cast<__String *>(obj)) { log("value = %s", dynamic_cast<__String *>(obj) -> getCString()); } else if (dynamic_cast<__Integer *>(obj)) { log("value = %d", dynamic_cast<__Integer *>(obj) -> getValue()); } else if (dynamic_cast<__Double *>(obj)) { log("value = %f", dynamic_cast<__Double *>(obj) -> getValue()); } } } break; case cn::sharesdk::C2DXResponseStateCancel: { log("Cancel"); } break; default: break; } }
-
获取用户的信息
授权之后,可以通过如下的方法来获取用户信息。
C2DXShareSDKC ::getUserInfo(C2DXPlatTypeSinaWeibo, getUserResultHandler);
-
分享
-
在需要分享操作的代码块中进行构造分享参数,示例如下:
__Dictionary *content = __Dictionary::create(); content -> setObject(__String::create("分享文本"), "text"); // 分享文本 content -> setObject(__String::create("HelloWorld.png"), "image");// 分享图片 content -> setObject(__String::create("测试标题"), "title"); // 分享标题 content -> setObject(__String::create("http://www.mob.com"), "url"); // 分享url content -> setObject(__String::createWithFormat("%d", cn::sharesdk::C2DXContentTypeWebPage), "type"); /
构造分享内容字段参数值请参考:《分享内容字段值表》
-
调用分享方法:
//弹出菜单 C2DXShareSDK::showShareMenu(NULL,content,100,100, shareContentResultHandler);// 第3,4个参数传入 iPad 视图要显示的坐标点,详见API说明 //无UI,直接分享 C2DXShareSDK::shareContent(cn::sharesdk::C2DXPlatTypeSinaWeibo, content, shareContentResultHandler); //弹出编辑界面分享 C2DXShareSDK::showShareView(cn::sharesdk::C2DXPlatTypeSinaWeibo, content, shareContentResultHandler);
-
设置分享回调方法 shareContentResultHandler,示例如下:
//分享回调 void shareContentResultHandler(int seqId, cn::sharesdk::C2DXResponseState state, cn::sharesdk::C2DXPlatType platType, __Dictionary *result) { switch (state) { case cn::sharesdk::C2DXResponseStateSuccess: { log("Success"); } break; case cn::sharesdk::C2DXResponseStateFail: { log("Fail"); //回调错误信息 __Array *allKeys = result->allKeys(); allKeys->retain(); for (int i = 0; i < allKeys-> count(); i++) { __String *key = (__String*)allKeys->getObjectAtIndex(i); Ref *obj = result->objectForKey(key->getCString()); log("key = %s", key -> getCString()); if (dynamic_cast<__String *>(obj)) { log("value = %s", dynamic_cast<__String *>(obj) -> getCString()); } else if (dynamic_cast<__Integer *>(obj)) { log("value = %d", dynamic_cast<__Integer *>(obj) -> getValue()); } else if (dynamic_cast<__Double *>(obj)) { log("value = %f", dynamic_cast<__Double *>(obj) -> getValue()); } } } break; case cn::sharesdk::C2DXResponseStateCancel: { log("Cancel"); } break; default: break; } }