Yandex Mobile Ads
Loading...
Searching...
No Matches
RewardedAd.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;
13
14namespace YandexMobileAds
15{
19 public class RewardedAd
20 {
21 private const string AdAlreadyPresentedErrorMessage =
22 "Rewarded ad was already presented. Single Rewarded ad can be presented just once.";
23
27 public event EventHandler<EventArgs> OnAdShown;
28
32 public event EventHandler<AdFailureEventArgs> OnAdFailedToShow;
33
37 public event EventHandler<EventArgs> OnAdDismissed;
38
42 public event EventHandler<EventArgs> OnAdClicked;
43
47 public event EventHandler<ImpressionData> OnAdImpression;
48
52 public event EventHandler<Reward> OnRewarded;
53
54 private IRewardedAdClient _client;
55 private bool _adShown;
56
57 internal RewardedAd(IRewardedAdClient client)
58 {
59 this._client = client;
60 this._adShown = false;
61
62 MainThreadDispatcher.initialize();
63 ConfigureRewardedAdEvents(this._client);
64 }
65
70 public AdInfo GetInfo()
71 {
72 return this._client.GetInfo();
73 }
74
78 public void Show()
79 {
80 if (_client == null)
81 {
82 return;
83 }
84
85 if (_adShown == true)
86 {
88 {
89 Message = AdAlreadyPresentedErrorMessage
90 };
91 this.OnAdFailedToShow(this, args);
92
93 return;
94 }
95
96 _adShown = true;
97 _client.Show();
98 }
99
103 public void Destroy()
104 {
105 _adShown = false;
106
107 if (_client != null)
108 {
109 _client.Destroy();
110 }
111
112 _client = null;
113
114 this.OnAdShown = null;
115 this.OnAdFailedToShow = null;
116 this.OnAdImpression = null;
117 this.OnAdClicked = null;
118 this.OnAdDismissed = null;
119 this.OnRewarded = null;
120 }
121
122 private void ConfigureRewardedAdEvents(IRewardedAdClient client)
123 {
124 if (client == null)
125 {
126 return;
127 }
128
129 client.OnAdShown += (sender, args) =>
130 {
131 if (this.OnAdShown == null)
132 {
133 return;
134 }
135
136 MainThreadDispatcher.EnqueueAction(() =>
137 {
138 if (this.OnAdShown == null)
139 {
140 return;
141 }
142
143 this.OnAdShown(this, args);
144 });
145 };
146
147 client.OnAdFailedToShow += (sender, args) =>
148 {
149 if (this.OnAdFailedToShow == null)
150 {
151 return;
152 }
153
154 MainThreadDispatcher.EnqueueAction(() =>
155 {
156 if (this.OnAdFailedToShow == null)
157 {
158 return;
159 }
160
161 this.OnAdFailedToShow(this, args);
162 });
163 };
164
165 client.OnAdDismissed += (sender, args) =>
166 {
167 if (this.OnAdDismissed == null)
168 {
169 return;
170 }
171
172 MainThreadDispatcher.EnqueueAction(() =>
173 {
174 if (this.OnAdDismissed == null)
175 {
176 return;
177 }
178
179 this.OnAdDismissed(this, args);
180 });
181 };
182
183 client.OnAdImpression += (sender, args) =>
184 {
185 if (this.OnAdImpression == null)
186 {
187 return;
188 }
189
190 MainThreadDispatcher.EnqueueAction(() =>
191 {
192 if (this.OnAdImpression == null)
193 {
194 return;
195 }
196
197 this.OnAdImpression(this, args);
198 });
199 };
200
201 client.OnAdClicked += (sender, args) =>
202 {
203 if (this.OnAdClicked == null)
204 {
205 return;
206 }
207
208 MainThreadDispatcher.EnqueueAction(() =>
209 {
210 if (this.OnAdClicked == null)
211 {
212 return;
213 }
214
215 this.OnAdClicked(this, args);
216 });
217 };
218
219 client.OnRewarded += (sender, args) =>
220 {
221 if (this.OnRewarded == null)
222 {
223 return;
224 }
225
226 MainThreadDispatcher.EnqueueAction(() =>
227 {
228 if (this.OnRewarded == null)
229 {
230 return;
231 }
232
233 this.OnRewarded(this, args);
234 });
235 };
236 }
237 }
238}
Represents an event, that occurs when ad fails to perform an action.
Container for base Ad information.
Definition AdInfo.cs:18
Full-screen rewarded ad.
Definition RewardedAd.cs:20
EventHandler< Reward > OnRewarded
Notifies that the user can be rewarded.
Definition RewardedAd.cs:52
EventHandler< AdFailureEventArgs > OnAdFailedToShow
Notifies that the rewarded ad failed to show.
Definition RewardedAd.cs:32
EventHandler< EventArgs > OnAdShown
Notifies that the rewarded ad has been shown.
Definition RewardedAd.cs:27
EventHandler< EventArgs > OnAdDismissed
Notifies that the rewarded ad has been dismissed.
Definition RewardedAd.cs:37
void Show()
Shows the rewarded ad. Single rewarded ad can be showed just once.
Definition RewardedAd.cs:78
AdInfo GetInfo()
Returns base information about loaded Ad.
Definition RewardedAd.cs:70
void Destroy()
Destroys Rewarded entirely and cleans up resources.
EventHandler< ImpressionData > OnAdImpression
Notifies that an impression was observed.
Definition RewardedAd.cs:47
EventHandler< EventArgs > OnAdClicked
Notifies that the user clicked on the ad.
Definition RewardedAd.cs:42