ATBannerAd:
| API | Parameters | Description |
|---|---|---|
| loadBannerAd | string placementid, Dictionary | Load an ad |
| setListener | ATBannerAdListener listener | Set the listener callback interface. (Deprecated after version 5.9.51) For how to set listeners, see: Banner Ad Event Settings |
| showBannerAd | string placementid, string position | Display ad for the first time position: ATBannerAdLoadingExtra.kATBannerAdShowingPisitionTop (display at the top of the screen) ATBannerAdLoadingExtra.kATBannerAdShowingPisitionBottom (display at the bottom of the screen) |
| 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) |
| hideBannerAd | string placementid | Hide the ad |
| showBannerAd | string placementid | Display a previously hidden ad |
| cleanBannerAd | string placementid | Remove the ad |
Use the following code to load a Banner ad:
public void loadBannerAd()
{
ATBannerAd.Instance.client.onAdLoadEvent += onAdLoad; ATBannerAd.Instance.client.onAdLoadFailureEvent +=onAdLoadFail;
ATBannerAd.Instance.client.onAdImpressEvent += onAdImpress;
ATBannerAd.Instance.client.onAdAutoRefreshEvent += onAdAutoRefresh;
ATBannerAd.Instance.client.onAdAutoRefreshFailureEvent += onAdAutoRefreshFail;
ATBannerAd.Instance.client.onAdClickEvent += onAdClick;
ATBannerAd.Instance.client.onAdCloseEvent += onAdCloseButtonTapped;
Dictionary jsonmap = new Dictionary();
//Configure the width, height, and whether to use pixel units for Banner display (only valid for iOS; Android uses pixel units). Note that banner ads on different platforms have certain restrictions. For example, if the configured Pangle banner ad is 640*100, to fill the screen width, calculate height H = (screen width *100)/640; then the size in the load extra would be (screen width: H).
ATSize bannerSize = new ATSize(this.screenWidth, 100, true);
jsonmap.Add(ATBannerAdLoadingExtra.kATBannerAdLoadingExtraBannerAdSizeStruct, bannerSize);
// Added in v5.6.5, only for Admob adaptive Banner
jsonmap.Add(ATBannerAdLoadingExtra.kATBannerAdLoadingExtraInlineAdaptiveWidth, bannerSize.width);
jsonmap.Add(ATBannerAdLoadingExtra.kATBannerAdLoadingExtraInlineAdaptiveOrientation, ATBannerAdLoadingExtra.kATBannerAdLoadingExtraInlineAdaptiveOrientationCurrent);
ATBannerAd.Instance.loadBannerAd(mPlacementId_native_all, jsonmap);
}
Please continue reading to learn how to get notified about Banner ad events, such as load success/failure, impression, and click.
There are currently two methods to display banner ads.
ATBannerAd.Instance.showBannerAd(mPlacementId_native_all, ATBannerAdLoadingExtra.kATBannerAdShowingPisitionTop);
ATBannerAd.Instance.showBannerAd(mPlacementId_native_all, ATBannerAdLoadingExtra.kATBannerAdShowingPisitionBottom);
When using predefined positions to display banner ads, AnyThinkSDK has already accounted for Safe Area-related blank areas such as the notch.
public void showBannerAd()
{
// Please keep the width and height consistent with the size passed during ad loading, and set the x and y coordinates as needed.
ATRect arpuRect = new ATRect(0,70, screenWidth, 100, true);
ATBannerAd.Instance.showBannerAd(mPlacementId_native_all, arpuRect);
}
The trailing parameter passed to the constructor of the ATRect 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.
When using the scenario feature:
public void showBannerAd()
{
Dictionary jsonmap = new Dictionary();
jsonmap.Add(AnyThinkAds.Api.ATConst.SCENARIO, showingScenarioID);
ATBannerAd.Instance.showBannerAd(mPlacementId_banner_all, ATBannerAdLoadingExtra.kATBannerAdShowingPisitionBottom, jsonmap);
//ATBannerAd.Instance.showBannerAd(mPlacementId_native_all, arpuRect, jsonmap);
}
If needed, use the following code to remove the Banner from the screen:
public void removeBannerAd()
{
ATBannerAd.Instance.cleanBannerAd(mPlacementId_native_all);
}
If you only want to temporarily hide the Banner (rather than remove it from the screen), use the code here:
public void hideBannerAd()
{
ATBannerAd.Instance.hideBannerAd(mPlacementId_native_all);
}
After hiding the Banner, you can re-display it using the following code:
public void reshowBannerAd()
{
ATBannerAd.Instance.showBannerAd(mPlacementId_native_all);
}
Note: Please note that the showBannerAd method here does not accept a rect parameter, unlike when you first display the Banner ad.
The difference between removing a Banner ad and hiding a Banner ad is that removing the Banner ad from the screen also destroys it (meaning you will need to load the Banner ad again before displaying it), while hiding the Banner only requires calling the showBannerAd method to re-display the previously hidden Banner ad without passing the ATRect parameter.
For callback info details, see: Callback Info Description
Use the following code to implement multiple listeners:
// Ad loaded successfully
ATBannerAd.Instance.client.onAdLoadEvent += onAdLoad;
// Ad load failed
ATBannerAd.Instance.client.onAdLoadFailureEvent += onAdLoadFail;
// Ad displayed successfully
ATBannerAd.Instance.client.onAdImpressEvent += onAdImpress;
// Ad auto-refresh succeeded
ATBannerAd.Instance.client.onAdAutoRefreshEvent += onAdAutoRefresh;
// Ad auto-refresh failed
ATBannerAd.Instance.client.onAdAutoRefreshFailureEvent += onAdAutoRefreshFail;
// Ad clicked
ATBannerAd.Instance.client.onAdClickEvent += onAdClick;
// Ad closed
ATBannerAd.Instance.client.onAdCloseEvent += onAdCloseButtonTapped;
Advanced listener settings:
// Ad Source started loading
ATBannerAd.Instance.client.onAdSourceAttemptEvent += startLoadingADSource;
// Ad Source loading completed
ATBannerAd.Instance.client.onAdSourceFilledEvent += finishLoadingADSource;
// Ad Source load failed
ATBannerAd.Instance.client.onAdSourceLoadFailureEvent += failToLoadADSource;
// Ad Source started bidding
ATBannerAd.Instance.client.onAdSourceBiddingAttemptEvent += startBiddingADSource;
// Ad Source bidding succeeded
ATBannerAd.Instance.client.onAdSourceBiddingFilledEvent += finishBiddingADSource;
// Ad Source bidding failed
ATBannerAd.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
// Load ad
// Ad auto-refresh succeeded
public void onAdAutoRefresh(object sender,ATAdEventArgs erg)
{
Debug.Log("Developer callback onAdAutoRefresh :" + erg.placementId );
}
// Ad auto-refresh failed
public void onAdAutoRefreshFail(object sender,ATAdErrorEventArgs erg )
{
Debug.Log("Developer callback onAdAutoRefreshFail : "+ erg.placementId );
}
// Ad clicked
public void onAdClick(object sender,ATAdEventArgs erg)
{
Debug.Log("Developer callback onAdClick :" + erg.placementId);
}
// Ad displayed successfully
public void onAdImpress(object sender,ATAdEventArgs erg)
{
Debug.Log("Developer callback onAdImpress :" + erg.placementId);
}
// Ad loaded successfully
public void onAdLoad(object sender,ATAdEventArgs erg)
{
Debug.Log("Developer callback onAdLoad :" + erg.placementId);
}
// Ad load failed
public void onAdLoadFail(object sender,ATAdErrorEventArgs erg )
{
Debug.Log("Developer callback onAdLoadFail : : " + erg.placementId + "--code:" + erg.code + "--msg:" + erg.message);
}
// Ad close button tapped
public void onAdCloseButtonTapped(object sender,ATAdEventArgs erg)
{
Debug.Log("Developer callback onAdCloseButtonTapped :" + 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(object sender,ATAdEventArgs erg){
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 Banner ad events (load success/failure, impression, and click), simply define an implementation class of the ATBannerAdListener interface: (Note: For Android, all callback methods are not on the Unity main thread)
class BannerCallback : ATBannerAdListener
{
// Ad auto-refresh succeeded
public void onAdAutoRefresh(string placementId, ATCallbackInfo callbackInfo)
{
Debug.Log("Developer callback onAdAutoRefresh :" + placementId);
}
// Ad auto-refresh failed
public void onAdAutoRefreshFail(string placementId, string code, string message)
{
Debug.Log("Developer callback onAdAutoRefreshFail : "+ placementId + "--code:" + code + "--msg:" + message);
}
// Ad clicked
public void onAdClick(string placementId, ATCallbackInfo callbackInfo)
{
Debug.Log("Developer callback onAdClick :" + placementId);
}
// This callback is no longer executed after v5.5.3; it has been moved to the onAdCloseButtonTapped method callback
public void onAdClose(string placementId)
{
Debug.Log("Developer callback onAdClose :" + placementId);
}
// Ad displayed successfully
public void onAdImpress(string placementId, ATCallbackInfo callbackInfo)
{
Debug.Log("Developer callback onAdImpress :" + placementId);
}
// Ad loaded successfully
public void onAdLoad(string placementId)
{
Debug.Log("Developer callback onAdLoad :" + placementId);
}
// Ad load failed
public void onAdLoadFail(string placementId, string code, string message)
{
Debug.Log("Developer callback onAdLoadFail : : " + placementId + "--code:" + code + "--msg:" + message);
}
// Ad close button tapped
public void onAdCloseButtonTapped(string placementId, ATCallbackInfo callbackInfo)
{
Debug.Log("Developer callback onAdCloseButtonTapped :" + 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 bannerScene.cs demo project in our Demo.