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
LevelPlayAdUnitHandlers.cs
1using System;
2using System.Diagnostics;
3using System.Threading;
4using Unity.Services.LevelPlay;
5
6namespace SCG.SpaceAdMobile
7{
8 // The second partial part of the LevelPlayAdUnit class focuses on handling various advertisement events,
9 // particularly logging and responding to key lifecycle events such as ad load failures, ad displays, and user interactions.
10 // This part of the class includes methods that are invoked in response to specific ad events,
11 // allowing for comprehensive logging and additional processing through base class methods.
12 // Each handler method is designed to capture detailed information about the event,
13 // such as failure reasons or simple acknowledgment of user interaction, and log this information
14 // for debugging, analytics, and operational insights.
15 // These methods ensure that any significant event in the ad lifecycle is observed and managed effectively,
16 // supporting a responsive and observable ad management system.
17 public abstract partial class LevelPlayAdUnit<TAd>
18 {
22 public DateTime? LoadTime { get; protected set; }
23
24 private long? loadingStartTimestamp;
25
39 public long? LoadingTimeMs { get; protected set; }
40
44 protected void SetLoadingStartTime() => loadingStartTimestamp = Stopwatch.GetTimestamp();
45
49 protected void ResetLoadingStartTime() => loadingStartTimestamp = null;
50
54 private void SetLoadingTime()
55 {
56 LoadingTimeMs = loadingStartTimestamp is { } startTimestamp
57 ? (long)((Stopwatch.GetTimestamp() - startTimestamp) * 1000d / Stopwatch.Frequency)
58 : null;
59 loadingStartTimestamp = null;
60 }
61
66 protected virtual void OnAdInfoChanged(LevelPlayAdInfo levelPlayAdInfo) => ResponseInfo = levelPlayAdInfo;
67
72 protected virtual void OnAdLoaded(LevelPlayAdInfo levelPlayAdInfo)
73 {
74 LoadTime = DateTime.Now;
75 SetLoadingTime();
76 ResponseInfo = levelPlayAdInfo;
77 Logger.Log(LogOptions.AdLoadedLogText);
78 base.OnAdLoaded();
79 }
80
85 protected virtual void OnAdLoadFailed(LevelPlayAdError error)
86 {
88 Logger.LogError(LogOptions.AdLoadFailedText, error.ErrorMessage, error.ErrorCode, error.AdUnitId);
89 base.OnAdLoadFailed(error);
90 }
91
96 protected virtual void OnAdClicked(LevelPlayAdInfo levelPlayAdInfo)
97 {
98 Logger.Log(LogOptions.AdClickedLogText);
99 base.OnAdClicked();
100 }
101
106 protected virtual void OnAdShown(LevelPlayAdInfo levelPlayAdInfo)
107 {
108 Logger.Log(LogOptions.AdShownLogText);
109 base.OnAdShown();
110 }
111
116 protected virtual void OnAdClosed(LevelPlayAdInfo levelPlayAdInfo)
117 {
118 Logger.Log(LogOptions.AdClosedLogText);
119 base.OnAdClosed();
120 }
121
127 protected virtual void OnAdContentFailed(LevelPlayAdInfo levelPlayAdInfo, LevelPlayAdError error)
128 {
129 Logger.LogError(LogOptions.AdContentFailedLogText, error.ErrorMessage, error.ErrorCode, error.AdUnitId);
130 base.OnAdContentFailed(error);
131 }
132
139 protected virtual void OnUserEarnedReward(LevelPlayAdInfo levelPlayAdInfo, AdReward reward)
140 {
141 Logger.Log(LogOptions.AdRewardEarnedLogText, reward.Name, reward.Amount);
142 base.OnUserEarnedReward(reward);
143 }
144
149 protected virtual void OnAdImpressionDataReady(LevelPlayImpressionData impressionData)
150 {
151 var synchronizationContext = unitySynchronizationContext;
152 if (synchronizationContext == null)
153 return;
154
155 if (ReferenceEquals(synchronizationContext, SynchronizationContext.Current))
156 {
157 ProcessImpressionData(impressionData);
158 return;
159 }
160
161 synchronizationContext.Post(_ =>
162 {
163 if (this != null)
164 ProcessImpressionData(impressionData);
165 }, null);
166 }
167
172 private void ProcessImpressionData(LevelPlayImpressionData impressionData)
173 {
174 base.OnAdPaid(impressionData);
175 base.OnAdImpressionRecorded();
176 }
177 }
178}
override string Name
Definition AdReward.cs:34
Concrete adapter that wraps an SDK-specific reward object and exposes it through the SDK-agnostic AdR...
Definition AdReward.cs:19
virtual LogOptions LogOptions
Definition AdUnit.cs:59
virtual void OnAdLoadFailed(LevelPlayAdError error)
Handles the ad load failure event, logging the failure reason, the code, the ad unit ID,...
virtual void OnAdClosed(LevelPlayAdInfo levelPlayAdInfo)
Handles the ad closed event, logging that the ad was closed by the user.
virtual void OnAdImpressionDataReady(LevelPlayImpressionData impressionData)
Handles impression-level revenue data on the Unity main thread.
void SetLoadingStartTime()
Saves the start time of an ad-loading request.
LevelPlayAdInfo ResponseInfo
Contains detailed information about a LevelPlay advertisement, including its dimensions,...
virtual void OnAdShown(LevelPlayAdInfo levelPlayAdInfo)
Handles the event when an ad is shown to the user, logging the occurrence.
DateTime? LoadTime
Date and time when the ad was loaded.
virtual void OnUserEarnedReward(LevelPlayAdInfo levelPlayAdInfo, AdReward reward)
Handles the user earned reward event, logging the type and amount of the reward earned through intera...
virtual void OnAdInfoChanged(LevelPlayAdInfo levelPlayAdInfo)
Handles changes to the ad information by updating the ResponseInfo property.
void ResetLoadingStartTime()
Clears the saved start time when an ad-loading request cannot be started.
virtual void OnAdContentFailed(LevelPlayAdInfo levelPlayAdInfo, LevelPlayAdError error)
Handles the ad content failed event, logging the error encountered while displaying the ad content.
virtual void OnAdClicked(LevelPlayAdInfo levelPlayAdInfo)
Handles the ad click event, logging that the ad was clicked by the user.
virtual void OnAdLoaded(LevelPlayAdInfo levelPlayAdInfo)
Handles the ad loaded event, setting the load time and logging the event.
long? LoadingTimeMs
Gets the duration of the latest successful ad-loading request in milliseconds. Divide the value by 10...