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
AdUnitHandlerBase.cs
1using System;
2
3namespace SCG.SpaceAdMobile
4{
12 public abstract class AdUnitHandlerBase : AdUnitBase
13 {
15 public event Action<object> OnPaid;
17 public event Action OnImpressionRecorded;
19 public event Action OnLoaded;
21 public event Action<object> OnLoadFailed;
23 public event Action OnClicked;
25 public override event Action OnShown;
27 public event Action OnClosed;
29 public event Action<object> OnContentFailed;
31 public event Action<AdRewardBase> OnRewarded;
32
36 private int loadCounter;
37
39 protected override void OnAdPaid(object adValue)
40 {
41 OnAdPaidInternal(adValue);
42 OnPaid?.Invoke(adValue);
43 if (!MessageOptions.AdPaid)
44 return;
45
46 foreach (var item in GetComponents<IAdPaidMessage>())
47 {
48 item.AdPaid(adValue);
49 }
50 }
51
53 private protected virtual void OnAdPaidInternal(object adValue) { }
54
56 protected override void OnAdImpressionRecorded()
57 {
59 OnImpressionRecorded?.Invoke();
60 if (!MessageOptions.AdImpressionRecorded)
61 return;
62
63 foreach (var item in GetComponents<IAdImpressionRecordedMessage>())
64 {
65 item.AdImpressionRecorded();
66 }
67 }
68
70 private protected virtual void OnAdImpressionRecordedInternal() { }
71
73 protected override void OnAdLoaded()
74 {
75 loadCounter = 0;
76
78 OnLoaded?.Invoke();
79 if (MessageOptions.AdLoaded)
80 {
81 foreach (var item in GetComponents<IAdLoadedMessage>())
82 {
83 item.AdLoaded();
84 }
85 }
86
87 //Show if specific option is enabled.
88 if (AdBehaviour.DisplayImmediatelyOnLoad)
89 {
90 Show();
91 }
92 }
93
95 private protected virtual void OnAdLoadedInternal() { }
96
98 protected override void OnAdLoadFailed(object error)
99 {
100 loadCounter++;
101
103 OnLoadFailed?.Invoke(error);
104 if (MessageOptions.AdLoadFailed)
105 {
106 foreach (var item in GetComponents<IAdLoadFailedMessage>())
107 {
108 item.AdLoadFailed(error);
109 }
110 }
111
112 if (AdBehaviour.ReloadAfterLoadFailure && loadCounter < AdBehaviour.MaxLoadFailureAttempts)
113 Load();
114 }
115
117 private protected virtual void OnAdLoadFailedInternal(object error) { }
118
120 protected override void OnAdClicked()
121 {
123 OnClicked?.Invoke();
124 if (!MessageOptions.AdClicked)
125 return;
126
127 foreach (var item in GetComponents<IAdClickedMessage>())
128 {
129 item.AdClicked();
130 }
131 }
132
134 private protected virtual void OnAdClickedInternal() { }
135
137 protected override void OnAdShown()
138 {
140 OnShown?.Invoke();
141 if (!MessageOptions.AdShown)
142 return;
143
144 foreach (var item in GetComponents<IAdShownMessage>())
145 {
146 item.AdShown();
147 }
148 }
149
151 private protected virtual void OnAdShownInternal() { }
152
154 protected override void OnAdClosed()
155 {
158 OnClosed?.Invoke();
159 if (MessageOptions.AdClosed)
160 {
161 foreach (var item in GetComponents<IAdClosedMessage>())
162 {
163 item.AdClosed();
164 }
165 }
166
167 if (AdBehaviour.ReloadAfterContentClosed)
168 Load();
169 else
170 Unload();
171 }
172
174 private protected virtual void OnAdClosedInternal() { }
175
177 protected override void OnAdContentFailed(object error)
178 {
181 OnContentFailed?.Invoke(error);
182 if (MessageOptions.AdContentFailed)
183 {
184 foreach (var item in GetComponents<IAdContentFailedMessage>())
185 {
186 item.AdContentFailed(error);
187 }
188 }
189
190 if (AdBehaviour.ReloadAfterShowFailure)
191 Load();
192 else
193 Unload();
194 }
195
197 private protected virtual void OnAdContentFailedInternal(object error) { }
198
200 protected override void OnUserEarnedReward(AdRewardBase reward)
201 {
203 OnRewarded?.Invoke(reward);
204 if (!MessageOptions.AdReward)
205 return;
206
207 foreach (var item in GetComponents<IAdRewardMessage>())
208 {
209 item.AdReward(reward);
210 }
211 }
212
214 private protected virtual void OnUserEarnedRewardInternal(AdRewardBase reward) { }
215
219 protected override void OnDestroy()
220 {
221 OnPaid = null;
223 OnLoaded = null;
224 OnLoadFailed = null;
225 OnClicked = null;
226 OnShown = null;
227 OnClosed = null;
228 OnContentFailed = null;
229 OnRewarded = null;
230 }
231 }
232}
Represents a backend-agnostic description of an ad reward.
MessageOptions MessageOptions
Options for enabling Unity SendMessage methods.
Definition AdUnitBase.cs:21
void ReleaseFullscreenAdAudio()
Releases the audio policy for the current fullscreen ad show.
AdBehaviour AdBehaviour
Behavior options for displaying and showing ads.
Definition AdUnitBase.cs:16
Base class for ad units. This class defines the abstract and virtual properties and methods that are ...
Definition AdUnitBase.cs:9
override void OnAdLoadFailed(object error)
Raised when loading of the AdObject is failed.
override void OnAdClicked()
Raised when a click is recorded for an AdObject.
virtual void OnUserEarnedRewardInternal(AdRewardBase reward)
override void OnAdImpressionRecorded()
Raised when an impression is recorded for an AdObject.
override void OnAdLoaded()
Raised when the AdObject is loaded.
override void OnAdPaid(object adValue)
Raised when the AdObject is estimated to have earned money.
virtual void OnAdContentFailedInternal(object error)
override void OnUserEarnedReward(AdRewardBase reward)
Raised when the AdObject rewarded the user.
virtual void OnAdLoadFailedInternal(object error)
override void OnAdClosed()
Raised when the AdObject closed full screen content.
override void OnAdContentFailed(object error)
Raised when the AdObject failed to open full screen content.
override void OnAdShown()
Raised when an AdObject opened full screen content.
virtual void OnAdPaidInternal(object adValue)
override void OnDestroy()
Releases resources during Unity's destroy callback.
Serves as a base handler for ad unit operations, abstracting common functionality for different types...