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