Yandex Mobile Ads
Loading...
Searching...
No Matches
RewardedAdLoader.cs
Go to the documentation of this file.
1/*
2 * This file is a part of the Yandex Advertising Network
3 *
4 * Version for Unity (C) 2023 YANDEX
5 *
6 * You may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at https://legal.yandex.com/partner_ch/
8 */
9
10using System;
12using YandexMobileAds.Common;
13using YandexMobileAds.Platforms;
14
15namespace YandexMobileAds
16{
20 public class RewardedAdLoader
21 {
22
26 public event EventHandler<RewardedAdLoadedEventArgs> OnAdLoaded;
27
31 public event EventHandler<AdFailedToLoadEventArgs> OnAdFailedToLoad;
32
33 private readonly AdRequestConfigurationFactory _adRequestConfigurationFactory;
34 private readonly IRewardedAdLoaderClient _client;
35
40 {
41 this._adRequestConfigurationFactory = new AdRequestConfigurationFactory();
42 this._client = YandexMobileAdsClientFactory.BuildRewardedAdLoaderClient();
43
44 MainThreadDispatcher.initialize();
45 ConfigureRewardedEvents();
46 }
47
53 public void LoadAd(AdRequestConfiguration adRequestConfiguration)
54 {
55 _client.LoadAd(_adRequestConfigurationFactory.CreateAdRequestConfiguration(adRequestConfiguration));
56 }
57
61 public void CancelLoading()
62 {
63 _client.CancelLoading();
64 }
65
66 private void ConfigureRewardedEvents()
67 {
68 this._client.OnAdLoaded += (sender, args) =>
69 {
70 if (this.OnAdLoaded == null)
71 {
72 return;
73 }
74
75 MainThreadDispatcher.EnqueueAction(() =>
76 {
77 if (this.OnAdLoaded == null)
78 {
79 return;
80 }
81
82 RewardedAdLoadedEventArgs adLoadedEventArgs = new RewardedAdLoadedEventArgs()
83 {
84 RewardedAd = new RewardedAd(args.Value)
85 };
86 this.OnAdLoaded(this, adLoadedEventArgs);
87 });
88 };
89
90 this._client.OnAdFailedToLoad += (sender, args) =>
91 {
92 if (this.OnAdFailedToLoad == null)
93 {
94 return;
95 }
96
97 MainThreadDispatcher.EnqueueAction(() =>
98 {
99 if (this.OnAdFailedToLoad == null)
100 {
101 return;
102 }
103
104 this.OnAdFailedToLoad(this, args);
105 });
106 };
107 }
108 }
109}
A class with data for a targeted ad request.
Full-screen rewarded ad.
Definition RewardedAd.cs:20
Full-screen rewarded ads.
void LoadAd(AdRequestConfiguration adRequestConfiguration)
Starts loading the ad by AdRequestConfiguration. A successfuly loaded RewardedAd will be delivered vi...
void CancelLoading()
Cancel active loading of the rewarded ads.
EventHandler< AdFailedToLoadEventArgs > OnAdFailedToLoad
Notifies that a rewarded ad request failed.
RewardedAdLoader()
Cunstructs an object of the RewardedAdLoader class.
EventHandler< RewardedAdLoadedEventArgs > OnAdLoaded
Notifies that ad is loaded.