Cocos2d-xフレームワークに入っているplugin-xのAdMobプラグインを利用してiOS/Android両方に対して広告掲載を行います。
試した環境は以下のようになっています。
- Cocos2d-x v3.0 final
- plugin-x (4a8c94a85a08cddbea5e1bc499c50cce7518fb15)
最新だとAdMobのiOS側で64bit対応が入っていたので、今回は最新のものを使っています。
簡単にplugin-xプロジェクト内の説明をします。
cocos2d-x-3.x/plugin 以下が plugin-x の中身となります。利用するのは pluginsとprotocols。pluginsにはpluginとして用意されているサービスがフォルダ分けされており、使いたいパッケージをプロジェクトに含んで利用する形となります。
samples内にサンプルコードがあるので、各プラグインの利用方法はこちらを参照。
iOS対応
AdMobを表示する為に必要なframework
Xcodeを起動して対象プロジェクトの[TARGETS]-[General]-[Linked Frameworks and Libraries]に以下のframeworkを追加。
- MessageUI
- SystemConfiguration
- StoreKit
- CoreTelephony
- AdSupport
- ImageIO
ビルド設定変更
TARGETSから対象プロジェクトを選択し、[Build Setting]-[Other Linker Flags]に”-ObjC”を追加
plugins(AdMob)、protocolsをプロジェクトへ追加
plugin-xのplugins(AdMob)とprotocolsフォルダをXcode上に追加します。そのまま適当な場所へ追加しちゃってOKですが、[proj.android]は邪魔なので除去してください(2, 3箇所あります)。
Android対応
Eclipseで行う前提で書きます。
libPluginProtocol と libPluginAdmob をEclipseへインポート
[Import]-[Existing Android Code Into Workspace]からplugin-xフォルダを指定して、libPluginProtocol と libPluginAdmob だけをインポートします。
libPluginAdmobプロジェクトのPropertiesを開き、[Android]-[Library]欄でリンクエラーになっているのを削除し、先ほど追加 libPluginProtocol を追加します。
メインプロジェクトも[Properties]-[Android]-[Library]欄を開き、先ほど追加した libPluginAdmob を追加しておきます。
メインプロジェクト設定
主にビルド周りの設定となります。以下のファイルをもろもろ編集していく作業となります。追加ポイントにはコメントでplugin-x目印を付けています。
メインプロジェクト/jni/Android.mk
(略) LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../cocos2d/extensions LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static LOCAL_WHOLE_STATIC_LIBRARIES += cocostudio_static # plugin-x LOCAL_WHOLE_STATIC_LIBRARIES += PluginProtocolStatic include $(BUILD_SHARED_LIBRARY) $(call import-module,2d) $(call import-module,audio/android) $(call import-module,editor-support/cocostudio) $(call import-module,Box2D) # plugin-x $(call import-module,plugin/protocols/proj.android/jni)
メインプロジェクト/jni/hellcpp/main.cpp
#include "AppDelegate.h"
#include "cocos2d.h"
#include "CCEventType.h"
#include "platform/android/jni/JniHelper.h"
#include <jni .h="">
#include <android log.h="">
#include "PluginJniHelper.h" // plugin-x
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
using namespace cocos2d;
void cocos_android_app_init (JNIEnv* env, jobject thiz) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
// plugin-x
JavaVM* vm;
env->GetJavaVM(&vm);
PluginJniHelper::setJavaVM(vm);
}
メインプロジェクト/src/org/cocos2dx/cpp/AppActivity.java
package org.cocos2dx.cpp;
import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import org.cocos2dx.plugin.PluginWrapper;
public class AppActivity extends Cocos2dxActivity {
public Cocos2dxGLSurfaceView onCreateView() {
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
// TestCpp should create stencil buffer
glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
PluginWrapper.init(this);
PluginWrapper.setGLSurfaceView(glSurfaceView);
return glSurfaceView;
}
}
メインプロジェクト/AndroidManifest.xml
(略)
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:configchanges="orientation" android:label="@string/app_name" android:name="org.cocos2dx.cpp.AppActivity" android:screenorientation="portrait" android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<!-- Tell NativeActivity the name of our .so -->
<meta-data android:name="android.app.lib_name" android:value="cocos2dcpp">
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">
</category></action></intent-filter>
</meta-data></activity>
<!-- plugin-x(AdMob) -->
<activity android:configchanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:name="com.google.ads.AdActivity">
</activity></application>
</application>
<supports-screens android:anyDensity="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
<!-- plugin-x(AdMob) -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
AdMobプラグインを使って広告掲載
AdMobサイトでバナー広告のユニットを作成して、広告ユニットIDをiOS/Androidの2つ用意してください。取得したユニットIDを使い、以下のように掲載したいlayerで実行します。(サンプルコードほぼそのままな内容です)
.h ファイル
#include "cocos2d.h"
#include "ui/CocosGUI.h"
// plugin-x(AdMob)
#include "ProtocolAds.h"
using namespace cocos2d::plugin;
USING_NS_CC;
using namespace ui;
class MyAdsListener : public cocos2d::plugin::AdsListener
{
public:
virtual void onAdsResult(cocos2d::plugin::AdsResultCode code, const char* msg);
virtual void onPlayerGetPoints(cocos2d::plugin::ProtocolAds* pAdsPlugin, int points);
};
class TitleScene : public Layer {
public:
static Scene* createScene();
CREATE_FUNC(TitleScene);
virtual bool init();
private:
// plugin-x(AdMob)
ProtocolAds *admob;
MyAdsListener *listener;
TAdsInfo adInfo;
};
.cpp ファイル
// plugin-x(AdMob)
#include "PluginManager.h"
using namespace cocos2d::plugin;
bool TitleScene::init() {
if (!Layer::init()) {
return false;
}
// plugin-x(AdMob)
listener = new MyAdsListener();
admob = dynamic_cast<protocolads>(PluginManager::getInstance()->loadPlugin("AdsAdmob"));
TAdsDeveloperInfo devInfo;
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
devInfo["AdmobID"] = "ca-app-pub-0000000000000000/0000000000";
#elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
devInfo["AdmobID"] = "ca-app-pub-0000000000000000/1111111111";
#endif
admob->configDeveloperInfo(devInfo);
admob->setAdsListener(listener);
admob->setDebugMode(true); // TODO:Debug終了次第消す予定
// 種類を設定。値の内容は AdsAdmob.h を参照
adInfo["AdmobType"] = "1";
adInfo["AdmobSizeEnum"] = "1";
admob->showAds(adInfo, ProtocolAds::kPosBottom);
return true;
}
...
組み込むのが結構大変でしたが、一度導入手順を覚えておけば次から楽できるハズ!あとprotocolsを使ってバナーライクな出し方を他でしたい時に流用できそうですね。





0 件のコメント:
コメントを投稿