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
AdButtonToolkit.cs
1using UnityEngine;
2using UnityEngine.UIElements;
3
5{
11 [DisallowMultipleComponent]
12 [AddComponentMenu("SCG/SpaceAdMobile/" + nameof(AdButtonToolkit))]
13 public class AdButtonToolkit : MonoBehaviour
14 {
15 #region Serialized fields
16
18 [Tooltip("Invoke AdUnit.Load() when clicked.")]
19 [SerializeField] private bool load;
20
22 [Tooltip("Invoke AdUnit.Show() when clicked.")]
23 [SerializeField] private bool show;
24
26 [Tooltip("Invoke Hide() on the controller if it implements IConcealableAd.")]
27 [SerializeField] private bool hide;
28
30 [Tooltip("Ad controller that hosts the current AdUnit (auto-filled in Editor).")]
31 [SerializeField] private AdControllerBase unitController;
32
34 [Tooltip("Specific AdUnit handler; if null, will be taken from controller.Ad (auto-filled in Editor).")]
35 [SerializeField] private AdUnitHandlerBase adUnit;
36
38 [Tooltip("UIDocument containing the target button; auto-filled from this GameObject if missing.")]
39 [SerializeField] private UIDocument uiDocument;
40
41#if UNITY_6000_5_OR_NEWER
43 [Tooltip("PanelRenderer containing the target button; used when no UIDocument is available.")]
44 [SerializeField] private PanelRenderer panelRenderer;
45#endif
46
48 [Tooltip("Name of the UI Toolkit Button in the visual tree (Q<Button> by name).")]
49 [SerializeField] private string buttonName = "AdButton";
50
51 #endregion
52
53 #region Non-serialized state
54
56 private Button _button;
57
58#if UNITY_6000_5_OR_NEWER
59 private PanelRenderer registeredPanelRenderer;
60 private VisualElement currentRoot;
61 private int lastPanelReloadVersion = -1;
62#endif
63
64 #endregion
65
66 #region Unity lifecycle
67
68#if UNITY_EDITOR
75 protected virtual void OnValidate()
76 {
77#pragma warning disable UNT0039 // Use RequireComponent attribute when self-invoking GetComponent
78 unitController = unitController ? unitController : GetComponent<AdControllerBase>();
79 adUnit = adUnit ? adUnit : GetComponent<AdUnitHandlerBase>();
80 uiDocument = uiDocument ? uiDocument : GetComponent<UIDocument>();
81#if UNITY_6000_5_OR_NEWER
82 panelRenderer = panelRenderer ? panelRenderer : GetComponent<PanelRenderer>();
83#endif
84#pragma warning restore UNT0039 // Use RequireComponent attribute when self-invoking GetComponent
85 }
86#endif
87
93 protected virtual void OnEnable()
94 {
95#if UNITY_6000_5_OR_NEWER
96 SynchronizeSource();
97#else
98#pragma warning disable UNT0039 // Use RequireComponent attribute when self-invoking GetComponent
99 uiDocument = uiDocument ? uiDocument : GetComponent<UIDocument>();
100#pragma warning restore UNT0039 // Use RequireComponent attribute when self-invoking GetComponent
101#endif
102
103 if (uiDocument != null)
104 {
105 RebindButton(uiDocument.rootVisualElement);
106 }
107#if UNITY_6000_5_OR_NEWER
108 else if (registeredPanelRenderer == null)
109 Debug.LogWarning($"{nameof(AdButtonToolkit)}: UIDocument or PanelRenderer not found.");
110#else
111 else
112 {
113 Debug.LogWarning($"{nameof(AdButtonToolkit)}: UIDocument not found.");
114 }
115#endif
116
117 }
118
123 protected virtual void OnDisable()
124 {
125 ClearButton();
126
127#if UNITY_6000_5_OR_NEWER
128 UnregisterPanelRenderer();
129#endif
130 }
131
132 #endregion
133
134 #region Handlers
135
140 public void Rebind()
141 {
142 if (!isActiveAndEnabled)
143 return;
144
145#if UNITY_6000_5_OR_NEWER
146 SynchronizeSource();
147#else
148#pragma warning disable UNT0039 // Use RequireComponent attribute when self-invoking GetComponent
149 uiDocument = uiDocument ? uiDocument : GetComponent<UIDocument>();
150#pragma warning restore UNT0039 // Use RequireComponent attribute when self-invoking GetComponent
151#endif
152
153 // ReSharper disable once InvertIf
154 if (uiDocument != null)
155 {
156 RebindButton(uiDocument.rootVisualElement);
157 // ReSharper disable once RedundantJumpStatement
158 return;
159 }
160
161#if UNITY_6000_5_OR_NEWER
162 RebindButton(currentRoot);
163#endif
164 }
165
172 public void OnButtonClick()
173 {
174 if (adUnit == null && unitController != null)
175 adUnit = unitController.Ad as AdUnitHandlerBase;
176
177 if (adUnit == null)
178 throw new System.NullReferenceException(nameof(adUnit));
179
180 if (load) adUnit.Load();
181 if (show) adUnit.Show();
182
183 // ReSharper disable once SuspiciousTypeConversion.Global
184 if (hide) ((IConcealableAd)unitController)?.Hide();
185 }
186
187 #endregion
188
189 #region Helpers
190
195 private void RebindButton(VisualElement root)
196 {
197 ClearButton();
198
199 if (root == null)
200 {
201 Debug.LogWarning($"{nameof(AdButtonToolkit)}: rootVisualElement is null.");
202 return;
203 }
204
205 _button = root.Q<Button>(buttonName);
206 if (_button == null)
207 {
208 Debug.LogWarning($"{nameof(AdButtonToolkit)}: Button '{buttonName}' not found.");
209 return;
210 }
211
212 _button.clicked += OnButtonClick;
213 }
214
215 private void ClearButton()
216 {
217 if (_button != null)
218 _button.clicked -= OnButtonClick;
219
220 _button = null;
221 }
222
223#if UNITY_6000_5_OR_NEWER
224 private void SynchronizeSource()
225 {
226 var resolvedDocument = uiDocument;
227 if (resolvedDocument == null)
228 TryGetComponent(out resolvedDocument);
229
230 var resolvedRenderer = resolvedDocument == null ? panelRenderer : null;
231 if (resolvedDocument == null && resolvedRenderer == null)
232 TryGetComponent(out resolvedRenderer);
233
234 var sourceChanged = uiDocument != resolvedDocument || registeredPanelRenderer != resolvedRenderer;
235 if (sourceChanged)
236 {
237 ClearButton();
238 currentRoot = null;
239 lastPanelReloadVersion = -1;
240 }
241
242 uiDocument = resolvedDocument;
243
244 if (uiDocument != null)
245 {
246 UnregisterPanelRenderer();
247 return;
248 }
249
250 if (registeredPanelRenderer == resolvedRenderer)
251 return;
252
253 UnregisterPanelRenderer();
254 registeredPanelRenderer = resolvedRenderer;
255 if (registeredPanelRenderer == null) return;
256 registeredPanelRenderer.RegisterUIReloadCallback(OnUIReload);
257 ((IPanelComponent)registeredPanelRenderer).PerformUpdate();
258 }
259
260 private void UnregisterPanelRenderer()
261 {
262 if (registeredPanelRenderer == null)
263 return;
264
265 registeredPanelRenderer.UnregisterUIReloadCallback(OnUIReload);
266 registeredPanelRenderer = null;
267 currentRoot = null;
268 }
269#endif
270
271#if UNITY_6000_5_OR_NEWER
279 private void OnUIReload(PanelRenderer renderer, VisualElement root, int version)
280 {
281 if (!isActiveAndEnabled)
282 return;
283
284 SynchronizeSource();
285 if (uiDocument != null)
286 {
287 RebindButton(uiDocument.rootVisualElement);
288 return;
289 }
290
291 if (renderer != registeredPanelRenderer || version == lastPanelReloadVersion)
292 return;
293
294 lastPanelReloadVersion = version;
295 currentRoot = root;
296 RebindButton(currentRoot);
297 }
298#endif
299
300 #endregion
301 }
302}
Base class for controlling ad units within Unity, providing the foundation for specific ad functional...
Serves as a base handler for ad unit operations, abstracting common functionality for different types...
void Rebind()
Rebinds the button to the currently preferred UI Toolkit source. This method is intended for explicit...
virtual void OnEnable()
Resolves the UI Toolkit Button from UIDocument or PanelRenderer and subscribes to its click event....
void OnButtonClick()
Handles button click:
virtual void OnDisable()
Unsubscribes from the click event and clears the cached button. Safe to call even if the button was n...
Binds a UI Toolkit Button (by name) to ad unit actions. Works with a UIDocument or a PanelRenderer,...
Provides a mechanism for hiding advertisements. This interface should be implemented by ads that can ...