Tip of the Day: Implementing Unity Ads
When working with mobile projects, strategically placing your ads will help your players gaining some rewards and at the same time help you fund your projects.
After Unity 2019, Unity changed the way you implement the ads and giving you more freedom and more options to customize your ads. Let’s start right away.
A. Unity Services
First we need to enable Unity Ads and IAP services.
A. Make sure your Advertisements package is up to date and installed. Go to Package Manager / Unity Registry / Advertisement.
B. Open the Services tab. Go to Windows / General / Services.
C. Create your organization and set the age target if it is below age 13 or above.
D. From your services tab, click on the Ads tab:
- Turn the ads on
- Turn the “Monetize your game” on
- Update the Ads Package if necessary
- Enable “Test Mode” in order to test your ads in the editor.
- Finally, open the Dashboard from the “Monetize your games”. This will open a new window in your browser and make sure to choose the project you are working on.
E. In your Dashboard, you need to make sure you do the following steps in order to insure a smooth Ads implementation and testing.
- Open the Project Settings
- Ads Provider: Change it to Unity (for now, you can change it later)
- Test Mode: Choose Google Play Store (OR Apple App Store if you want) and enable test mode
If you do this properly, this will give you project Game IDs for google store and apple store that we will use them for implementation later.
F. Finally in your dashboard, go to Placements and customize your ads. For now take note of the rewarded ads placement and its ID.
Code Implementation for Rewarded Ads
Before you implement the code, we need to make sure of two things first.
- In the Build Settings, make sure to switch to Android or Apple.
- In the services tab, go to In-Add Purchasing then enable it and update it as needed.
Now, create a C# script call it “AdsManager” and add it to a gameObject and let’s also call it “Ads Manager”.
A quick explanation: after Unity 2019, Unity uses an interface “IUnityAdsListerner” that you must implement in your script in order to “Listen” and “Initialize” your rewarded ads.
According to Unity Docs: Rewarding players for watching ads increases user engagement, resulting in higher revenue. For example, games may reward players with in-game currency, consumables, additional lives, or experience-multipliers. For more information on how to effectively design your rewarded ads, see documentation on Ads best practices.
To reward players for completing a video ad, implement a callback method using the ShowResult result to check if the user finished the ad and should be rewarded.
A. Add the namespace: using UnityEngine.Advertisements;
B. Add the IUnityListerner interface to the script. This will force you to implement four methods that we will use later.
C. Add the following variables:
D. Add the scripts as an Advertisement Listener onEnable, remove it onDisable, and Initialize the advertisement using the gameID
E. Create a public method “ShowRewardedVideo” that will check if the Ad is ready and then “Show” the rewarded Ad.
F. Finally using the interface’s IUnityAdsListner on finished method, in our project will reward the player 100 diamonds when the ad finishes and display a warning if the ad fails.
You can check the final script here on pastebin using the following link: Rewarded_Ads — Pastebin.com
This is how it looks in the Unity Editor when testing =>