Menu

Native

Note on Android:
Currently, when you use Unity to directly create an Android APK, you cannot use native video ads, because the Unity packaged APK will turn off the activity hardware acceleration of the game by default, so the video cannot be displayed. If you need to display native advertising videos, you must use the export project method. The processing method is as follows:

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

(2) Modify the attributes in AndroidManifest and set the hardwareAccelerated attribute in the picture to true

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

1. Access process suggestions

  1. Load ads by calling ATNativeAd#loadNativeAd when starting the application
  2. Use ATNativeAd#hasAdReady to determine whether ads can be displayed where they need to be displayed
  3. If you need to use Advertising scenariosDistinguish the data of different business scenarios, please refer to the sample code

2. API description

ATNativeAd:

APIParameterDescription
loadNativeAdstring placementid,Dictionary<string,string> extraLoad ads(Starting from v5.6.8, for pangolin template rendering, Mintegral automatic rendering of ads, etc. The width and height must be passed through the extra parameter, otherwise the ad size may display abnormally)
setListenerATNativeAdListener listenerSet Listening callback interface (Abandoned after version 5.9.51) For specific reference to how to set the listener: Native ad event setting instructions
hasAdReadystring placementid Determine whether there is ad cache
renderAdToScenestring placementid,ATNativeAdView anyThinkNativeAdViewDisplay ads
entryScenarioWithPlacementIDstring placementId, string scenarioIDSet to enter the displayable advertising scene

3. Load native ads

You can use the following code to load native ads :

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 or 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: Read on to learn how to get notified on load success/failure events.

4. Determine whether there is advertising cache

ATNativeAd.Instance.hasAdReady(mPlacementId_native_all);

5. Display native ads

You can display native ads using the following code:

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

        //(New 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 scene function:

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 tail parameter passed to the constructor of the ATNativeItemProperty class indicates whether to use pixels(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 These values will be 10, 40, 100, 150; that is, the final value depends on the screen ratio of the target device.

As you can see above, we have defined an ATNativeConfig class for you to configure the various properties of the native assets (bgColor, textColor, textSize , position, etc.), such as CTA button, application icon, title text, description text, cover image, etc. 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 Object, used to control the style of Native ads. ATNativeItemProperty controls the position and style of individual Native ad elements; some elements may not support Some properties in ATNativeItemProperty, such as picture elements (icon, main image) only support x, y, width, height, usesPixel, but does not support background color, font size, font color, etc., while text elements (such as title, cta, desc) support all attributes. In iOS games, if you want to specify "transparency", use "clearColor". For example, if you want the background of the advertising area to be transparent, assign "clearColor" to the backgroundColor of parentProperty. Note: 1) Only iOS supports clearColor. If you need to specify a transparent background on Android, you only need to use rgba The a part in is 0;

2) iOS does not support alpha, so the color value should be passed like #5aef00 (six-digit rbg value), while Android Can support alpha, whose color contains 8-digit hexadecimal, such as 5a2b3c00

parentProperty

parentProperty controls the overall size of Native, as shown in the red circle area in the figure below.

Native advertising elements are described as follows:

  • appIconProperty : The appIconProperty attribute controls the icon properties of the advertisement, as followsFigure 1 shown:
  • mainImageProperty: mainImageProperty controls the cover image of the advertisement, as follows Figure 2 shown:
  • titleProperty: titleProperty controls the advertisement title, as followsFigure 3Shown:
  • descProperty : descProperty controls the ad description text, as shown below Figure 4
  • adLogoProperty :adLogoProperty controls the advertising logo properties, as followsAs shown in Figure 5. < strong>Note: The position of the advertising logo on some platforms is internally fixed and does not support developer specification, such as Admob.
  • ctaButtonProperty: ctaButtonProperty control Click the button, as shown below Figure 6
  • dislikeButtonProperty(new in v5.7.21) dislikeButtonProperty controls the close button (if not set, the close button will not be displayed)

Note: The above Native advertising elements need to be rendered when returned

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

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

