Menu

Native Ads

1. Native advertising call instructions

1.1 API description

Class name: ATCocosSdk

APIParameter DescriptionFunction Description
loadNativeAdconst char * placementId, cocos2d::ValueMap parametersUsed to load native ads
unitId is the advertising slot id;
parameters can set information for third-party platforms
showNativeAdconst char * placementId, std::string parametersDisplay Native ads
parameters specifies the location where native ads are displayed
showNativeAdInScenarioconst char * placementId, std::string parameters, const char * scenarioDisplay 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
removeNativeAdconst char * placementIdRemove native ads
isNativeAdReadyconst char * placementIdDetermine whether the ads in the specified ad slot have been loaded
checkNativeAdStatusconst char * placementIdGet 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
setNativeAdListenerATCocosNativeAdListener * listener, const char * placementIdSet callback object

1.2 Listener callback method description

Interface: ATNativeListener

APIParameter DescriptionFunction description
onNativeAdLoadSuccessconst char * placementIdAd loading completed
onNativeAdLoadFailedconst char * placementId, const char * errorStringAd loading failed
errorMsg is the reason for the loading failure
onNativeAdShowWithExtraconst char * placementId, const char * extraAd slot display
extra contains additional information about the current ad
onNativeAdClickWithExtraconst char * placementId, const char * extraThe ad slot generates a click
extra contains other information about the current ad
onNativeAdVideoStartWithExtraconst char * placementId, const char * extraNative video playback starts(different networks may support different)
extra contains other information about the current ad
onNativeAdVideoEndWithExtraconst char * placementId, const char * extraThe native video playback ends(different networks may support different)
extra contains other information about the current advertisement
onNativeAdCloseButtonTappedWithExtraconst char * placementId, const char * extraClicked the close button
extra contains additional information about the current ad

1.3 Assets object description

Assets object: ATNativeItemProperty

APIAttribute descriptionFunction description
ATNativePropertyATNativeItemProperty 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

APIParameter descriptionFunction description
ATNativeItemPropertyint 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.4 Calling example

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);


Last modified: 2025-05-30Powered by