Integrating Ads Into Your Game

This module will provide all the basic methods and callbacks for Banner, Interstitial, and Rewarded ads.

All of your ad related logic should be managed by the Coda SDK Ad module, this will enable ad logic to be integrated with other parts of the SDK.

You can still make direct calls to the ad provider you are using, but that is not an ideal way to use ads within Coda SDK.

Like all modules, you can access the ad module via CodaGameManager. Any ad-related calls are made on CodaGameManager.Instance.AdManager.

Showing Banner Ads

Showing banners is as easy as calling:

CodaGameManager.Instance.AdManager.ShowBannerAd();

The Ad Module then will check if you have your ads enabled or not. So you do not need to add extra logic for that in your game code.

It will try and fetch the banner, when its ready it will show the banner. Ad Module will automatically keep trying to fetch even if it fails initially. You will not need to check for that either.

Get Banner Height In Pixels

If you would like to manage your UI depending on the banner height or put a background image for the banner, below code will return you the banner height in pixels. Also have in mind that you need to consider safe area aswell. Below code example adjusts the background image's height depending on the banner's height. Same consept can be applied for the whole ui parent recttransform in order to give a gap at the bottom for the banner.

private void Start()
{
   CodaGameManager.Instance.AdManager.SubscribeBannerVisibilityChanged(OnBannerVisibilityChanged);
}

private void OnBannerVisibilityChanged(bool bannerVisibility)
{
   var bannerHeight = CodaGameManager.Instance.AdManager.GetBannerHeight();
   _bannerBackground.sizeDelta = new Vector2(_bannerBackground.sizeDelta.x, bannerVisibility ? bannerHeight : 0f);
}
Banner Background Image SettingsBanner Background Image Settings

Banner Background Image Settings

Showing Interstitial Ads

Interstitial ads, again is shown by calling a single method.
But this time you have a few callbacks for when an ad is shown, clicked or dismissed. Registering to those callbacks will ensure that you control the flow of your game logic properly for that critical points.

You can call for an interstitial ad by:

CodaGameManager.Instance.AdManager.ShowInterstitialAd();
CodaGameManager.Instance.AdManager.ShowInterstitialAd(InterstitialAdContext context);

Ad Module will again check for the predefined conditions for when to show a new interstitial ad.

❗️

Don't Introduce Custom Logic

You should not keep track of interstitial ads or introduce custom logic for determining when to show them. It will conflict with the Coda SDK built-in Ad Rule Set.

You basically register to Interstitial Ad Finished event to catch when the ad is shown and closed, so you can continue with your game's flow.

CodaGameManager.Instance.AdManager.SubscribeInterstitialAdFinished(HandleOnInterstitialAdFinished);

HandleOnInterstitialAdFinished is going to be called right after the app returns from the shown ad. On the handler method you should unsubscribe from that event to prevent duplicate calls on later interstitials.

CodaGameManager.Instance.AdManager.UnsubscribeInterstitialAdDismissed(HandleOnInterstitialAdFinished);

And this will unsubscribe the handler method from the event to prevent duplicate calls to that.

❗️

1.8.1 API Changes

Following our 1.8.1 release, following interstitial ad callbacks have one extra parameter in their signature. Any existing game specific code need to be updated accordingly.

i.e.: HandleOnInterstitialAdFinished method mentioned above, should be updated to have the (string context, string adUnit) signature.

    - public delegate void OnInterstitialAdTriggered(string context, string adUnitID);
    - public delegate void OnInterstitialAdRejected(string context, string adUnitID, string reason);
    - public delegate void OnInterstitialAdImpression(string context, string adNetwork, string placementId, double revenue, string adUnitID, int priority);
    - public delegate void OnInterstitialAdClicked(string context, string adUnitID);
    - public delegate void OnInterstitialAdDismissed(string context, string adUnitID);
    - public delegate void OnInterstitialAdFinished(string context, string adUnitID)

Showing Rewarded Ads

Rewarded ads are similar to interstitial ads. But here you can get multiple outcomes since rewarded ads can be either be just dismissed without reward or they can run to completion and unlock the reward associated with them.

To invoke a rewarded ad, you should call:

CodaGameManager.Instance.AdManager.ShowRewardedAd("<Descriptor for the reward context>");

This call receives a string parameter to describe the reward context. You will get the description on handler methods to decide which rewarded ad is completed. Example context descriptors can be, "Extra Coin", "Extra Life" etc.

You have to subscribe to the following events to detect when a rewarded ad is finished and/or video watched until the end.

CodaGameManager.Instance.AdManager.SubscribeRewardedAdRewarded(HandleOnRewardedAdRewarded);

CodaGameManager.Instance.AdManager.SubscribeRewardedAdFinished(HandleOnRewardedAdFinished);

HandleOnRewardedAdRewarded is called when a user completely watches the rewarded ad, and qualifies for a reward. This does not indicate that the ad is finished showing. So you can set a flag to be used when the HandleOnRewardedAdFinished call happens to decide whether to give the reward or not.

HandleOnRewardedAdFinished will be called right after the user finishes watching the ad or when an ad fails to load the video or ad is expired. In any case, your game logic should continue running.

Now you can unsubscribe from this two events to prevent duplicate calls in further rewarded ad watches.

CodaGameManager.Instance.AdManager.UnsubscribeRewardedAdRewarded(HandleOnRewardedAdRewarded);

CodaGameManager.Instance.AdManager.UnsubscribeRewardedAdFinished(HandleOnRewardedAdFinished);

You can look into the example project for the references to the methods mentioned above. And inspect the working examples there.

❗️

IMPORTANT NOTICE

Advertisement event callbacks are not being called from the main thread. So you can only call thread-safe operations from this callback. Please check operations that are not thread-safe in Unity 3D such as changing a transform component of a Unity GameObject.


Did this page help you?