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
GoogleAdUnit.cs
1using System.Collections.Generic;
2using System;
3using GoogleMobileAds.Api;
4using UnityEngine;
5
6namespace SCG.SpaceAdMobile
7{
19 public abstract partial class GoogleAdUnit<TAd> : AdUnit<TAd>
20 {
24 public ResponseInfo ResponseInfo { get; protected set; }
25
27 protected override bool SdkInitialized => SpaceAdMobile.Ready || Application.isEditor;
28
30 public override void Unload()
31 {
33 base.Unload();
34 ResponseInfo = null;
35 }
36
38 protected override bool LoadInternal()
39 {
40 Logger.Log(LogOptions.AdLoadingLogText);
41
42 //Create request used to load the ad.
43 var adRequest = new AdRequest();
44 PrepareAdRequest(adRequest);
45 //Send the request to load the ad.
47 var loadingStarted = LoadInternal(adRequest);
48 if (!loadingStarted)
50
51 return loadingStarted;
52 }
53
67 protected virtual void OnLoad(TAd ad, LoadAdError error)
68 {
69 //If error
70 if (error != null || ad == null)
71 {
72 OnAdLoadFailed(error);
73 return;
74 }
75
76 //Loading completed
77 AdObject = ad;
78 OnAdLoaded();
80 }
81
87 protected abstract bool LoadInternal(AdRequest adRequest);
88
97 protected abstract ResponseInfo GetResponseInfo();
98
103 protected virtual void PrepareAdRequest(AdRequest adRequest) { }
104
109 protected virtual void PrepareAdObject() => ResponseInfo = GetResponseInfo();
110
112 protected override SpaceAdLogger InitLogger()
113 {
114 var replaceLogValues = new Dictionary<string, Func<string>>
115 {
116 { "{ad}", () => SystemAdTypeName },
117 { "{time}", () => LoadTime?.ToString(System.Globalization.CultureInfo.CurrentCulture) },
118 { "{loadingTimeMs}", () => LoadingTimeMs?.ToString(System.Globalization.CultureInfo.InvariantCulture) ?? string.Empty },
119 { "{loadTimeMs}", () => LoadingTimeMs?.ToString(System.Globalization.CultureInfo.InvariantCulture) ?? string.Empty },
120 { "{name}", () => name },
121 { "{adSourceName}", () => ResponseInfo?.GetLoadedAdapterResponseInfo()?.AdSourceName },
122 { "{adSourceInstanceName}", () => ResponseInfo?.GetLoadedAdapterResponseInfo()?.AdSourceInstanceName },
123 { "{adapterClassName}", () => ResponseInfo?.GetLoadedAdapterResponseInfo()?.AdapterClassName }
124 };
125 return new SpaceAdLogger(this, replaceLogValues, LogOptions.Channel, !LogOptions.SuppressAllLogs);
126 }
127 }
128}
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
virtual LogOptions LogOptions
Definition AdUnit.cs:59
void RegisterEventHandlers()
To further customize the behavior of your ad, you can hook into a number of events in the ad's lifecy...
virtual void OnAdLoadFailed(LoadAdError error)
Handles the ad load failed event, logging the error encountered during ad loading.
override void OnAdLoaded()
Handles the ad loaded event, setting the load time, preparing the ad object for display,...
void SetLoadingStartTime()
Saves the start time of an ad-loading request.
long? LoadingTimeMs
Gets the duration of the latest successful ad-loading request in milliseconds. Divide the value by 10...
override void Unload()
Unloads the ad.
bool LoadInternal(AdRequest adRequest)
Abstract method for loading an ad with a specified request configuration. This method must be impleme...
ResponseInfo GetResponseInfo()
Retrieves detailed response information for the most recent ad request, providing insights into the a...
ResponseInfo ResponseInfo
Holds essential information for advertisements.
virtual void PrepareAdObject()
Optional overridable method for setting up the AdObject. This can be used for initializing or customi...
DateTime? LoadTime
Date and time when the ad was loaded.
void ResetLoadingStartTime()
Clears the saved start time when an ad-loading request cannot be started.
override SpaceAdLogger InitLogger()
virtual void PrepareAdRequest(AdRequest adRequest)
Optional overridable method for setting up the ad request. This can be used to customize ad requests.
virtual void OnLoad(TAd ad, LoadAdError error)
Called when an ad loading attempt concludes. It evaluates the result of the loading process,...
static bool Ready
Indicates whether the SDK has been initialized.