This demo project shows how to integrate the Unity Ads SDK into a Cocos2Dx iOS game
*Game original project provided by Raywenderlich - Jean-Yves Mengant - https://www.raywenderlich.com/33752
Create a Game Project on the Unity Ads Dashboard
- Log into the dashboard using your UDN Account
- Create a new game project
- Look for your iOS Game ID in the project, a 7-digit number that you will use in your integration
Download the Unity SDK from https://github.com/Applifier/unity-ads-sdk
- Download the SDK zip file
- Unzip the project, and locate UnityAds.framework and UnityAds.bundle
Import UnityAds.framework and UnityAds.bundle into your project
- Drag and drop the files into your project's file manager
- Select the box next to "Copy items if needed"
Make sure the following dependancies are enabled in your project
AdSupport.framework
AVFoundation.framework
CFNetwork.framework
CoreFoundation.framework
CoreMedia.framework
CoreTelephony.framework
StoreKit.framework
SystemConfiguration.framework
- Click your project settings
- select Build Phases > Link Binary With Library
- Click the + button > select the Framework > Click Add
Add C bridging header file
-
Right click on Cocos2DxFirstIosSample->ios folder, choose New File, then select Header File. Enter what ever file name you like, for example C-Interface
-
Add basic UnityAds wrapper functions declaration in the c header file
int UnityAdsInit (void *parameter); int UnityAdsCanShow (void *parameter); int UnityAdsShow (void *parameter);
Implement Unity Ads wrapper functions in your AppController
-
Add
#import "MyObject-C-Interface.h"
in the top of AppController -
Add below implementation in the bottom of AppController
int UnityAdsInit (void *gameIdP) { return 1; } int UnityAdsCanShow (void *zoneStringP) { return 1; } int UnityAdsShow (void* zoneStringP) { return 1; }
Add UnityAds to your AppController
-
Import UnityAds header file
#import <UnityAds/UnityAds.h>
in AppController.h file -
Make your AppController conform to UnityAdsDelegate protocol
@interface AppController : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate, UIApplicationDelegate, UnityAdsDelegate> {
-
Implement UnityAdsDelegate's @required callback in AppController
#pragma UnityAdsDelegate methods - (void)unityAdsVideoCompleted:(NSString*)rewardItemKey skipped:(BOOL)skipped { }
-
Initialize UnityAds in UnityAdsInit function, we are passing in the gameId parameter from cpp class, so no need to worry what is it now
UIApplication* app = [UIApplication sharedApplication]; AppController* controller = (AppController*)[app delegate]; UIViewController* rootController = [controller.window rootViewController]; NSString* gameId = [NSString stringWithFormat:@"%s", gameIdP]; [[UnityAds sharedInstance] startWithGameId:gameId andViewController:rootController];
-
Set AppController as UnityAds' delegate
[[UnityAds sharedInstance] setDelegate:controller];
-
Replace return 1; with below code snippet
NSString* zoneId = [NSString stringWithFormat:@"%s", zoneStringP]; [[UnityAds sharedInstance] setZone:zoneId]; if ([[UnityAds sharedInstance] canShow]) { [[UnityAds sharedInstance] show]; return 1; } else { return 0; }
- Open HelloWorldScene.cpp and add #include "MyObject-C-Interface.h"` in the top
-
Open AppDelegate.h and in the public: area, add virtual void rewardItemOne();
-
Open AppDelegate.cpp and add below code in the bottom of this class
void AppDelegate::rewardItemOne() { CCScene *scene = CCDirector::sharedDirector()->getRunningScene(); HelloWorld* gameScene = dynamic_cast<HelloWorld*>(scene->getChildren()->objectAtIndex(0)); if(gameScene != NULL) { gameScene->rewardItemOne(); } }
-
Open HelloWorldScene.h, in the pblic: area, add virtual void rewardItemOne();
-
Open HelloWorldScene.cpp, in the bottom in this class, add below implementation
void HelloWorld::rewardItemOne() { CCLOG("[UnityAds cpp] rewarded"); }
-
Open HelloWorldScene.cpp and create a new CCMenuItemLabel
CCMenuItemLabel *resultItem = CCMenuItemLabel::create(label, this, menu_selector(HelloWorld::resultTapped) );
-
Replace original CCLabel to below
resultItem->setScale(0.1); resultItem->setPosition(ccp(winSize.width/2 , winSize.height*0.6)); menu->addChild(resultItem); resultItem ->runAction(CCScaleTo::create(0.5, 1.0));
-
Add resultTapped function
void HelloWorld::resultTapped() { char* zoneString = "rewardedVideo"; UnityAdsShow(zoneString); }
-
Before calling UnityAdsShow, we must make sure the ad is initialized, in beginning, add below code
char* gameIdString = "1003843"; UnityAdsInit(gameIdString);
NOTE: The game ID in the example project is 1003843, you need to replace this number with your own game ID
Arrived here, you should already able to show an ad by tapping resultItem.
The idea is we want to reward the player by giving additional life in the next play.
-
Add int _extraLives = 0; definition in HelloWorldScene.cpp
-
Move resultItem declaration to the header file, in the private: area
CCMenuItemLabel *_resultItem;
-
Add extra life to _lives
_lives = 1+_extraLives; _extraLives = 0;
-
Add up an extra life after the player watched a video in HelloWorld::rewardItemOne function
_extraLives = 1;
-
Modify resultItem's' implementation, only appear if there isn't extra life.
if(_extraLives <= 0) { strcpy(message,"You Win"); if ( endReason == KENDREASONLOSE) strcpy(message,"Watch video to get extra life"); CCLabelBMFont * label ; label = CCLabelBMFont::create(message, "Arial.fnt"); _resultItem = CCMenuItemLabel::create(label, this, menu_selector(HelloWorld::resultTapped) ); _resultItem->setScale(0.1); _resultItem->setPosition(ccp(winSize.width/2 , winSize.height*0.6)); menu->addChild(_resultItem); _resultItem ->runAction(CCScaleTo::create(0.5, 1.0)); }
-
Add hide _resultItem logic in rewardItemOne function
void HelloWorld::rewardItemOne() { _extraLives = 1; if(_resultItem != NULL) { _resultItem->removeFromParentAndCleanup(true); _resultItem = NULL; } CCLOG("[UnityAds cpp] rewarded"); }
Finally, we finished a rewarded video. Player would enjoy more after watching a video for an extra life in the next play.
For more information, check out the iOS Integration Guide, the support Forum, or contact [email protected]