Aller au contenu principal

Banner Ad

Integration

Step 1. Instantiate Banner Ad

You can instanstiate a Banner ad right after SDK finish it's initialization. You have to pass the platform specific banner placement id and banner AdPosition in BannerAd constructor. Banner ad supports ad positions of following types:

PositionvalueDefinition
Top0Show banner at the top of the screen
Bottom1Show banner at the bottom of the screen
public class BlueStackAdsController : MonoBehaviour
{
private BannerAd _bannerAd;

void start() {
Settings settings = new Settings(true);
BlueStackAds.Initialize("app_id", settings, HandleInitCompleteAction);
}

private void HandleInitCompleteAction(InitializationStatus initializationStatus)
{
_bannerAd = new BannerAd(placementId, adPosition);
}
}

Step 2. Register event listeners

BannerAd exposes the following events through it's lifecycle.

MethodsDefinition
OnBannerDidLoadAd is successfully loaded and SDK is ready to display the ad.
OnBannerDidFailedThe ad failed to load or display. An additional error parameter of type BlueStackError contains the reason of the failure.
OnAdClickedUser has clicked the ad. Ad may open a link in browser.
OnBannerDisplayThe banner ad has been displayed on the screen.
OnBannerHideThe banner ad has been hidden.
OnBannerDidRefreshThe banner ad has been refreshed
OnBannerDidFailToRefreshThe banner ad has been failed to refresh

To register for banner ad events, add the following code after instantiating BannerAd.

    _bannerAd.OnBannerDidLoad += (sender, args) =>
{
Debug.Log("OnBannerDidLoad");
};

_bannerAd.OnBannerDidFailed += (sender, args) =>
{
Debug.LogError("OnBannerDidFailed: " + args.Message);
};

_bannerAd.OnBannerDisplay += (sender, args) =>
{
Debug.Log("OnBannerDisplay");
};
_bannerAd.OnBannerHide += (sender, args) =>
{
Debug.Log("OnBannerHide");
};
_bannerAd.OnAdClicked += (sender, args) =>
{
Debug.Log("OnAdClicked");
};
_bannerAd.OnBannerDidRefresh += (sender, args) =>
{
Debug.Log("OnBannerDidRefresh");
};
_bannerAd.OnBannerDidFailToRefresh += (sender, args) =>
{
Debug.LogError("OnBannerDidFailToRefresh: " + args.Message);
};

attention

Make sure you only register event listener once.

Step 3. Load Banner ad

BannerAd takes AdSize to load banner. It also have another Load method that takes Preference instance along with AdSize. Banner Ad supports following ad sizes:

Ad SizeDefinition
Banner320x50
DynamicBanner0x50
LargeBanner320x100
FullBanner468x60
Leaderboard728x90
DynamicLeaderboard0x90
MediumRectangle300x250
  • Without Preference
_bannerAd.Load(AdSize.Banner);
  • With Preference
public class BlueStackAdsController : MonoBehaviour
{
...
private RequestBannerAd() {

Preference _preference = new Preference();
Location myLocation = new Location(Location.NONE_PROVIDER)
{
Latitude = 35.757866,
Longitude = 10.810547
};
_preference.SetAge(25);
_preference.SetLanguage("en");
_preference.SetGender(Gender.Male);
_preference.SetKeyword("brand=myBrand;category=sport");
_preference.SetLocation(myLocation, 1);
_preference.SetContentUrl("https://madvertise.com/en/");

_bannerAd.Load(AdSize.Banner, _preference);
}
...
}

Note : The SetLocation method takes the following parameters:

  • The Location instance.
  • The CONSENT_FLAG value (corresponds to a int : 0,1,2 or 3).
    • 0 = Not allow to send location.
    • 1 = When you managed location according to consent value.
    • 2 and 3 = Allow the SDK to managed location directly in accordance with the consent value use TCF v1 or TCF v2, see with the madvertise team it depends on your implementation.

Step 4. Show Banner ad

After loading the banner ad you can request it to be displayed.

_bannerAd.OnBannerDidLoad += (sender, args) =>
{
_bannerAd?.Show();
};

Hide Banner ad

Banner Ad can be set to be hidden also.

_bannerAd.Hide();

Change banner ad position

You can set banner ad position runtime.

_bannerAd.setPosition(adPosition);

Destroy banner ad

Destroy banner ad if you want to create a new one.

_bannerAd.Destroy();