Creating Rewarded Video Ads in Unity
Now that we have our game set up, let’s take a quick look at how we can implement ads into the game to give the player a little extra diamonds to be able to purchase extra items. For this little game, I’m going to set it up so that you have enough to get the key to complete the game, however if you want to play around with the flaming sword or boots of flight.
To start, we will want to create an AdManager to work with, like we have done with our UI or the GameManager. From there, we will create a new button to work with and place it at the top of the shop menu when it opens:
From here, we will need to make a couple adjustments to the settings of our game to allow ads, along with giving us the option to test them in the editor:
Next, we are going to go to our dashboard which will open up a new window to the Unity dashboard. From there, we will go to monetization and select the option to use unity’s ads, and go into the Ad Units to get our code needed to run the ads:
There are different styles of ads that can be run within a game. In our case, we are going to have this ad give the player 100 diamonds upon the completion of watching it. Now that we have the code we need, let’s move towards creating our script:
For the first part of the script, we are initializing our ads. We also need to be sure that we are adding the IUnityAdsListener interface. As with any interface, we have specific parameters that need to be added to this script:
As for those parameters, they are OnUnityAds Ready/DidError/DidStart/DidFinish. For the most part, we don’t need to put much in the particular voids, aside from our last one. This is the one that let’s Unity know what to do depending on what condition is met. In order for us to get the rewarded amount to our player however, we need to let it know to add the reward amount to both the player, and also we will want to update our total in the shop window upon the end of the ad so that the player doesn’t have to leave and reopen the shop up to see their new total. The final step is to make sure that with the ad button, we create a link towards the proper parameter so that it will run the ad.
With this completed, let’s see if we get a proper notification within Unity about the ads working:
Now that we have the ads working, we can turn off our test mode so that the ads can run properly on our game to the phone.