美文网首页
Cocos2d-x with Vungle iOS SDK fo

Cocos2d-x with Vungle iOS SDK fo

作者: 账房先生2016 | 来源:发表于2016-08-15 13:25 被阅读0次

1.创建Cocos XCODE工程
http://www.gamefromscratch.com/post/2014/09/29/Cocos2D-x-Tutorial-Series-Installation-Creating-a-Project-and-Hello-World.aspx

2.集成Vungle SDK
https://support.vungle.com/hc/en-us/articles/204430550-Get-Started-with-Vungle-iOS-SDK
备注:在集成时需要额外增加GameController和MediaPlayer两个框架,否则会报错

3.在Classes文件夹下,添加一个C++和OC的混编类,命名为:
VunglePubAd.h和VunglePubAd.mm
修改VunglePubAd.mm的Identity and Type-Type为Objective-C++ Source(否则会报错)

VunglePubAd.h代码如下:

#import <VungleSDK/VungleSDK.h>

class VunglePubAd
{
    public:static void showAd();
};

VunglePubAd.h代码如下:

#import "VunglePubAd.h"
void VunglePubAd::showAd()
{
    // Play a Vungle ad (with default options)
    VungleSDK* sdk = [VungleSDK sharedSDK];
    
    NSError *error;
    UIViewController *rootController=[[[[UIApplication sharedApplication]delegate] window] rootViewController];
    [sdk playAd:rootController error:&error];
    if (error) {
        NSLog(@"Error encountered playing ad: %@", error);
    }
}

游戏场景中调用播放方法代码为一下代码,并修改Identity and Type-Type为Objective-C++ Source(否则会报错)

#include "VunglePubAd.h"
VunglePubAd::showAd();

<VungleSDKDelegate>
可以在RootViewController中实现<VungleSDKDelegate>并在- (void)vungleSDKAdPlayableChanged:(BOOL)isAdPlayable回调中直接调用C++方法来控制广告播放按钮是否可用或显示
需要将RootViewController.mm的Identity and Type-Type为Objective-C++ Source(否则会报错)

- (void)vungleSDKAdPlayableChanged:(BOOL)isAdPlayable {
    if (isAdPlayable) {
        NSLog(@"An ad is available for playback");
        HelloWorld::showLog();
        
    } else {
        NSLog(@"No ads currently available for playback");
    }
}

github:
https://github.com/vincentyao2016/Cocos2dx-VungleSDK

相关文章

网友评论

      本文标题:Cocos2d-x with Vungle iOS SDK fo

      本文链接:https://www.haomeiwen.com/subject/isqjsttx.html