Yandex Mobile Ads
Loading...
Searching...
No Matches
Banner.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 Banner
21 {
25 public event EventHandler<EventArgs> OnAdLoaded;
26
30 public event EventHandler<AdFailureEventArgs> OnAdFailedToLoad;
31
35 public event EventHandler<EventArgs> OnReturnedToApplication;
36
41 public event EventHandler<EventArgs> OnLeftApplication;
42
46 public event EventHandler<EventArgs> OnAdClicked;
47
51 public event EventHandler<ImpressionData> OnImpression;
52
53 private readonly IBannerClient _client;
54 private readonly AdRequestCreator _adRequestFactory;
55
62 public Banner(string blockId, BannerAdSize adSize, AdPosition position)
63 {
64 this._adRequestFactory = new AdRequestCreator();
65 this._client = YandexMobileAdsClientFactory.BuildBannerClient(blockId, adSize, position);
66
67 MainThreadDispatcher.initialize();
68 ConfigureBannerEvents();
69 }
70
75 public void LoadAd(AdRequest request)
76 {
77 _client.LoadAd(_adRequestFactory.CreateAdRequest(request));
78 }
79
83 public void Hide()
84 {
85 _client.Hide();
86 }
87
91 public void Show()
92 {
93 _client.Show();
94 }
95
99 public void Destroy()
100 {
101 _client.Destroy();
102 }
103
104 private void ConfigureBannerEvents()
105 {
106 this._client.OnAdLoaded += (sender, args) =>
107 {
108 if (this.OnAdLoaded == null)
109 {
110 return;
111 }
112
113 MainThreadDispatcher.EnqueueAction(() =>
114 {
115 if (this.OnAdLoaded == null)
116 {
117 return;
118 }
119
120 this.OnAdLoaded(this, args);
121 });
122 };
123
124 this._client.OnAdFailedToLoad += (sender, args) =>
125 {
126 if (this.OnAdFailedToLoad == null)
127 {
128 return;
129 }
130
131 MainThreadDispatcher.EnqueueAction(() =>
132 {
133 if (this.OnAdFailedToLoad == null)
134 {
135 return;
136 }
137
138 this.OnAdFailedToLoad(this, args);
139 });
140 };
141
142 this._client.OnReturnedToApplication += (sender, args) =>
143 {
144 if (this.OnReturnedToApplication == null)
145 {
146 return;
147 }
148
149 MainThreadDispatcher.EnqueueAction(() =>
150 {
151 if (this.OnReturnedToApplication == null)
152 {
153 return;
154 }
155
156 this.OnReturnedToApplication(this, args);
157 });
158 };
159
160 this._client.OnLeftApplication += (sender, args) =>
161 {
162 if (this.OnLeftApplication == null)
163 {
164 return;
165 }
166
167 MainThreadDispatcher.EnqueueAction(() =>
168 {
169 if (this.OnLeftApplication == null)
170 {
171 return;
172 }
173
174 this.OnLeftApplication(this, args);
175 });
176 };
177
178 this._client.OnAdClicked += (sender, args) =>
179 {
180 if (this.OnAdClicked == null)
181 {
182 return;
183 }
184
185 MainThreadDispatcher.EnqueueAction(() =>
186 {
187 if (this.OnAdClicked == null)
188 {
189 return;
190 }
191
192 this.OnAdClicked(this, args);
193 });
194 };
195
196 this._client.OnImpression += (sender, args) =>
197 {
198 if (this.OnImpression == null)
199 {
200 return;
201 }
202
203 MainThreadDispatcher.EnqueueAction(() =>
204 {
205 if (this.OnImpression == null)
206 {
207 return;
208 }
209
210 this.OnImpression(this, args);
211 });
212 };
213 }
214 }
215}
A class for displaying banner ad view.
Definition Banner.cs:21
Banner(string blockId, BannerAdSize adSize, AdPosition position)
Initializes an object of the Banner class to display the banner with the specified size.
Definition Banner.cs:62
void Show()
Shows Banner on screen.
Definition Banner.cs:91
EventHandler< EventArgs > OnLeftApplication
Notifies that the app will become inactive now because the user clicked on the banner ad and is about...
Definition Banner.cs:41
void Hide()
Hides Banner from screen.
Definition Banner.cs:83
EventHandler< EventArgs > OnAdLoaded
Notifies that the banner is loaded. At this time, you can add banner if you haven’t done so yet.
Definition Banner.cs:25
EventHandler< AdFailureEventArgs > OnAdFailedToLoad
Notifies that the banner failed to load.
Definition Banner.cs:30
EventHandler< EventArgs > OnAdClicked
Notifies that the user has clicked on the banner.
Definition Banner.cs:46
void LoadAd(AdRequest request)
Loads Banner with data for targeting.
Definition Banner.cs:75
EventHandler< EventArgs > OnReturnedToApplication
Called when user returned to application after click.
Definition Banner.cs:35
void Destroy()
Destroys Banner.
Definition Banner.cs:99
EventHandler< ImpressionData > OnImpression
Notifies delegate when an impression was tracked.
Definition Banner.cs:51
Contains targeting information used to fetch an ad. new instance should be created using AdRequestCon...
Definition AdRequest.cs:20
This class is responsible for the banner size.
AdPosition
Position of ad on screen.
Definition AdPosition.cs:16