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.
ATNativeAd:
API | Parameter | Description |
---|---|---|
loadNativeAd | string placementid,Dictionary<string,string> extra | Load 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) |
setListener | ATNativeAdListener listener | Set Listening callback interface (Abandoned after version 5.9.51) For specific reference to how to set the listener: Native ad event setting instructions |
hasAdReady | string placementid | Determine whether there is ad cache |
checkAdStatus | string placementid | (new in v5.7.22)Get the status of the current advertising slot (Json string): 1. isLoading: whether it is loading 2. isReady: whether there is advertising cache (Same function as hasAdReady) 3. AdInfo: The current highest priority ad cache information (refer to ATCallbackInfo description) |
getValidAdCaches | string placementid | (new in v5.7.54 Added)Get all successfully loaded ad cache information (JSON string) (refer to ATCallbackInfo description) |
renderAdToScene | string placementid,ATNativeAdView anyThinkNativeAdView | Display ads |
entryScenarioWithPlacementID | string placementId, string scenarioID | Set to enter the displayable advertising scene |
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.
ATNativeAd.Instance.hasAdReady(mPlacementId_native_all);
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.
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:
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);
}
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);//屏幕底部居中
//jsonmap.Add(AnyThinkAds.Api.ATConst.POSITION, AnyThinkAds.Api.ATConst.POSITION_TOP);//屏幕顶部居中
ATNativeAd.Instance.renderAdToScene(mPlacementId_native_all, anyThinkNativeAdView, jsonmap);
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;
Advanced monitoring settings:
//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);
}
// v5.8.10 adds ad source level callbacks
// Advertising source starts loading
public void startLoadingADSource(object sender, ATAdEventArgs erg){
Debug.Log("Developer startLoadingADSource------");
}
// Advertising 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------");
}
// Advertising sources start bidding
public void startBiddingADSource(object sender, ATAdEventArgs erg){
Debug.Log("Developer startBiddingADSource------");
}
// Advertising source bidding successful
public void finishBiddingADSource(object sender, ATAdEventArgs erg){
Debug.Log("Developer finishBiddingADSource------");
}
// Advertising source bidding failed
public void failBiddingADSource(object sender, ATAdEventArgs erg){
Debug.Log("Developer failBiddingADSource------");
}
To get information about various native advertising events (loading success/failure, impression and click etc.), you can define an implementation class of ATNativeAdListener. Here is an example: (Note: For Android, all callback methods are not on Unity’s main thread)
class ATNativeCallbackListener : ATNativeAdListener
{
//Advertisement loaded successfully
public void onAdLoaded(string placementId)
{
Debug.Log("Developer onAdLoaded------:" + placementId);
}
//Ad loading failed
public void onAdLoadFail(string placementId, string code, string message)
{
Debug.Log("Developer onAdLoadFail------:" + placementId + "--code:" + code + "--msg:" + message);
}
//Advertisement 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);
}
//The advertising video starts playing. Some advertising platforms have this callback.
public void onAdVideoStart(string placementId)
{
Debug.Log("Developer onAdVideoStart------:" + placementId);
}
//The advertising video ends playing, some advertising platforms have this callback
public void onAdVideoEnd(string placementId)
{
Debug.Log("Developer onAdVideoEnd------:" + placementId);
}
//Ad video playback progress, some advertising platforms have this callback
public void onAdVideoProgress(string placementId, int progress)
{
Debug.Log("Developer onAdVideoProgress------:" + placementId);
}
//The ad close button is clicked, and some advertising platforms have this callback
public void onAdCloseButtonClicked(string placementId, ATCallbackInfo callbackInfo)
{
Debug.Log("Developer onAdCloseButtonClicked------:" + placementId);
}
//v5.8.10 adds ad source level callbacks
//Advertising source starts loading
public void startLoadingADSource(string placementId, ATCallbackInfo callbackInfo){
Debug.Log("Developer startLoadingADSource------");
}
// Advertising 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------");
}
//Advertising sources start bidding
public void startBiddingADSource(string placementId, ATCallbackInfo callbackInfo){
Debug.Log("Developer startBiddingADSource------");
}
//Advertising source bidding successful
public void finishBiddingADSource(string placementId, ATCallbackInfo callbackInfo){
Debug.Log("Developer finishBiddingADSource------");
}
// Advertising 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 come from our Demo's nativeScene.cs demo project.