Day 127 — Creating Rewarded Video Ads in Unity

Hey and welcome.

This one is going to cover how to set up an ad that the player can watch and get rewarded 100 Gems.

First things first, go ahead and duplicate the buy item button, remove its current OnClick function using the minus icon and change the text to something along the lines of “Click here for 100G”. Re-colour and style it to your liking and put it to the top right of your shop UI.

Next up is to prepare our project for use with Unity Ads. Head over to Window > General > Services and click on the Ads option and it should open up the relevant page in your project settings, switch this from off to on and then make sure that Test Mode is enabled so that you can simulate the ads when you test the game.

This part may ask you to set up your organization/dashboard account so make sure to do that if you haven’t done that already. Next click on the Dashboard link here to be taken to your Unity dashboard and select this project. If this is your first time here you’ll see a splash page asking you to activate your account, go ahead and do that and select no for the 3rd party ad options and you should now see something like the below when you check out the Ad Units page:

The important thing for you to keep track of here though is the Game ID’s for Google Play or Apple Store depending on which one you’re going to put your game on.

Once you have that all sorted go ahead and create and empty object called AdsManager and then create an AdsManager script to attach to it and add in the following code:

Now there’s quite a lot going on here but the important thing to take note of first is that we’re including the Advertisements dependency which allows us to use the IUnityAdsListener interface as well as some specific functions for the ad itself like checking its status and actually showing the ad.

Replace the value for _gameId with the ID you got earlier. Since this is an interface it’s important to make sure that you have the same function names here except for ShowRewardedAd() since that’s a personalised one otherwise this script won’t work. Speaking of that function, that’s what we’re going to use for our OnClick event for the reward ad button we created earlier so go ahead and add that in. This function just handles the initialization of this script so we can call the function directly to get this to run.

The important functions here though are OnUnityAdsReady() which displays the rewarded ad by referencing it directly with “rewardedVideo” when it’s finished loading and OnUnityAdsDidFinish() which takes the result of the ad when it’s finished playing which can return either Finished, Skipped or Failed which we can use to determine the logic for each case. In the case that the ad finished playing successfully that’s where we add in the code to update the player’s gem count as well as the UI for the gem counter.

The last bit of code you’ll need is in your GameManager here:

This creates an instance of our player component that we can reference when we’re making a call to the GameManager, pretty cool stuff!

With all that done you should have something like the above working for you now. If you come across an issue trying turning off your ads from the services window and turn them back on and double check that test mode is enabled.

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.