Class name: ATCocosSdk
| API | Parameter Description | Function Description |
|---|---|---|
| loadNativeAd | const char * placementId, cocos2d::ValueMap parameters | Used to load native ads unitId is the advertising slot id; parameters can set information for third-party platforms |
| showNativeAd | const char * placementId, std::string parameters | Display Native ads parameters specifies the location where native ads are displayed |
| showNativeAdInScenario | const char * placementId, std::string parameters, const char * scenario | Display Native ads with scenario id parameters are the locations where native ads are displayed, and scenario is the ad display scenario. Scenario parameters can be created from the background |
| removeNativeAd | const char * placementId | Remove native ads |
| isNativeAdReady | const char * placementId | Determine whether the ads in the specified ad slot have been loaded |
| checkNativeAdStatus | const char * placementId | Get the status of the current ad slot (Json string): 1. isLoading: whether it is loading 2. isReady: whether there is an ad cache (same function as isNativeAdReady) 3. AdInfo: the current highest priority ad cache information |
| setNativeAdListener | ATCocosNativeAdListener * listener, const char * placementId | Set callback object |
Interface: ATNativeListener
| API | Parameter Description | Function description |
|---|---|---|
| onNativeAdLoadSuccess | const char * placementId | Ad loading completed |
| onNativeAdLoadFailed | const char * placementId, const char * errorString | Ad loading failed errorMsg is the reason for the loading failure |
| onNativeAdShowWithExtra | const char * placementId, const char * extra | Ad slot display extra contains additional information about the current ad |
| onNativeAdClickWithExtra | const char * placementId, const char * extra | The ad slot generates a click extra contains other information about the current ad |
| onNativeAdVideoStartWithExtra | const char * placementId, const char * extra | Native video playback starts(different networks may support different) extra contains other information about the current ad |
| onNativeAdVideoEndWithExtra | const char * placementId, const char * extra | The native video playback ends(different networks may support different) extra contains other information about the current advertisement |
| onNativeAdCloseButtonTappedWithExtra | const char * placementId, const char * extra | Clicked the close button extra contains additional information about the current ad |
Assets object: ATNativeItemProperty
| API | Attribute description | Function description |
|---|---|---|
| ATNativeProperty | ATNativeItemProperty parent, ATNativeItemProperty icon, ATNativeItemProperty mainImage, ATNativeItemProperty title, ATNativeItemProperty desc, ATNativeItemProperty adLogo, ATNativeItemProperty cta, ATNativeItemProperty rating, ATNativeItemProperty dislike row | parent: parent layout, icon: App icon mainImage: large picture or media video title: title desc: description adLogo: advertising logo cta: CTA button rating: rating dislike: close button |
Layout object: ATNativeProperty
| API | Parameter description | Function description |
|---|---|---|
| ATNativeItemProperty | int x, int y, int width, int height, std::string backgroundColor, std::string textColor, int textSize, int isCustomClick | x: x coordinate of the upper left corner, y: y coordinate of the upper left corner width: width height: height backgroundColor: background color textColor: text Color textSize: Text size isCustomClick: Whether to download directly when clicked (valid only for Android and only for application ads of Tencent advertising platform) |
1. Load native ads
auto glView = Director::getInstance()->getOpenGLView();
auto frameSize = glView->getFrameSize();
int width = frameSize.width;
std::string widthStr = StringUtils::format("%d", width);
std::string heightStr = StringUtils::format("%d", width * 4 / 5);
cocos2d::ValueMap localExtra;
localExtra[ATCocosSdk::KEY_WIDTH] = widthStr;
localExtra[ATCocosSdk::KEY_HEIGHT] = heightStr;
ATCocosSdk::loadNativeAd(nativePlacementId, localExtra);2. Display native ads
Rect rect = Director::getInstance()->getOpenGLView()->getVisibleRect();
auto *glView = Director::getInstance()->getOpenGLView();
auto frameSize = glView->getFrameSize();
if (ATCocosSdk::isNativeAdReady(nativePlacementId)) {
auto glView = Director::getInstance()->getOpenGLView();
auto frameSize = glView->getFrameSize();
int width = frameSize.width;
int height = frameSize.height;
cocos2d::ValueMap rectDict;
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS // IOS
int ptHight = height/ATCocosSdk::getScreenScale();
int parentWidth = width/ATCocosSdk::getScreenScale();
int parentHeight = (width * 4/5)/ATCocosSdk::getScreenScale();
int appIconSize = (width / 7)/ATCocosSdk::getScreenScale();
int padding = (width / 35)/ATCocosSdk::getScreenScale();
ATNativeProperty property = ATNativeProperty();
property.parent = ATNativeItemProperty(0, ptHight - parentHeight, parentWidth, parentHeight, "#ffffff", "", 0);
property.cta = ATNativeItemProperty(parentWidth-appIconSize*2, parentHeight - appIconSize, appIconSize*2, appIconSize, "#2095F1" , "#000000", appIconSize/3);
property.icon = ATNativeItemProperty(0, parentHeight - appIconSize, appIconSize,appIconSize ,"clearColor", "",0);
property.mainImage = ATNativeItemProperty(padding ,padding, parentWidth-2*padding, parentHeight - appIconSize - 2*padding, "#ffffff", "#000000", 14);
property.title = ATNativeItemProperty(appIconSize + padding , parentHeight - appIconSize, parentWidth - 3* appIconSize -2 * padding, appIconSize/2 , "#ffffff" , "#000000", appIconSize/3);
property.desc = ATNativeItemProperty(appIconSize + padding , parentHeight - appIconSize/2, parentWidth - 3* appIconSize -2 * padding, appIconSize/2 , "#ffffff" , "#000000", appIconSize/4);
#endif
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID // android
int parentWidth = width;
int parentHeight = width * 4 / 5;
int appIconSize = width / 7;
int padding = width / 35;
ATNativeProperty property = ATNativeProperty();
property.parent = ATNativeItemProperty(0, height - parentHeight, parentWidth, parentHeight, "#ffffff", "", 0);
property.cta = ATNativeItemProperty(parentWidth-appIconSize*2, parentHeight - appIconSize, appIconSize*2, appIconSize, "#2095F1" , "#000000", appIconSize/3);
property.icon = ATNativeItemProperty(0, parentHeight - appIconSize, appIconSize,appIconSize ,"", "",0);
property.mainImage = ATNativeItemProperty(padding ,padding, parentWidth-2*padding, parentHeight - appIconSize - 2*padding, "#ffffff", "#000000", 14);
property.title = ATNativeItemProperty(appIconSize + padding , parentHeight - appIconSize, parentWidth - 3* appIconSize -2 * padding, appIconSize/2 , "" , "#000000", appIconSize/3);
property.desc = ATNativeItemProperty(appIconSize + padding , parentHeight - appIconSize/2, parentWidth - 3* appIconSize -2 * padding, appIconSize/2 , "#ffffff" , "#000000", appIconSize/4);
// property.adLogo = ATNativeItemProperty(0,0,0,0,"#ffffff","#ffffff",14);
// property.rating = ATNativeItemProperty(0,0,0,0,"#ffffff","#ffffff",14);
#endif
ATCocosSdk::showNativeAd(nativePlacementId, property.toJsonString());
} else {
CCLOG("ATCocosSdk::isNativeAdReady is false");
}3. Remove native ads
ATCocosSdk::removeNativeAd(nativePlacementId);