Nerd For Tech
Published in

Nerd For Tech

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:

  1. Turn the ads on
  2. Turn the “Monetize your game” on
  3. Update the Ads Package if necessary
  4. Enable “Test Mode” in order to test your ads in the editor.
  5. 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.

  1. Open the Project Settings
  2. Ads Provider: Change it to Unity (for now, you can change it later)
  3. Test Mode: Choose Google Play Store (OR Apple App Store if you want) and enable test mode
Game IDs

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.

Rewarded Ads

Code Implementation for Rewarded Ads

Before you implement the code, we need to make sure of two things first.

  1. In the Build Settings, make sure to switch to Android or Apple.
  2. 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 =>

Player Rewarded 100 Diamonds upon rewarded ad completion

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

CyberSecurity in C++

Dear Facebook Messenger

You can now pay to get your Whole Genome Sequenced with cryptocurrencies (WETH, GNOME, GENE, USDT…

Identify the Active Controller In Kafka

Creating Your GitHub Portfolio

The new way of defining eloquent accessors and mutators in Laravel 9.x

what i have been waiting for has come !

Writing good commit messages.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mohamed Hijazi

Mohamed Hijazi

A knowledge seeking biologist who is following his passion into a full time career in Unity / Game Development. https://www.linkedin.com/in/mohamed-hijazi/

More from Medium

Using Decals in Unity’s HDRP

Making a Treasure Chest System Using Timeline in Unity

Converting from 3D to 2D in Unity

nitCreating Collectables in Unity