Lending a helping hand for when you want to save some time.
Sure we can farm the area for more diamonds but when you don’t have the time for that kind of gameplay anymore, adding in a way to help them out with much less time can be useful. Without shoving it in the player's face every 2 seconds, ads can be a useful tool for games but doing it wrong can put people off a game.
First, let’s set up the shop to have a button to click to watch an ad.
Here we can duplicate the “Buy Item button” and move it to be above the shop. Don’t forget to rename the button and remove the OnClick event. Here you can also change the color of the button to make it stand out a little.
With that setup, we can move on to enabling unity ads. Open up the services tab, it might already be up beside the inspector, if it isn’t go to Windows>General>Services, or shortcut Ctrl+0 or command +0 if on mac.
Next, we need to select an organization, if you have already made one you can select that, if not you can make one then select that. After selecting one click the create button.
Now you should see this panel except with the name of your game, not mine.
Click on the tab that says “Ads” then click the top button to enable them. It may ask if your game is aimed at kids under 13, click continue to get to the next page. After that unity will import the unity ads into your project.
Turn on the test mode, this will allow us to be able to test the ads in the unity editor.
After that is all set up we can start and make our first ad.
Create a c# script called AdsManager and attach it to an empty gameObject also called AdsManager in the hierarchy.
With this script, we can use Unity’s build-in code for playing ads that reward the player for watching them.
First, we need to add a “using UnityEngine.Advertisements;” at the top of the script to be able to use the code for ads.
Next, we can make a new public method called “ShowRewardedAd” this just makes it easy to see what kind of ad it is for later if we want to change/add in extra ads.
At this point, we can attach the method to the button on the shopkeeper, remember to save the script first, I won’t show up in that list otherwise.
Back to the AdsManager script.
First, we need to check if the ad is ready to play using the advertisement.IsRead(“name of the ad from the dashboard”)
You can find the name you need to use by going back to the services tab, click on the ads then the dashboard.
This should bring up a web page. on the sidebar, you should see Ad Units, click on that then Rewarded
As you can see for me, it’s Rewarded_Android and Rewarded_IOS, these names can be copied and pasted into IsReady.
At this point, we can use “Advertisement.Show(“Same name as we just checked”) but we want to be able to check if the video was skipped, finished, or failed. We can do this by making another method to check it using a switch statement.
Using that we can now check what the results of the video were.
after we check if the ad is ready we can take a look at the ad.show.
The 4th option gives us the chance to use those options we just made but first we need to make a new ShowOptions variable. Let’s call it options and assign it to a new ShowOptions and put options into the ad.Show method.
Now all that is left to do is add in what we want those options to do, we can put this inside the { } after the ShowOptions. All we need to do is use the resultCallback to = the new method we just made.
Result call back is part of the ShowOptions, we can see this if you select it and press F12.
But with that, we can actually test out if the ad will play or not.
Jump into the game and head over to the shopkeeper. After you get there you can click on the Watch Ad for 100G, atm it won’t give you anything but we can see if the ad is working.
Success! If it doesn’t work, do try restarting unity, which I ended up having to do.
But with that, we have added ads to the game. We still need to build on them to be able to give the player some gold but that is the easy part since they are just like pickups. See you in the next one!