You've successfully subscribed to Hacking with Unity - Learn how to create your own games
Great! Next, complete checkout for full access to Hacking with Unity - Learn how to create your own games
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

Unity: Simple example of integrating the RewardedVideo into your game (Part 2/4)

integrating the RewardedVideo adv, admob adsense into your game

Ben Liu
Ben Liu

For the prerequisite steps, such as how to install Unity Advertisement package and integrated into project, please have a look at this:

Unity: Integrate the Advertisement package and monetize your game (Part 1/4)
Install the Advertisement package and setup environment ‌There are few things needed before integration, and first of all you need to install the Unity Advertisement package: Step 1: Install Unity Advertisement package: Till now, the latest version is 3.4.7, you can use newer version if you like. …

Here is a simple and easy use case of how to put rewardedvideo ads in your game. Image we have a button in the game named extra life. The players will gain an extra life (extra coin, gold or anything you want) when they finish watching the rewaredVideo:

Menu asking for Extra life 

For this extra life button, we create a script attached to it (This script is from Unity doc):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.UI;

public class RewardedVideoButton : MonoBehaviour, IUnityAdsListener {
    
     #if UNITY_IOS
    private string gameId = "3685328";
    #elif UNITY_ANDROID
    private string gameId = "3685329";
    #endif

    Button myButton;
    public string myPlacementId = "rewardedVideo";

    void Start () {   
        myButton = GetComponent <Button> ();

        // Set interactivity to be dependent on the Placement’s status:
        myButton.interactable = Advertisement.IsReady (myPlacementId); 

        // Map the ShowRewardedVideo function to the button’s click listener:
        if (myButton) myButton.onClick.AddListener (ShowRewardedVideo);

        // Initialize the Ads listener and service:
        Advertisement.AddListener (this);
        Advertisement.Initialize (gameId, true);
    }

    // Implement a function for showing a rewarded video ad:
    void ShowRewardedVideo () {
        Advertisement.Show (myPlacementId);
    }

    // Implement IUnityAdsListener interface methods:
    public void OnUnityAdsReady (string placementId) {
        // If the ready Placement is rewarded, activate the button: 
        if (placementId == myPlacementId) {        
            myButton.interactable = true;
        }
    }

    public void OnUnityAdsDidFinish (string placementId, ShowResult showResult) {
        // Define conditional logic for each ad completion status:
        if (showResult == ShowResult.Finished) {
            // Reward the user for watching the ad to completion.
            Debug.Log("Player gain extra life");
        } else if (showResult == ShowResult.Skipped) {
            // Do not reward the user for skipping the ad.
        } else if (showResult == ShowResult.Failed) {
            Debug.LogWarning ("The ad did not finish due to an error.");
        }
    }

    public void OnUnityAdsDidError (string message) {
        // Log the error.
    }

    public void OnUnityAdsDidStart (string placementId) {
        // Optional actions to take when the end-users triggers an ad.
    }
}

When user click the Extra life button, a test adv will be displayed.


What is the next from here?

Ben Liu

Please feel free to contact me if you have any question. <br> https://t.me/NeedoneApp