Menu

Native

Note for Android:
Currently, using Unity to directly build an Android APK cannot display native video ads, because Unity's default APK packaging disables hardware acceleration for the game Activity, making it impossible to display videos. If you need to display native ad videos, you must use the export project method, as follows:

(1) Select Export Project mode to export the Android project

(2) Modify the property in AndroidManifest, setting the hardwareAccelerated property as shown in the image to true

After completing the above steps, use the current Android project for packaging.

1. Integration Process Suggestions

  1. Call ATNativeAd#loadNativeAd to load an ad when the app starts
  2. Use ATNativeAd#hasAdReady to check if an ad can be displayed at the location where you want to show it
  3. If you need to use ad scenarios to distinguish data for different business scenarios, see the sample code for details

2. API Description

ATNativeAd:

API Parameters Description
loadNativeAd string placementid, Dictionary<string,string> extra Load an ad (Starting from v5.6.8, for Pangle template rendering, Mintegral auto-rendering ads, etc., you must pass width and height via the extra parameter, otherwise the ad size may display abnormally)
setListener ATNativeAdListener listener Set the listener callback interface. (Deprecated after version 5.9.51) For how to set listeners, see: Native Ad Event Settings
hasAdReady string placementid Check if there is a cached ad
checkAdStatus string placementid (Added in v5.7.22) Get the current ad placement status (JSON string): 1. isLoading: Whether it is loading 2. isReady: Whether there is a cached ad (same as hasAdReady) 3. AdInfo: The highest-priority cached ad info (see ATCallbackInfo Description)
getValidAdCaches string placementid (Added in v5.7.54) Get all successfully loaded cached ad info (JSON string) (see ATCallbackInfo Description)
renderAdToScene string placementid, ATNativeAdView anyThinkNativeAdView Display an ad
entryScenarioWithPlacementID string placementId, string scenarioID Set entry into a displayable ad scenario

You can use the following code to load a native ad:

java Copy
public void loadNative() 
{
        Debug.Log ("Developer load native, unit id = " + mPlacementId_native_all);
           ATNativeAd.Instance.client.onAdLoadEvent += onAdLoad;
           ATNativeAd.Instance.client.onAdLoadFailureEvent += onAdLoadFail;
        ATNativeAd.Instance.client.onAdImpressEvent += onAdImpressed;
        ATNativeAd.Instance.client.onAdClickEvent += onAdClick;
        ATNativeAd.Instance.client.onAdCloseEvent += onAdClose;
        ATNativeAd.Instance.client.onAdVideoStartEvent += onAdVideoStart;
        ATNativeAd.Instance.client.onAdVideoEndEvent += onAdVideoEnd;
        ATNativeAd.Instance.client.onAdVideoProgressEvent += onAdVideoProgress;

        //----- v5.6.8 and above -----
        Dictionary<string, object> jsonmap = new Dictionary<string, object>();

        #if UNITY_ANDROID
            ATSize nativeSize = new ATSize(width, height);
            jsonmap.Add(ATNativeAdLoadingExtra.kATNativeAdLoadingExtraNativeAdSizeStruct, nativeSize);
        #elif UNITY_IOS || UNITY_IPHONE
            ATSize nativeSize = new ATSize(width, height, false);
            jsonmap.Add(ATNativeAdLoadingExtra.kATNativeAdLoadingExtraNativeAdSizeStruct, nativeSize);

        ATNativeAd.Instance.loadNativeAd(mPlacementId_native_all, jsonmap);
}

Note: Please continue reading to learn how to get notified on load success/failure events.

java Copy
ATNativeAd.Instance.hasAdReady(mPlacementId_native_all);

You can use the following code to display a native ad:

