Hello, I’m trying to set a function which would display an ad and, if shown, call another function. I found a lot of tutotials on Internet but each was different from each other… Can you help me in finding a simple way to do that ?
Thanks.
@username
When your rewarded video complete then HandleRewardBasedVideoRewarded callback fires and you can call your function.
using System;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;
using System.Collections;
public class RewardedVideoScript : MonoBehaviour
{
const string adUnitIdRewardedVideo = "ca-app-pub-xxxxxxxx/xxxxxxx"; // Your Key
protected RewardBasedVideoAd rewardBasedVideo;
void OnEnable ()
{
rewardBasedVideo = RewardBasedVideoAd.Instance;
// RewardBasedVideoAd is a singleton, so handlers should only be registered once.
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}
void Start ()
{
RequestRewardBasedVideo ();
}
void OnDisable ()
{
rewardBasedVideo.OnAdRewarded -= HandleRewardBasedVideoRewarded;
}
protected AdRequest createAdRequest ()
{
AdRequest request = new AdRequest.Builder ().Build ();
// .AddTestDevice(AdRequest.TestDeviceSimulator) // Simulator.
// .AddTestDevice("E32C2E79AD1DAE9D9AE99EF4F61E80ED") // My test device.
// .Build();
return request;
}
public void RequestRewardBasedVideo ()
{
rewardBasedVideo.LoadAd (createAdRequest (), adUnitIdRewardedVideo);
}
public void ShowRewardBasedVideo ()
{
if (rewardBasedVideo.IsLoaded ()) {
rewardBasedVideo.Show ();
} else {
RequestRewardBasedVideo ();
}
}
public void HandleRewardBasedVideoRewarded (object sender, Reward args)
{
//Call your function here
}
}