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
AdCallbackMessagesVerifier.cs
1using System.Collections.Generic;
2using UnityEngine;
3
4namespace SCG.SpaceAdMobile
5{
11 public abstract class AdCallbackMessagesVerifier : MonoBehaviour
12 {
13#if UNITY_EDITOR
14 // Prevent duplicate scheduling for the same instance.
15#if UNITY_6000_4_OR_NEWER
16 private static readonly HashSet<EntityId> pending = new();
17#else
18 private static readonly HashSet<int> pending = new();
19#endif
20
21 [SerializeField]
22 [Tooltip("Automatically validates and adjusts the " + nameof(MessageOptions) +
23 " based on the selected " + nameof(ValidateType) + " policy.")]
24 protected MessageValidateType ValidateType = MessageValidateType.Full;
25
26 protected virtual void OnValidate()
27 {
28 // Do not run in playmode; ignore prefab assets; ignore objects not in a loaded scene.
29 if (!ValidateScript()) return;
30
31 // Schedule a safe, editor-ready validation by instance id.
32#if UNITY_6000_4_OR_NEWER
33 ScheduleAfterEditorReady(GetEntityId());
34#else
35 ScheduleAfterEditorReady(GetInstanceID());
36#endif
37 }
38
43 protected virtual void ValidateSafe()
44 {
45 if (!ValidateScript()) return;
46
47 if (!TryGetComponent<AdUnitBase>(out var adUnit) || !adUnit)
48 {
49 Debug.LogWarning($"{nameof(AdUnitBase)} component not found on the GameObject '{name}'.", this);
50 return;
51 }
52
53 if (ValidateType == MessageValidateType.None) return;
54
55 ValidateMessageOptions(adUnit);
56 }
57
58 protected virtual bool ValidateScript()
59 {
60 if (!this || !gameObject) return false;
61 var scn = gameObject.scene;
62 return scn.IsValid() && scn.isLoaded && !Application.isPlaying;
63 }
64
65#if UNITY_6000_4_OR_NEWER
66 private static void ScheduleAfterEditorReady(EntityId entityId)
67 {
68 if (!pending.Add(entityId)) return;
69
70 UnityEditor.EditorApplication.delayCall += () =>
71 {
72 if (UnityEditor.EditorApplication.isUpdating || UnityEditor.EditorApplication.isCompiling)
73 {
74 pending.Remove(entityId);
75 ScheduleAfterEditorReady(entityId);
76 return;
77 }
78
79 pending.Remove(entityId);
80
81 var obj = UnityEditor.EditorUtility.EntityIdToObject(entityId) as AdCallbackMessagesVerifier;
82 if (!obj || !obj.gameObject) return;
83
84 var scn = obj.gameObject.scene;
85 if (!scn.IsValid() || !scn.isLoaded) return;
86
87 obj.ValidateSafe();
88 };
89 }
90#else
91 private static void ScheduleAfterEditorReady(int instanceId)
92 {
93 // throttle duplicates
94 if (!pending.Add(instanceId)) return;
95
96 UnityEditor.EditorApplication.delayCall += () =>
97 {
98 if (UnityEditor.EditorApplication.isUpdating || UnityEditor.EditorApplication.isCompiling)
99 {
100 pending.Remove(instanceId);
101 ScheduleAfterEditorReady(instanceId);
102 return;
103 }
104
105 pending.Remove(instanceId);
106
107 var obj = UnityEditor.EditorUtility.InstanceIDToObject(instanceId) as AdCallbackMessagesVerifier;
108 if (!obj || !obj.gameObject) return;
109
110 var scn = obj.gameObject.scene;
111 if (!scn.IsValid() || !scn.isLoaded) return;
112
113 obj.ValidateSafe();
114 };
115 }
116#endif
117
122 protected virtual void ValidateMessageOptions(AdUnitBase adUnit)
123 {
124 adUnit.MessageOptions.AdPaid = CheckByType<IAdPaidMessage>(adUnit.MessageOptions.AdPaid);
125 adUnit.MessageOptions.AdImpressionRecorded = CheckByType<IAdImpressionRecordedMessage>(adUnit.MessageOptions.AdImpressionRecorded);
126 adUnit.MessageOptions.AdLoaded = CheckByType<IAdLoadedMessage>(adUnit.MessageOptions.AdLoaded);
127 adUnit.MessageOptions.AdLoadFailed = CheckByType<IAdLoadFailedMessage>(adUnit.MessageOptions.AdLoadFailed);
128 adUnit.MessageOptions.AdClicked = CheckByType<IAdClickedMessage>(adUnit.MessageOptions.AdClicked);
129 adUnit.MessageOptions.AdShown = CheckByType<IAdShownMessage>(adUnit.MessageOptions.AdShown);
130 adUnit.MessageOptions.AdClosed = CheckByType<IAdClosedMessage>(adUnit.MessageOptions.AdClosed);
131 adUnit.MessageOptions.AdContentFailed = CheckByType<IAdContentFailedMessage>(adUnit.MessageOptions.AdContentFailed);
132 adUnit.MessageOptions.AdReward = CheckByType<IAdRewardMessage>(adUnit.MessageOptions.AdReward);
133 }
134
141 protected bool CheckByType<T>(bool value)
142 => ValidateType == MessageValidateType.Full ? this is T : this is T || value;
143#endif
144 }
145
150 {
151 [Tooltip("Validates and adjusts the message options fully, setting them either to true" + "\n" +
152 "or false based on compatibility with the verifier type.")]
153 Full,
154 [Tooltip("Enables the message options that are compatible with the verifier type, without disabling any options.")]
155 Enable,
156 [Tooltip("Performs no automatic adjustments on the message options.")]
157 None
158 }
159}
Facilitates automatic validation and configuration of ad callback message options within Unity Editor...
MessageOptions MessageOptions
Options for enabling Unity SendMessage methods.
Definition AdUnitBase.cs:21
Base class for ad units. This class defines the abstract and virtual properties and methods that are ...
Definition AdUnitBase.cs:9
bool AdImpressionRecorded
Enables the callback for when an ad impression is recorded.
bool AdClosed
Enables the callback for when an ad is closed by the user.
bool AdPaid
Enables the callback for when an ad generates revenue. The option is available only for Google AdMob.
bool AdShown
Enables the callback for when an ad is shown.
bool AdReward
Enables the callback for when a reward is earned from interacting with an ad.
bool AdLoadFailed
Enables the callback for when an ad fails to load, with an error parameter.
bool AdClicked
Enables the callback for when an ad is clicked.
bool AdLoaded
Enables the callback for when an ad successfully loads.
bool AdContentFailed
Enables the callback for when an ad fails to present content.
Defines options for enabling message callbacks related to ad events. These options allow you to subsc...
MessageValidateType
Represents the validation strategy for adjusting ad callback message options within Unity Editor.