If you played lots of mobile game, you must know the mysterious power of ads. Sometimes you hate it, and sometimes you need it to give you some rewards.
In this article, we will give Player 100G as rewards after watching an ad by using Unity Ads services.
Enable Unity Ads services
Open Package Manager, search Advertisements and install it.
Once installed, select Window> General> Services from drop down menu.
Select Ads in Services window and enable it. Then check on both Enable test mode & Enable build-in extension.
That’s all we need to do so far. Next we can create a button to watch ads.
Button to play ads
Let’s create the ad-play button . Add a new button in Shop panel.
Once finished button setting, we can create the function to make Player watching ads.
We need to create an empty object and create a script “AdsManager”attach to it.
Open this script. First we need to add the library to it.
Then we need to implement an interface to make the ads workable.
This interface would need to implement other 4 methods, which will be added later.
Now we need to create some variables to process the main part of watching ads.
Now we can open Dashboard of our Unity project to locate those 2 Id:
Inside Services window, click Go to Dashboard. It should open page to Unity Dashboard.
For the first time of using Dashboard, it might ask you to enable Ads. After enable it, select Ad Unit from side menu and you should see the Game ID of your project.
Then select Rewarded Android and it would open a window that shows all information you need. We will use Ad Unit ID in our script, click copy icon to copy that.
Back to editor, select Ads_Manager gameobject and paste Unit ID and Game ID to the column we created.
Now we can back to script to finish the ads function.
Use Awake() method to get reference of Player, Add an Ads Listener, and initialize ads services.
Then we also need a public method that could assign to button.
Then back to editor and drag the button into variable column in Ads_Manager. And also drag Ads_Manger into OnClick() function of that button.
Now we can implement the needed methods from interface to finish this script.
Implement methods from interface
There are 4 methods needed to be implemented when implement the interface: IUnityAdsListener.
We will start with the easy one.
OnUnityAdsDidStart() is using to proceed some function after ads played, and we do not need it in this game. We can leave a comment in it.
OnUnityAdsDidError() would be called when error occurred. Let’s simply leave a Debug.LogError to it.
OnUnityAdsReady() will be called when Ads is ready. In this, we will make sure the ID is same as our Unit ID. Create an if statement to check, and set the button to interactable when it is true.
The last one is OnUnityAdsDidFinish(). This would check whether ads is finished playing, been skipped, or failed to play.
Create a switch statement to check the state, and we would set the rewards to Player if it finished playing.
After this, these 4 methods would look like this.
All done! We can build the game and test it in mobile.