Rewarded ads
This is an archived version of the documentation. Actual documentation for all platforms can be found here.
A rewarded ad is a configurable full-screen ad. The user gets a reward for viewing the ad.
Adding a rewarded ad
to the project
To enable advertising, create a RewardedAd
object in the script (in C#) that is attached to the GameObject
.
... using YandexMobileAds; using YandexMobileAds.Base; ... public class YandexMobileAdsRewardedAdDemoScript : MonoBehaviour { private RewardedAd rewardedAd; ... private void RequestRewardedAd() { string adUnitId = "YOUR_adUnitId"; rewardedAd = new RewardedAd(adUnitId); } ... }
Copied to clipboard
The RewardedAd
constructor contains the adUnitId
parameter, a unique identifier that is assigned in the Partner interface that takes the format R-M-XXXXXX-Y.
In the mobile mediation interface, the AdUnitId
is called the location ID.
Loading ads
After creating and configuring the RewardedAd
class object, you need to load ads. To load your ads, use the LoadAd
method which takes the AdRequest
object as an argument.
... private void RequestRewardedAd() { ... AdRequest request = new AdRequest.Builder().Build(); rewardedAd.LoadAd(request); ... } ...
Copied to clipboard
Displaying ads
After the ad has loaded, you can display it:
... private void ShowRewardedAd() { if (this.rewardedAd.IsLoaded()) { rewardedAd.Show(); } else { Debug.Log("Rewarded Ad is not ready yet"); } } ...
Copied to clipboard
Rewarded ad events
To track events that occur in a rewarded ad, register a delegate for the appropriate EventHandler
, as shown below:
... private void RequestRewardedAd() { ... rewardedAd.OnRewardedAdLoaded += HandleRewardedAdLoaded; rewardedAd.OnRewardedAdFailedToLoad += HandleRewardedAdFailedToLoad; rewardedAd.OnReturnedToApplication += HandleReturnedToApplication; rewardedAd.OnLeftApplication += HandleLeftApplication; rewardedAd.OnAdClicked += HandleAdClicked; rewardedAd.OnRewardedAdShown += HandleRewardedAdShown; rewardedAd.OnRewardedAdDismissed += HandleRewardedAdDismissed; rewardedAd.OnImpression += HandleImpression; rewardedAd.OnRewarded += HandleRewarded; rewardedAd.OnRewardedAdFailedToShow += HandleRewardedAdFailedToShow; ... } public void HandleRewardedAdLoaded(object sender, EventArgs args) { MonoBehaviour.print("HandleRewardedAdLoaded event received"); } public void HandleRewardedAdFailedToLoad(object sender, AdFailureEventArgs args) { MonoBehaviour.print( "HandleRewardedAdFailedToLoad event received with message: " + args.Message); } public void HandleReturnedToApplication(object sender, EventArgs args) { MonoBehaviour.print("HandleReturnedToApplication event received"); } public void HandleLeftApplication(object sender, EventArgs args) { MonoBehaviour.print("HandleLeftApplication event received"); } public void HandleAdClicked(object sender, EventArgs args) { MonoBehaviour.print("HandleAdClicked event received"); } public void HandleRewardedAdShown(object sender, EventArgs args) { MonoBehaviour.print("HandleRewardedAdShown event received"); } public void HandleRewardedAdDismissed(object sender, EventArgs args) { MonoBehaviour.print("HandleRewardedAdDismissed event received"); } public void HandleImpression(object sender, ImpressionData impressionData) { var data = impressionData == null ? "null" : impressionData.rawData; MonoBehaviour.print("HandleImpression event received with data: " + data); } public void HandleRewarded(object sender, Reward args) { MonoBehaviour.print("HandleRewarded event received: amout = " + args.amount + ", type = " + args.type); } public void HandleRewardedAdFailedToShow(object sender, AdFailureEventArgs args) { MonoBehaviour.print( "HandleRewardedAdFailedToShow event received with message: " + args.Message); }
Copied to clipboard
Clearing ads
When an ad object is no longer needed, you can delete it. To do this, call the Destroy
method:
rewardedAd.Destroy();
Copied to clipboard