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
AdControllerBase.cs
1using System.Collections.Generic;
2using System.Threading.Tasks;
3using UnityEngine;
4
5namespace SCG.SpaceAdMobile
6{
11 public abstract class AdControllerBase : IdentifierUtility
12 {
16 public abstract AdUnitBehaviourBase Ad { get; }
17
22 protected virtual int LoadAsyncPollingDelayMilliseconds => 500;
23
25 public abstract bool Load();
26
33 public virtual async Task<bool> LoadAsync()
34 {
35 if (!Load())
36 return false;
37
38 while (!Ad.CanShowAd())
39 await Task.Delay(LoadAsyncPollingDelayMilliseconds);
40
41 return true;
42 }
43
45 public abstract void Show();
46
48 public abstract void Unload();
49 }
50
56 public abstract class AdControllerBase<T> : AdControllerBase where T : AdUnitBehaviourBase
57 {
59 public override AdUnitBehaviourBase Ad => AdUnit;
60
64 [field: SerializeField, Tooltip("Attach the ad unit component that this controller should manage.")]
65 protected T AdUnit { get; private set; }
66
70 private static readonly Dictionary<string, T> adInstances = new();
71
77 public T this[string id] => adInstances.GetValueOrDefault(id);
78
79#pragma warning disable IDE0031
81 public override bool Load() => AdUnit != null && AdUnit.Load();
82
84 public override void Show()
85 {
86 if (AdUnit != null)
87 AdUnit.Show();
88 }
89
91 public override void Unload()
92 {
93 if (AdUnit != null)
94 AdUnit.Unload();
95 }
96#pragma warning restore IDE0031
97
101 protected virtual void Awake()
102 {
103 if (AdUnit != null)
104 {
105 if (!AdUnit.TryGetComponent<UndestroyableUtility>(out var undestroyable))
106 {
107 throw new UnityException($"Ad object must include a component of type {nameof(UndestroyableUtility)} to ensure it is not inadvertently destroyed.");
108 }
109
110 if (!undestroyable.IsDestroyed)
111 {
112 adInstances.TryAdd(Id, AdUnit);
113 }
114 else
115 {
116 AdUnit = null;
117 }
118 }
119
120 AdUnit ??= this[Id];
121
122 if (AdUnit == null)
123 {
124 throw new UnityException("Failed to assign an Ad object. Ensure the Ad object is properly initialized.");
125 }
126 }
127
132 protected virtual void OnApplicationQuit()
133 {
134 Unload();
135 adInstances.Clear();
136 }
137 }
138}
T AdUnit
The ad unit component attached to this controller. It controls the ad's behavior and lifecycle.
virtual void OnApplicationQuit()
Performs clean-up actions when the application quits, such as unloading ad instances and clearing the...
override AdUnitBehaviourBase Ad
virtual void Awake()
Initializes the component during Unity's Awake callback.
AdUnitBehaviourBase Ad
The ad unit component attached to this controller. It controls the ad's behavior and lifecycle.
virtual int LoadAsyncPollingDelayMilliseconds
Delay between fallback asynchronous load readiness checks, in milliseconds. Derived controllers can o...
virtual async Task< bool > LoadAsync()
Loads the controlled ad unit asynchronously. Default implementation starts a regular load and waits u...
Base class for controlling ad units within Unity, providing the foundation for specific ad functional...
Base MonoBehaviour class for ad units. This class defines the abstract and virtual properties and met...
virtual string Id
The unique identifier for the object instance. It is serialized and can be viewed and modified in the...
Provides a base class for objects requiring a unique identifier, using MonoBehaviour as its foundatio...
A utility component to make an object persist across different scenes in a Unity project....