using System; using GameAnalyticsSDK; using UnityEngine; using UnityEngine.Advertisements; public enum AdType { Rewarded, Interstitial } public class UnityAdController : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener { public static UnityAdController Instance { get; private set; } private event Action OnInitializeComplete; private event Action OnShowAdComplete; [SerializeField] private BannerPosition bannerPosition = BannerPosition.BOTTOM_CENTER; [SerializeField] private string androidId; [SerializeField] private string iOSId; private bool _adsDisabled; private bool _isInitialized; private string _rewardedUnitId; private string _interstitialUnitId; private string _bannerUnitId; private void Awake() { if (Instance != null) return; Instance = this; } public void Initialize(Action onComplete) { OnInitializeComplete += onComplete; #if UNITY_EDITOR _adsDisabled = true; #endif if (!Advertisement.isSupported) { Debug.Log("Unity Ads are not supported on this platform!"); OnInitializeComplete?.Invoke(); OnInitializeComplete = null; return; } if (Advertisement.isInitialized) { Debug.Log("Ad System already Initialized!"); OnInitializeComplete?.Invoke(); OnInitializeComplete = null; return; } #if UNITY_IOS Advertisement.Initialize(iOSId, false, this); _rewardedUnitId = "Rewarded_iOS"; _interstitialUnitId = "Interstitial_iOS"; _bannerUnitId = "Banner_iOS"; #elif UNITY_ANDROID Advertisement.Initialize(androidId, false, this); _rewardedUnitId = "Rewarded_Android"; _interstitialUnitId = "Interstitial_Android"; _bannerUnitId = "Banner_Android"; #endif Advertisement.Banner.SetPosition(bannerPosition); } public void OnInitializationComplete() { Debug.Log("Unity Ads Initialized successfully!"); _isInitialized = true; OnInitializeComplete?.Invoke(); OnInitializeComplete = null; LoadAd(AdType.Rewarded); LoadAd(AdType.Interstitial); LoadBanner(); } private void LoadBanner() { var options = new BannerLoadOptions { loadCallback = BannerLoaded, errorCallback = BannerError }; AnalyticsSystem.SendAdEvent(GAAdAction.Request, GAAdType.Banner, _bannerUnitId); Advertisement.Banner.Load(_bannerUnitId, options); return; void BannerLoaded() { Debug.Log("Banner loaded"); AnalyticsSystem.SendAdEvent(GAAdAction.Loaded, GAAdType.Banner, _bannerUnitId); // Options needed ?!! // Hide on purchase // Load & Play again to refresh?!! // TODO: Rework to initialize IAP first and check for ad purchases then to skip AdSystem Init. if (_adsDisabled) return; var showOptions = new BannerOptions { // clickCallback = OnBannerClicked, // hideCallback = OnBannerHidden, showCallback = () => AnalyticsSystem.SendAdEvent(GAAdAction.Show, GAAdType.Banner, _bannerUnitId) }; Advertisement.Banner.Show(_bannerUnitId, showOptions); } void BannerError(string message) { Debug.LogWarning($"Banner Load Error: {message}"); AnalyticsSystem.SendAdEvent(GAAdAction.Undefined, GAAdType.Banner, _bannerUnitId); } } public void OnInitializationFailed(UnityAdsInitializationError error, string message) { Debug.Log($"Unity Ads Initialization error: {error.ToString()} - {message}"); OnInitializeComplete?.Invoke(); OnInitializeComplete = null; } private void LoadAd(AdType inType) { switch (inType) { case AdType.Rewarded: Advertisement.Load(_rewardedUnitId, this); break; case AdType.Interstitial: Advertisement.Load(_interstitialUnitId, this); break; default: Debug.Log($"Error: [{inType.ToString()}] does not exist!"); break; } } public static void PlayAd(AdType inType, Action onComplete = null) { if (Instance._adsDisabled || !Instance._isInitialized) { onComplete?.Invoke(); return; } Instance.OnShowAdComplete += () => { Instance.LoadAd(inType); onComplete?.Invoke(); }; switch (inType) { case AdType.Rewarded: Advertisement.Show(Instance._rewardedUnitId, Instance); break; case AdType.Interstitial: Advertisement.Show(Instance._interstitialUnitId, Instance); break; default: Debug.Log($"Error: [{inType.ToString()}] does not exist!"); return; } } public void OnUnityAdsAdLoaded(string placementId) { Debug.Log($"AD: [{placementId}] loaded successfully!"); AnalyticsSystem.SendAdEvent(GAAdAction.Loaded, GAAdType.Video, placementId); } public void OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message) => Debug.Log($"Error: [{placementId}] failure to load - {error.ToString()} - {message}"); public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message) { Debug.Log($"Error: [{placementId}] failure to show - {error.ToString()} - {message}"); Debug.Log("-- Invoking OnComplete Event anyways."); OnShowAdComplete?.Invoke(); OnShowAdComplete = null; AnalyticsSystem.SendAdEvent(GAAdAction.FailedShow, GAAdType.Video, placementId); } public void OnUnityAdsShowStart(string placementId) => Debug.Log($"AD: [{placementId}] started to show!"); public void OnUnityAdsShowClick(string placementId) => Debug.Log($"AD: [{placementId}] clicked to show"); public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState) { OnShowAdComplete?.Invoke(); OnShowAdComplete = null; AnalyticsSystem.SendAdEvent(GAAdAction.Show, GAAdType.Video, placementId); } public static void DisableAds() { Instance._adsDisabled = true; Advertisement.Banner.Hide(); } public static bool AreAdsDisabled() => Instance._adsDisabled; }