Managing multiple ad services in Unity
Mobile advertisement, as a monetization solution is a hot trend these days. Many game studios have fully switched to ads and are making a considerable amount of money.
There are actually so many mobile ad services that you can use but it is important to use as many of them as possible. For example, according to Appbrain, Voodoo’s game “Helix Jump” with more than 500 million downloads is using 18 different ad services! Including Admob, UnityAds, Vungle, IronSource, AppLovin, Facebook Ads, Chartboost, and …
So I decided to create a simple solution to use all these different services together.
Our goal is to reach a solution so that we don’t have to change our main implementation every time we add a new ad service.
First I identified similar ad formats and similar behaviors between different ad services and created an Interface that contains all those things:
This interface helps us identify all ad platforms as a single type so that we can easily switch between them. You should remember that each one of these IAdServices belongs to one platform and one ad format. For example, AdmobBannerAd will be one class implementing IAdService and AdmobRewardedAd will be another class implementing IAdService.
The next step is to create a class that handles all ad service objects. I created MasterAd to do so:
MasterAd does 3 main things:
- Loading, showing, and destroying ads
- Adding and switching between ad services using a service manager
In order to stick to S.O.L.I.D, I separated the part that manages services in an interface called IServiceManager:
With this, you can create your own implementation of adding, data structure type to store services, and the way they will be switched.
I created my own implementation called ServiceSwitcher:
Here I’m using a simple List to store services and a pointer to know which service is currently in use, and switch to the next service when the current one fails. Remember that each time you add a service, you should call the Init() function to prepare the ad service.
So now that we have our main classes, it’s time to show you an example. Here I implemented Admob Banner Ad and UnityAds Banner Ad to demonstrate how IAdService should be used:
You can notice that I have added an additional dictionary for banner view objects as we may have multiple banner ads with different ad unit ids.
You can see that I have added the exact same ad token names (with different token ids actually) to this class so that we only need to pass one single ad name to both ad services.
Now that we have at least two ad services we can use them in action. I created a small demo class only to show how MasterAd and different IAdServices work together:
After this, whenever I call LoadBanner the MasterAd class tries to load the ad from the current service and if it fails, it automatically switches to another service and does the same thing until one service answers. And when I call ShowBanner it will show the ad after checking if the ad was loaded. This functionality works for most ad services and formats.
I’m hosting this project on my Github here. Make sure to check it out and feel free to add any comments.
Thank you for your time.