AdMob Interstitial Cocos2d-x WP8

时间:2015-02-16 00:42:03

标签: windows-phone-8 admob windows-phone-8.1 cocos2d-x c++-cx

可以告诉我如何在我的cocos2d-x游戏中的场景之间调用AdMob非页内广告吗?

我已经尝试了这个http://robwirving.com/2014/07/21/calling-c-methods-c-winrt-components/指南,但我不知道如何从cocos类中运行它。

还有其他方法或指南吗?

1 个答案:

答案 0 :(得分:4)

我最近成功了。你必须做一些事情。首先创建帮助程序类,它将帮助您调用本机函数(我在所有3个平台上使用它,但这里只是Windows手机):

NativeHelper.h:

#ifndef  __NATIVE_HELPER_H_
#define  __NATIVE_HELPER_H_

#include <string>
#include <functional>
#include "cocos2d.h"

using namespace std;
USING_NS_CC;

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
namespace cocos2d

{

    public delegate void CSharpShowInterstitialDelegate();

    public ref class WP8NativeEventHelper sealed

    {

    public:

        void WP8NativeEventHelper::SetCSharpShowInterstitialDelegate(CSharpShowInterstitialDelegate^ delegate){

            m_CSharpShowInterstitialDelegate = delegate;

        }

        void CallShowInterstitial();

    private:

        property static CSharpShowInterstitialDelegate^ m_CSharpShowInterstitialDelegate;

    };



}
#endif

class NativeHelper
{
public:
    static void showInterstitial(string adSdk);

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
    WP8NativeEventHelper^ wp8helper;
#endif

    //instance required only for setting callback
    static NativeHelper* getInstance();

    ~NativeHelper()
    {
        instanceFlag = false;
    }

private:

    static bool instanceFlag;
    static NativeHelper* instance;

    NativeHelper() {};

};

#endif // __NATIVE_HELPER_H_

因此。我们有特殊的C ++ / CX类Wp8NativeEventHelper,可以&#34; talk&#34;用C#。这里我们存储一个代表。

工作原理:

  1. C#正在调用SetCSharpShowInterstitialDelegate并将委托传递给它,这将在静态属性中记住。
  2. 然后C ++ \ CX可以使用CallShowInterstitial调用它。
  3. 现在NativeHelperWP.cpp:

    #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
    
    #ifndef  __NATIVE_HELPER_WP_H_
    #define  __NATIVE_HELPER_WP_H_
    
    #include "NativeHelper.h"
    
    void WP8NativeEventHelper::CallShowInterstitial(){
    
        if (m_CSharpShowInterstitialDelegate)
    
        {
    
            m_CSharpShowInterstitialDelegate->Invoke();
    
        }
    
    }
    
    bool NativeHelper::instanceFlag = false;
    NativeHelper* NativeHelper::instance = NULL;
    
    NativeHelper* NativeHelper::getInstance()
    {
        if(!instanceFlag){
            instance = new NativeHelper();
            instanceFlag = true;
            instance->wp8helper = ref new WP8NativeEventHelper();
        }
    
        return instance;
    }
    
    void NativeHelper::showInterstitial(){
        NativeHelper::getInstance()->wp8helper->CallShowInterstitial();
    }
    
    #endif
    
    #endif
    

    这里只是CallShowInterstitial的一个实现。同样在NativeHelper :: showInterstitial中,我们调用C ++ / CX,稍后调用c#。

    现在c#代码(MainPage.xaml.cs):

    外部命名空间:

    using GoogleAds;
    

    内课:

      private InterstitialAd interstitialAd;
    

    在构造函数中:

    WP8NativeEventHelper helper = new WP8NativeEventHelper();
    helper.SetCSharpShowInterstitialDelegate(showInterstitial);
    

    并创建showInterstitial函数:

    public void showInterstitial() //we recreate interstitial each time, because otherwise it'll show only once, only new requests won't work
    {
      interstitialAd = new InterstitialAd("MY_AD_UNIT_ID");
      AdRequest adRequest = new AdRequest();
    
      #if DEBUG
      // Enable test ads.
      adRequest.ForceTesting = true;
      #endif
    
    
      interstitialAd.ReceivedAd += OnAdReceived;
      interstitialAd.LoadAd(adRequest);
    }
    

    最后是OnAdReceived:

    private void OnAdReceived(object sender, AdEventArgs e)
    {
      System.Diagnostics.Debug.WriteLine("Ad received successfully");
      interstitialAd.ShowAd();
    }
    

    按照本指南设置admob:https://developers.google.com/mobile-ads-sdk/docs/admob/wp/quick-start

    现在让我们使用它。

    在HelloWorldScene.h中添加:

    #include "NativeHelper.h"
    

    在HelloWorldScene.cpp中:

    NativeHelper::showInterstitial();
    

    例如,您可以以相同的方式显示/隐藏/更改admob横幅的位置(但是它有问题,因此我使用广告调解)。