java Copy
public void showNative()
{
        Debug.Log ("Developer show native....");
        ATNativeConfig conifg = new ATNativeConfig ();

        string bgcolor = "#ffffff";
        string textcolor = "#000000";
        int rootbasex = 100, rootbasey = 100;

        int x = rootbasex,y = rootbasey,width = 300*3,height = 200*3,textsize = 17;
        conifg.parentProperty = new ATNativeItemProperty(x,y,width,height,bgcolor,textcolor,textsize, true);

        //adlogo 
        x = 0*3;y = 0*3;width = 30*3;height = 20*3;textsize = 17;
        conifg.adLogoProperty  = new ATNativeItemProperty(x,y,width,height,bgcolor,textcolor,textsize, true);

        //adicon
        x = 0*3;y = 50*3-50;width = 60*3;height = 50*3;textsize = 17;
        conifg.appIconProperty  = new ATNativeItemProperty(x,y,width,height,bgcolor,textcolor,textsize, true);

        //ad cta 
        x = 0*3;y = 150*3;width = 300*3;height = 50*3;textsize = 17;
        conifg.ctaButtonProperty  = new ATNativeItemProperty(x,y,width,height,"#ff21bcab","#ffffff",textsize, true);

        //ad desc
        x = 60*3;y = 100*3;width = 240*3-20;height = 50*3-10;textsize = 10;
        conifg.descProperty  = new ATNativeItemProperty(x,y,width,height,bgcolor,"#777777",textsize, true);

        //ad image
        x = 60*3;y = 0*3+20;width = 240*3-20;height = 100*3-10;textsize = 17;
        conifg.mainImageProperty  = new ATNativeItemProperty(x,y,width,height,bgcolor,textcolor,textsize, true);

        //ad title 
        x = 0*3;y = 100*3;width = 60*3;height = 50*3;textsize = 12;
        conifg.titleProperty  = new ATNativeItemProperty(x,y,width,height,bgcolor,textcolor,textsize, true);

        // (Added in v5.7.21) ad dislike button (close button)
        x = 300*3 - 75;y = 0;width = 75;height = 75;
        conifg.dislikeButtonProperty  = new ATNativeItemProperty(x,y,width,height,"#00000000",textcolor,textsize, true);

        ATNativeAdView anyThinkNativeAdView = new ATNativeAdView(conifg);
        AnyThinkAds.Demo.ATManager.anyThinkNativeAdView = anyThinkNativeAdView;
        Debug.Log("Developer renderAdToScene--->");
        ATNativeAd.Instance.renderAdToScene(mPlacementId_native_all, anyThinkNativeAdView);
}

When using the scenario feature:

java Copy
public void showNative()
{
    ...
    Dictionary<string, string> jsonmap = new Dictionary<string, string>();
    jsonmap.Add(AnyThinkAds.Api.ATConst.SCENARIO, showingScenarioID);
    ATNativeAd.Instance.renderAdToScene(mPlacementId_native_all, anyThinkNativeAdView, jsonmap);
}

The trailing parameter passed to the constructor of the ATNativeItemProperty class indicates whether to use pixel units (only valid for iOS). For example, on iPhone 6, if you pass 30, 120, 300, 450 for x, y, width, and height respectively, the actual values passed to the Objective-C code on iPhone 7 will be 15, 60, 150, 225, and on another device, these values will be 10, 40, 100, 150. In other words, the final values will be determined by the screen scale of the target device.

As you can see above, we have defined an ATNativeConfig class for you to configure various properties of native assets (bgColor, textColor, textSize, position, etc.), such as the CTA button, app icon, title text, description text, cover image, and so on. Feel free to modify the properties in the config object and see what happens based on your modifications.

Detailed Description of ATNativeConfig & ATNativeItemProperty

ATNativeConfig contains multiple ATNativeItemProperty objects used to control the style of the Native ad. ATNativeItemProperty controls the position and style of a single Native ad element. Some elements may not support certain properties in ATNativeItemProperty. For example, image elements (icon, main image) only support x, y, width, height, usesPixel, but do not support background color, font size, and font color, while text elements (such as title, cta, desc) support all properties. In iOS games, if you want to specify "transparent", use "clearColor". For example, if you want the background of the ad area to be transparent, assign "clearColor" to the backgroundColor of parentProperty.
Note: 1) Only iOS supports clearColor. On Android, if you need to specify a transparent background, simply set the alpha component in rgba to 0.

  1. iOS does not support alpha, so color values should be passed as something like #5aef00 (a six-digit RGB value), while Android can support alpha, with colors containing 8 hexadecimal digits, such as 5a2b3c00.

parentProperty

