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:
using UnityEngine.Advertisements;public class AdsManager : MonoBehaviour, IUnityAdsListener
{
private string _gameId = "1234567";
private bool _testMode = true;
private bool _hasPlayed = false; public void ShowRewardedAd()
{
Advertisement.AddListener (this);
Advertisement.Initialize (_gameId, _testMode);
} public void OnUnityAdsDidFinish(string placementId, ShowResult result)
{
switch (result)
{
case ShowResult.Finished:
GameManager.Instance.Player.AddGems(100);
UIManager.Instance.OpenShop(GameManager.Instance.Player.Diamonds);
break;
case ShowResult.Skipped:
Debug.Log("Add has been skipped");
break;
case ShowResult.Failed:
Debug.Log("The video has failed");
break;
}
} public void OnUnityAdsReady(string placementId)
{
if (placementId == "rewardedVideo" && _hasPlayed == false)
{
_hasPlayed = true;
Advertisement.Show ("rewardedVideo");
}
} public void OnUnityAdsDidError (string message)
{
Debug.LogError("Something has gone wrong with loading the ad");
} public void OnUnityAdsDidStart (string placementId)
{
// Optional actions to take when the end-users triggers an ad.
}
}
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:
public Player Player { get; private set; }private void Awake()
{
_instance = this;
Player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
}
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.