Yandex Mobile Ads
Loading...
Searching...
No Matches
Interstitial.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 Interstitial
20 {
21 private const string AdAlreadyPresentedErrorMessage =
22 "Interstitial ad was already presented. Single Interstitial 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
49 private IInterstitialClient _client;
50 private bool _adShown;
51
52 internal Interstitial(IInterstitialClient client)
53 {
54 this._client = client;
55 this._adShown = false;
56
57 MainThreadDispatcher.initialize();
58 ConfigureInterstitialEvents(this._client);
59 }
60
65 public AdInfo GetInfo()
66 {
67 return this._client.GetInfo();
68 }
69
73 public void Show()
74 {
75 if (_client == null)
76 {
77 return;
78 }
79
80 if (_adShown == true)
81 {
83 {
84 Message = AdAlreadyPresentedErrorMessage
85 };
86 this.OnAdFailedToShow(this, args);
87
88 return;
89 }
90
91 _adShown = true;
92 _client.Show();
93 }
94
98 public void Destroy()
99 {
100 _adShown = true;
101 if (_client != null)
102 {
103 _client.Destroy();
104 }
105 _client = null;
106
107 this.OnAdShown = null;
108 this.OnAdFailedToShow = null;
109 this.OnAdImpression = null;
110 this.OnAdClicked = null;
111 this.OnAdDismissed = null;
112 }
113
114 private void ConfigureInterstitialEvents(IInterstitialClient client)
115 {
116 if (client == null)
117 {
118 return;
119 }
120
121 client.OnAdClicked += (sender, args) =>
122 {
123 if (this.OnAdClicked == null)
124 {
125 return;
126 }
127
128 MainThreadDispatcher.EnqueueAction(() =>
129 {
130 if (this.OnAdClicked == null)
131 {
132 return;
133 }
134
135 this.OnAdClicked(this, args);
136 });
137 };
138
139 client.OnAdShown += (sender, args) =>
140 {
141 if (this.OnAdShown == null)
142 {
143 return;
144 }
145
146 MainThreadDispatcher.EnqueueAction(() =>
147 {
148 if (this.OnAdShown == null)
149 {
150 return;
151 }
152
153 this.OnAdShown(this, args);
154 });
155 };
156
157 client.OnAdDismissed += (sender, args) =>
158 {
159 if (this.OnAdDismissed == null)
160 {
161 return;
162 }
163
164 MainThreadDispatcher.EnqueueAction(() =>
165 {
166 if (this.OnAdDismissed == null)
167 {
168 return;
169 }
170
171 this.OnAdDismissed(this, args);
172 });
173 };
174
175 client.OnAdImpression += (sender, args) =>
176 {
177 if (this.OnAdImpression == null)
178 {
179 return;
180 }
181
182 MainThreadDispatcher.EnqueueAction(() =>
183 {
184 if (this.OnAdImpression == null)
185 {
186 return;
187 }
188
189 this.OnAdImpression(this, args);
190 });
191 };
192
193 client.OnAdFailedToShow += (sender, args) =>
194 {
195 if (this.OnAdFailedToShow == null)
196 {
197 return;
198 }
199
200 MainThreadDispatcher.EnqueueAction(() =>
201 {
202 if (this.OnAdFailedToShow == null)
203 {
204 return;
205 }
206
207 this.OnAdFailedToShow(this, args);
208 });
209 };
210
211 }
212 }
213}
Represents an event, that occurs when ad fails to perform an action.
Container for base Ad information.
Definition AdInfo.cs:18
Full-screen interstitial ad.
void Destroy()
Destroys Interstitial entirely and cleans up resources.
void Show()
Shows the interstitial ad. Single interstitial ad can be showed just once.
EventHandler< EventArgs > OnAdClicked
Notifies that the user clicked on the ad.
EventHandler< ImpressionData > OnAdImpression
Notifies that an impression was observed.
AdInfo GetInfo()
Returns base information about loaded Ad.
EventHandler< AdFailureEventArgs > OnAdFailedToShow
Notifies that the rewarded ad failed to show.
EventHandler< EventArgs > OnAdDismissed
Notifies that the rewarded ad has been dismissed.
EventHandler< EventArgs > OnAdShown
Notifies that the interstitial ad has been shown.