【问题标题】:AdMob Interstitial Cocos2d-x WP8AdMob 插页式 Cocos2d-x WP8
【发布时间】:2015-04-16 11:11:53
【问题描述】:

谁能告诉我如何在我的 cocos2d-x 游戏中的场景之间调用 AdMob Interstitial?

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

有没有其他方法,或者一些指南?

【问题讨论】:

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


【解决方案1】:

我最近成功了。你必须做的事情很少。首先创建帮助类,它将帮助您调用本机函数(我在所有 3 个平台上都使用它,但这里只是 windows phone):

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,它可以与 C#“对话”。我们在这里存储一个委托。

它是如何工作的:

  1. C# 正在调用 SetCSharpShowInterstitialDelegate 并向其传递一个委托,该委托将被记住在静态属性中。
  2. 然后 C++\CX 可以使用 CallShowInterstitial 调用它。

现在 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 横幅的位置(但是它有问题,所以我正在使用广告中介)。

【讨论】:

  • 哇,做得很好。谢谢,它似乎工作,但我有一些问题。本指南适用于 cocos2d v3.x?我正在使用 cocos2d 2.x,它给了我错误:输出文件名 PhoneDirect3DXamlAppComponent.winmd 应该与 .winmd 文件中的一种类型的命名空间 cocos2d 匹配。我应该移植到 cocos 3.x 吗?还是只使用 PhoneDirect3DXamlAppComponent 命名空间而不是 cocos2d?
  • 是的,这适用于 cocos2d-x 3.0。我不使用 2.0 版本,但我认为更改命名空间应该可以解决它。
  • 你知道cocos2d-x 3.6 中集成广告的方法吗?新模板不提供 C# 项目,XAML 文件也不同
  • 使用cocos控制台制作项目。它应该和以前一样。
  • 是的,这仅适用于 3.x 的早期版本。在 3.4 或 3.5 左右的某个时间点,Cocos2d-x 项目从 C++/CX(C# 和 C++)项目更改为仅 C++ 项目。可以为当前的 cocos2d-x 3.7 版创建一个 C++/CX 项目,但也可能有充分的理由将其删除并替换为 C++ 项目。我问 Microsoft Cocos2d-x 的 Dale Stammen,他是否知道为什么 C++/CX 项目从 Cocos2d-x 中被删除:discuss.cocos2d-x.org/t/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多