About template rendering advertisement instructions< /span>

  • Template rendering ads can only be controlled through parentProperty. Adjusting other properties (appIcon, title, desc, adLogo, ctaButton, mainImage, etc.) is invalid, among which parentProperty and mainImageProperty must be set, and the parameters of mainImageProperty can be consistent with parentProperty
  • The width and height of parentProperty must be consistent with the width and height passed in when loading, otherwise there may be problems with incomplete display or excessive display
  • The template ad has its own width and height The ratio can be viewed in the backend of the advertising platform. Try to choose a template with the same or close width-to-height ratio after the advertising platform, and use that width-to-height ratio in the code to load and display ads to obtain the best display effect
  • Template rendering ad adaptive height: Developers can implement adaptive height through the following steps (adaptive height when loading is only for Android Pangolin and Youlianghui platforms) Note:
  • When using adaptive height, relatively tall template ads may appear. Developers can check the required width-to-height ratio templates in the advertising platform background according to actual needs, and remove templates that do not meet expectations< /li>
  • Adaptive height is not controlled by the height of parentProperty
  • 1) (This point is only for Android ) Turn on adaptive height when loading (Key: ADAPTIVE_HEIGHT required)
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);
  • 2) Turn on the adaptive height when displaying (need to pass in Key: Key: ADAPTIVE_HEIGHT)
...
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);
  • 3) When displaying, you can control the advertisement to be centered at the top or bottom of the screen
...
Dictionary<string, string> jsonmap = new Dictionary<string, string>();
jsonmap.Add(AnyThinkAds.Api.ATConst.POSITION, AnyThinkAds.Api.ATConst.POSITION_BOTTOM);//屏幕底部居中
//jsonmap.Add(AnyThinkAds.Api.ATConst.POSITION, AnyThinkAds.Api.ATConst.POSITION_TOP);//屏幕顶部居中

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

6. Implement native advertising listener

Please view the callback information for details: Callback information description

Use the following code to implement multiple listeners

        //Advertisement loaded successfully
           ATNativeAd.Instance.client.onAdLoadEvent += onAdLoad;
           //Ad loading failed
        ATNativeAd.Instance.client.onAdLoadFailureEvent += onAdLoadFail;
        //Advertisement displayed successfully
        ATNativeAd.Instance.client.onAdImpressEvent += onAdImpressed;
        //ad clicked
        ATNativeAd.Instance.client.onAdClickEvent += onAdClick;
        //The ad close button is clicked, and some advertising platforms have this callback
        ATNativeAd.Instance.client.onAdCloseEvent += onAdClose;
        //The advertising video starts playing. Some advertising platforms have this callback
        ATNativeAd.Instance.client.onAdVideoStartEvent += onAdVideoStart;
        //The advertising video ends playing, some advertising platforms have this callback
        ATNativeAd.Instance.client.onAdVideoEndEvent += onAdVideoEnd;
        //Ad video playback progress, some advertising platforms have this callback
        ATNativeAd.Instance.client.onAdVideoProgressEvent += onAdVideoProgress;


The method definition parameters are as follows (Note: The method name can Refer to the following code or customize the method name, but the parameters must be consistent)

    //sender is the advertising type object, erg is the return information
   //Advertisement loaded successfully
    public void onAdLoad(object sender, ATAdEventArgs erg)
    {
        Debug.Log("Developer onAdLoaded------:" + erg.placementId);
    }
    //Ad loading failed
    public void onAdLoadFail(object sender, ATAdEventArgs erg)
    {
        Debug.Log("Developer onAdLoadFail------:" + erg.placementId + "--code:" + erg.code + "--msg:" + erg.message);
    }
    //Advertisement 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);
    }
    //The advertising video starts playing. Some advertising platforms have this callback.
    public void onAdVideoStart(object sender, ATAdEventArgs erg)
    {
        Debug.Log("Developer onAdVideoStart------:" + erg.placementId);
    }
    //The advertising video ends playing, some advertising platforms have this callback
    public void onAdVideoEnd(object sender, ATAdEventArgs erg)
    {
        Debug.Log("Developer onAdVideoEnd------:" + erg.placementId);
    }
    //Ad video playback progress, some advertising platforms have this callback
    public void onAdVideoProgress(object sender, ATAdProgressEventArgs erg)
    {
        Debug.Log("Developer onAdVideoProgress------:" + erg.placementId);
    }
    //The ad close button is clicked, and some advertising platforms have this callback
    public void onAdCloseButtonClicked(object sender, ATAdEventArgs erg)
    {
        Debug.Log("Developer onAdCloseButtonClicked------:" + erg.placementId);
   }


Previous
Banner Ad
Next
Splash ad
Last modified: 2025-05-30Powered by