Space Ad Mobile 9.5.1
Space Ad Mobile makes ads easy to add into your mobile game: one unified workflow for LevelPlay, AdMob, Yandex.Ads across banners, interstitials and rewarded.
Loading...
Searching...
No Matches
YandexAdUnit.cs
1#if !LEGACY_YANDEX_MOBILE_ADS
2using System;
3using System.Collections.Generic;
4using System.Threading.Tasks;
5using UnityEngine;
6using YandexMobileAds.Base;
7
8namespace SCG.SpaceAdMobile
9{
21 public abstract partial class YandexAdUnit<TAd> : AdUnit<TAd>, IAsyncAdUnit
22 {
26 public AdInfo ResponseInfo { get; protected set; }
27
35 protected abstract AdInfo GetResponseInfo();
36
38 [field: SerializeField]
39 [field: Tooltip("Loads this Yandex ad unit through the SDK async API when Load is called.")]
40 public bool LoadAsyncByDefault { get; set; }
41
43 protected override bool SdkInitialized => true;
44
46 public override void Unload()
47 {
49 base.Unload();
50 ResponseInfo = null;
51 }
52
54 public override bool Load()
55 {
56 if (!LoadAsyncByDefault) return base.Load();
57
58 if (!TryPrepareLoad())
59 return false;
60
61 LoadDefaultAsync();
62 return true;
63 }
64
66 protected override bool LoadInternal()
67 {
68 var customAdUnit = LoadCustomType();
69 if (customAdUnit != null)
70 AdObject = customAdUnit;
71
72 var adRequest = CreateAdRequest();
74 var loadingStarted = LoadInternal(adRequest);
75 if (!loadingStarted)
77
78 return loadingStarted;
79 }
80
82 public async Task<bool> LoadAsync()
83 {
84 var customAdUnit = LoadCustomType();
85 if (customAdUnit != null)
86 AdObject = customAdUnit;
87
88 var adRequest = CreateAdRequest();
90 var loadingStarted = await LoadInternalAsync(adRequest);
91 if (!loadingStarted)
93
94 return loadingStarted;
95 }
96
102 private async void LoadDefaultAsync()
103 {
104 try
105 {
106 await LoadAsync();
107 }
108 catch (AdLoadingException)
109 {
110 // The provider failure has already been routed through the regular load-failed callbacks.
111 }
112 catch (Exception exception)
113 {
114 Debug.LogException(exception, this);
115 }
116 }
117
124 protected abstract bool LoadInternal(AdRequest adRequest);
125
133 protected abstract Task<bool> LoadInternalAsync(AdRequest adRequest);
134
141 protected virtual TAd LoadCustomType() => default;
142
149 protected virtual AdRequest CreateAdRequest()
150 {
151 var requestOptions = SpaceAdMobile.CurrentRequestOptions;
152 return new AdRequest(
153 GetId(),
154 requestOptions?.CreateTargeting(),
155 requestOptions?.AdTheme ?? AdTheme.None,
156 requestOptions?.CreateParameters());
157 }
158
163 protected virtual void PrepareAdObject() { }
164
166 protected override SpaceAdLogger InitLogger()
167 {
168 var replaceLogValues = new Dictionary<string, Func<string>>
169 {
170 { "{ad}", () => SystemAdTypeName },
171 { "{time}", () => LoadTime?.ToString(System.Globalization.CultureInfo.CurrentCulture) },
172 { "{loadingTimeMs}", () => LoadingTimeMs?.ToString(System.Globalization.CultureInfo.InvariantCulture) ?? string.Empty },
173 { "{loadTimeMs}", () => LoadingTimeMs?.ToString(System.Globalization.CultureInfo.InvariantCulture) ?? string.Empty },
174 { "{name}", () => name }
175 };
176 return new SpaceAdLogger(this, replaceLogValues, LogOptions.Channel, !LogOptions.SuppressAllLogs);
177 }
178 }
179}
180#else // Supports versions before 8.0.
181using System.Collections.Generic;
182using System;
183using YandexMobileAds.Base;
184
185namespace SCG.SpaceAdMobile
186{
198 public abstract partial class YandexAdUnit<TAd> : AdUnit<TAd>
199 {
203 public AdInfo ResponseInfo { get; protected set; }
204
206 protected override bool SdkInitialized => true;
207
209 public override void Unload()
210 {
211 ResetLoadingStartTime();
212 base.Unload();
213 ResponseInfo = null;
214 }
215
217 protected override bool LoadInternal()
218 {
219 var customAdUnit = LoadCustomType();
220 if (customAdUnit != null)
221 {
222 //Create request used to load the Banner.
223 AdObject = customAdUnit;
224 var customAdBuilder = new AdRequest.Builder();
225 PrepareAdBuilder(customAdBuilder);
226 var customAdRequest = customAdBuilder.Build();
227 //Send the request to load the Banner.
228 SetLoadingStartTime();
229 var loadingStarted = LoadInternal(customAdRequest);
230 if (!loadingStarted)
231 ResetLoadingStartTime();
232
233 return loadingStarted;
234 }
235
236 //Create request used to load the ad.
237 var adBuilder = new AdRequestConfiguration.Builder(GetId());
238 PrepareAdBuilder(adBuilder);
239 var adRequest = adBuilder.Build();
240 //Send the request to load the ad.
241 SetLoadingStartTime();
242 var loadingStarted = LoadInternal(adRequest);
243 if (!loadingStarted)
244 ResetLoadingStartTime();
245
246 return loadingStarted;
247 }
248
254 protected abstract bool LoadInternal(AdRequestConfiguration adRequest);
255
262 protected virtual bool LoadInternal(AdRequest adRequest) => true;
263
270 protected virtual TAd LoadCustomType() => default;
271
278 protected virtual void PrepareAdBuilder(AdRequest.Builder adBuilder) { }
279
287 protected virtual void PrepareAdBuilder(AdRequestConfiguration.Builder adBuilder) { }
288
293 protected virtual void PrepareAdObject() { }
294
296 protected override SpaceAdLogger InitLogger()
297 {
298 var replaceLogValues = new Dictionary<string, Func<string>>
299 {
300 { "{ad}", () => SystemAdTypeName },
301 { "{time}", () => LoadTime?.ToString(System.Globalization.CultureInfo.CurrentCulture) },
302 { "{loadingTimeMs}", () => LoadingTimeMs?.ToString(System.Globalization.CultureInfo.InvariantCulture) ?? string.Empty },
303 { "{loadTimeMs}", () => LoadingTimeMs?.ToString(System.Globalization.CultureInfo.InvariantCulture) ?? string.Empty },
304 { "{name}", () => name }
305 };
306 return new SpaceAdLogger(this, replaceLogValues, LogOptions.Channel, !LogOptions.SuppressAllLogs);
307 }
308 }
309}
310#endif
string GetId()
Determines and returns the appropriate ad ID based on the current platform and ad mode....
Definition AdUnit.cs:332
TAd AdObject
The ad object associated with this unit. This property allows for direct interaction with the specifi...
Definition AdUnit.cs:22
string SystemAdTypeName
A convenient string representation of the system ad type, used primarily for logging and debugging pu...
Definition AdUnit.cs:89
bool TryPrepareLoad()
Performs shared loading preflight used by synchronous and asynchronous provider implementations....
Definition AdUnit.cs:200
virtual LogOptions LogOptions
Definition AdUnit.cs:59
static YandexRequestOptions CurrentRequestOptions
Current Yandex request options used by Yandex ad units. The value is refreshed from the active scene ...
Represents a mobile ad manager component specifically designed for integration with Google AdMob serv...
AdInfo ResponseInfo
Holds essential information for advertisements, excluding banners and app open ads.
bool LoadAsyncByDefault
Controls whether the regular Load call uses the Yandex SDK async loading API.
virtual void PrepareAdObject()
Optional overridable method for setting up the AdObject. This can be used for initializing or customi...
long? LoadingTimeMs
Gets the duration of the latest successful ad-loading request in milliseconds. Divide the value by 10...
override SpaceAdLogger InitLogger()
bool LoadInternal(AdRequest adRequest)
Loads an ad with a configured provider request. Derived classes implement the format-specific loader ...
async Task< bool > LoadAsync()
Loads the ad asynchronously. The returned task completes when loading succeeds or fails with the load...
override void Unload()
Unloads the ad.
AdInfo GetResponseInfo()
Retrieves detailed response information for the most recent ad request, providing insights into the a...
override bool Load()
Loads the ad.
virtual AdRequest CreateAdRequest()
Creates the request used by all Yandex ad formats. Request targeting and parameters are read from the...
virtual TAd LoadCustomType()
Creates a custom SDK ad object before loading. Banners use this to create their Banner instance befor...
void ResetLoadingStartTime()
Clears the saved start time when an ad-loading request cannot be started.
DateTime? LoadTime
Date and time when the ad was loaded.
Task< bool > LoadInternalAsync(AdRequest adRequest)
Loads an ad asynchronously with a configured provider request. Derived classes override this when the...
void SetLoadingStartTime()
Saves the start time of an ad-loading request.
Defines asynchronous loading support for ad units.