parentProperty controls the overall size of the Native ad, as shown in the red-circled area below.

Native ad element descriptions:

  • appIconProperty: The appIconProperty controls the ad's icon property, as shown in Figure 1 below:
  • mainImageProperty: mainImageProperty controls the ad's cover image, as shown in Figure 2 below:
  • titleProperty: titleProperty controls the ad title, as shown in Figure 3 below:
  • descProperty: descProperty controls the ad description text, as shown in Figure 4 below
  • adLogoProperty: adLogoProperty controls the ad logo property, as shown in Figure 5 below. Note: Some platforms have a fixed ad logo position internally and do not support developer-specified positioning, such as Admob.
  • ctaButtonProperty: ctaButtonProperty controls the click button, as shown in Figure 6 below
  • dislikeButtonProperty: (Added in v5.7.21) dislikeButtonProperty controls the close button (can be left unset if you don't want to display the close button)

Note: All of the above Native ad elements must be rendered when returned

If you want to remove the native ad from the screen, use the following code:

java Copy
public void cleanView()
{
    Debug.Log ("Developer cleanView native....");
   ATNativeAd.Instance.cleanAdView(mPlacementId_native_all,AnyThinkAds.Demo.ATManager.anyThinkNativeAdView);
}

About Template Rendering Ad Description

  • Only parentProperty can be used to control template rendering ads. Adjusting other properties (appIcon, title, desc, adLogo, ctaButton, mainImage, etc.) is invalid. Among them, parentProperty and mainImageProperty must be set, and mainImageProperty parameters can be consistent with parentProperty.
  • The width and height of parentProperty must be consistent with the width and height passed during loading, otherwise the ad may display incompletely or too large.
  • Template ads have their own aspect ratio, which can be viewed in the ad platform dashboard. Try to select templates with the same or similar aspect ratio in the ad platform dashboard, and pass the width and height in the code with that aspect ratio to load and display ads for the best display effect.
  • Adaptive height for template rendering ads: Developers can implement adaptive height through the following steps (adaptive height during loading is only for Pangle and GDT platforms on Android). Note:
  • When using adaptive height, taller template ads may appear. Developers can check the desired aspect ratio templates in the ad platform dashboard based on actual needs and remove templates that do not meet expectations.
  • Adaptive height is not controlled by the height of parentProperty.
    1. (This point is only for Android) Enable adaptive height during loading (Key must be passed: ADAPTIVE_HEIGHT)
java Copy
Dictionary<string, object> jsonmap = new Dictionary<string, object>();

#if UNITY_ANDROID
    ATSize nativeSize = new ATSize(width, height);
    jsonmap.Add(ATNativeAdLoadingExtra.kATNativeAdLoadingExtraNativeAdSizeStruct, nativeSize);
    jsonmap.Add(AnyThinkAds.Api.ATConst.ADAPTIVE_HEIGHT, AnyThinkAds.Api.ATConst.ADAPTIVE_HEIGHT_YES);  
...
ATNativeAd.Instance.loadNativeAd(mPlacementId_native_all, jsonmap);
    1. Enable adaptive height during display (Key must be passed: Key: ADAPTIVE_HEIGHT)
java Copy
...
Dictionary<string, string> jsonmap = new Dictionary<string, string>();
jsonmap.Add(AnyThinkAds.Api.ATConst.ADAPTIVE_HEIGHT, AnyThinkAds.Api.ATConst.ADAPTIVE_HEIGHT_YES);

ATNativeAd.Instance.renderAdToScene(mPlacementId_native_all, anyThinkNativeAdView, jsonmap);
    1. During display, the ad can be controlled to be centered at the top or bottom of the screen
java Copy
...
Dictionary<string, string> jsonmap = new Dictionary<string, string>();
jsonmap.Add(AnyThinkAds.Api.ATConst.POSITION, AnyThinkAds.Api.ATConst.POSITION_BOTTOM);// Centered at bottom of screen
//jsonmap.Add(AnyThinkAds.Api.ATConst.POSITION, AnyThinkAds.Api.ATConst.POSITION_TOP);// Centered at top of screen

ATNativeAd.Instance.renderAdToScene(mPlacementId_native_all, anyThinkNativeAdView, jsonmap);

6. Implement Native Ad Listeners (Note: Only supported on V5.9.51 and above)

For callback info details, see: Callback Info Description

Use the following code to implement multiple listeners:

java Copy
        // Ad loaded successfully
           ATNativeAd.Instance.client.onAdLoadEvent += onAdLoad;
           // Ad load failed
        ATNativeAd.Instance.client.onAdLoadFailureEvent += onAdLoadFail;
        // Ad displayed successfully
        ATNativeAd.Instance.client.onAdImpressEvent += onAdImpressed;
        // Ad clicked
        ATNativeAd.Instance.client.onAdClickEvent += onAdClick;
        // Ad close button clicked, some ad platforms have this callback
        ATNativeAd.Instance.client.onAdCloseEvent += onAdClose;
        // Ad video playback started, some ad platforms have this callback
        ATNativeAd.Instance.client.onAdVideoStartEvent += onAdVideoStart;
        // Ad video playback ended, some ad platforms have this callback
        ATNativeAd.Instance.client.onAdVideoEndEvent += onAdVideoEnd;
        // Ad video playback progress, some ad platforms have this callback
        ATNativeAd.Instance.client.onAdVideoProgressEvent += onAdVideoProgress;

Advanced listener settings:

java Copy
        // Ad Source started loading
        ATNativeAd.Instance.client.onAdSourceAttemptEvent += startLoadingADSource;
        // Ad Source loading completed
        ATNativeAd.Instance.client.onAdSourceFilledEvent += finishLoadingADSource;
        // Ad Source failed
        ATNativeAd.Instance.client.onAdSourceLoadFailureEvent += failToLoadADSource;
        // Ad Source started bidding
        ATNativeAd.Instance.client.onAdSourceBiddingAttemptEvent += startBiddingADSource;
        // Ad Source bidding succeeded
        ATNativeAd.Instance.client.onAdSourceBiddingFilledEvent += finishBiddingADSource;
        // Ad Source bidding failed
        ATNativeAd.Instance.client.onAdSourceBiddingFailureEvent += failBiddingADSource;    

The method definition parameters are as follows (Note: You can refer to the code below for method names or use custom method names, but the parameters must be consistent):

java Copy
    //sender is the ad type object, erg is the returned info
   // Ad loaded successfully
    public void onAdLoad(object sender, ATAdEventArgs erg)
    {
        Debug.Log("Developer onAdLoaded------:" + erg.placementId);
    }
    // Ad load failed
    public void onAdLoadFail(object sender, ATAdEventArgs erg)
    {
        Debug.Log("Developer onAdLoadFail------:" + erg.placementId + "--code:" + erg.code + "--msg:" + erg.message);
    }
    // Ad displayed successfully
    public void onAdImpressed(object sender, ATAdEventArgs erg)
    {
        Debug.Log("Developer onAdImpressed------:" + erg.placementId);
    }
    // Ad clicked
    public void onAdClicked(object sender, ATAdEventArgs erg)
    {
        Debug.Log("Developer onAdClicked------:" + erg.placementId);
    }
    // Ad video playback started, some ad platforms have this callback
    public void onAdVideoStart(object sender, ATAdEventArgs erg)
    {
        Debug.Log("Developer onAdVideoStart------:" + erg.placementId);
    }
    // Ad video playback ended, some ad platforms have this callback
    public void onAdVideoEnd(object sender, ATAdEventArgs erg)
    {
        Debug.Log("Developer onAdVideoEnd------:" + erg.placementId);
    }
    // Ad video playback progress, some ad platforms have this callback
    public void onAdVideoProgress(object sender, ATAdProgressEventArgs erg)
    {
        Debug.Log("Developer onAdVideoProgress------:" + erg.placementId);
    }
    // Ad close button clicked, some ad platforms have this callback
    public void onAdCloseButtonClicked(object sender, ATAdEventArgs erg)
    {
        Debug.Log("Developer onAdCloseButtonClicked------:" + erg.placementId);
   }

      // v5.8.10 added Ad Source-level callbacks
      // Ad Source started loading
    public void startLoadingADSource(object sender, ATAdEventArgs erg){
        Debug.Log("Developer startLoadingADSource------");
    }
      // Ad Source loading completed
        public void finishLoadingADSource(string placementId, ATCallbackInfo callbackInfo){
        Debug.Log("Developer finishLoadingADSource------");
    }
      // Ad Source failed
        public void failToLoadADSource(object sender, ATAdEventArgs erg){
        Debug.Log("Developer failToLoadADSource------");
    }
      // Ad Source started bidding
        public void startBiddingADSource(object sender, ATAdEventArgs erg){
               Debug.Log("Developer startBiddingADSource------");
    }
      // Ad Source bidding succeeded
        public void finishBiddingADSource(object sender, ATAdEventArgs erg){
        Debug.Log("Developer finishBiddingADSource------");
    }
      // Ad Source bidding failed
        public void failBiddingADSource(object sender, ATAdEventArgs erg){
        Debug.Log("Developer failBiddingADSource------");
    }

7. Legacy Native Ad Listener Implementation (Note: Only for use below V5.9.51, deprecated in new versions)

To get notified about various native ad events (load success/failure, impression, click, etc.), you can define an implementation class of ATNativeAdListener. Below is an example: (Note: For Android, all callback methods are not on the Unity main thread)

java Copy
class ATNativeCallbackListener : ATNativeAdListener
{
    // Ad loaded successfully
    public void onAdLoaded(string placementId)
    {
        Debug.Log("Developer onAdLoaded------:" + placementId);
    }
    // Ad load failed
    public void onAdLoadFail(string placementId, string code, string message)
    {
        Debug.Log("Developer onAdLoadFail------:" + placementId + "--code:" + code + "--msg:" + message);
    }
    // Ad displayed successfully
    public void onAdImpressed(string placementId, ATCallbackInfo callbackInfo)
    {
        Debug.Log("Developer onAdImpressed------:" + placementId);
    }
    // Ad clicked
    public void onAdClicked(string placementId, ATCallbackInfo callbackInfo)
    {
        Debug.Log("Developer onAdClicked------:" + placementId);
    }
    // Ad video playback started, some ad platforms have this callback
    public void onAdVideoStart(string placementId)
    {
        Debug.Log("Developer onAdVideoStart------:" + placementId);
    }
    // Ad video playback ended, some ad platforms have this callback
    public void onAdVideoEnd(string placementId)
    {
        Debug.Log("Developer onAdVideoEnd------:" + placementId);
    }
    // Ad video playback progress, some ad platforms have this callback
    public void onAdVideoProgress(string placementId, int progress)
    {
        Debug.Log("Developer onAdVideoProgress------:" + placementId);
    }
    // Ad close button clicked, some ad platforms have this callback
    public void onAdCloseButtonClicked(string placementId, ATCallbackInfo callbackInfo)
    {
        Debug.Log("Developer onAdCloseButtonClicked------:" + placementId);
   }

      // v5.8.10 added Ad Source-level callbacks
      // Ad Source started loading
    public void startLoadingADSource(string placementId, ATCallbackInfo callbackInfo){
        Debug.Log("Developer startLoadingADSource------");
    }
      // Ad Source loading completed
        public void finishLoadingADSource(string placementId, ATCallbackInfo callbackInfo){
        Debug.Log("Developer finishLoadingADSource------");
    }
      // Ad Source failed
        public void failToLoadADSource(string placementId,ATCallbackInfo callbackInfo,string code, string message){
        Debug.Log("Developer failToLoadADSource------");
    }
      // Ad Source started bidding
        public void startBiddingADSource(string placementId, ATCallbackInfo callbackInfo){
               Debug.Log("Developer startBiddingADSource------");
    }
      // Ad Source bidding succeeded
        public void finishBiddingADSource(string placementId, ATCallbackInfo callbackInfo){
        Debug.Log("Developer finishBiddingADSource------");
    }
      // Ad Source bidding failed
        public void failBiddingADSource(string placementId,ATCallbackInfo callbackInfo,string code, string message){
        Debug.Log("Developer failBiddingADSource------");
    }
}

Note: The code snippets you see in this section are from the nativeScene.cs demo project in our Demo.

Previous
Banner Ad
Next
Splash ad
Last modified: 2026-07-02Powered by