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
AdButton.cs
1using UnityEngine;
2using UIButton = UnityEngine.UI.Button;
3
5{
11 [DisallowMultipleComponent]
12 [AddComponentMenu("SCG/SpaceAdMobile/" + nameof(AdButton))]
13 public class AdButton : 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("UGUI Button to bind; auto-filled in Editor via GetComponent.")]
39 [SerializeField] private UIButton button;
40
41 #endregion
42
43 #region Unity lifecycle
44
45#if UNITY_EDITOR
52 protected virtual void OnValidate()
53 {
54 unitController = unitController ? unitController : GetComponent<AdControllerBase>();
55 adUnit = adUnit ? adUnit : GetComponent<AdUnitHandlerBase>();
56 button = button ? button : GetComponent<UIButton>();
57 }
58#endif
59
64 protected virtual void Awake()
65 {
66 if (button != null)
67 button.onClick.AddListener(OnButtonClick);
68 }
69
74 protected virtual void OnDestroy()
75 {
76 if (button != null)
77 button.onClick.RemoveListener(OnButtonClick);
78 }
79
80 #endregion
81
82 #region Handlers
83
90 public void OnButtonClick()
91 {
92 if (adUnit == null && unitController != null)
93 adUnit = unitController.Ad as AdUnitHandlerBase;
94
95 if (adUnit == null)
96 throw new System.NullReferenceException(nameof(adUnit));
97
98 if (load) adUnit.Load();
99 if (show) adUnit.Show();
100
101 // ReSharper disable once SuspiciousTypeConversion.Global
102 if (hide) ((IConcealableAd)unitController)?.Hide();
103 }
104
105 #endregion
106 }
107}
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...
virtual void Awake()
Subscribes to the UGUI Button click to call OnButtonClick. Skips subscription if Button is missing.
Definition AdButton.cs:64
void OnButtonClick()
Handles button click:
Definition AdButton.cs:90
virtual void OnDestroy()
Unsubscribes from button click to avoid dangling listeners. Safe to call even if Button is null.
Definition AdButton.cs:74
Binds a standard UGUI Button to ad unit actions. Executes Load/Show/Hide on click depending on the to...
Definition AdButton.cs:14
Provides a mechanism for hiding advertisements. This interface should be implemented by ads that can ...