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.
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:
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.
ATNativeAd.Instance.hasAdReady(mPlacementId_native_all);
You can use the following code to display a native ad:
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:
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.
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.
parentProperty
parentProperty controls the overall size of the Native ad, as shown in the red-circled area below.

Native ad element descriptions:
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:
public void cleanView()
{
Debug.Log ("Developer cleanView native....");
ATNativeAd.Instance.cleanAdView(mPlacementId_native_all,AnyThinkAds.Demo.ATManager.anyThinkNativeAdView);
}
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);
...
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);
...
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);
For callback info details, see: Callback Info Description
Use the following code to implement multiple listeners:
// 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:
// 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):
//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------");
}
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)
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.