public void ShowAdvertisement(AdsTypes type, AdsPositions position = AdsPositions.Top, Action onAdShown = null, Action onAdClicked = null, Action <object> onRewarded = null, Action <string> onFail = null, bool forceAds = false, Dictionary <string, object> extraOptions = null, float invokeDelay = 0, Action onSkipped = null) { if (!LocalDataManager.Instance.IsConnectedToInternet) { Debug.Log("Show ads ignored, as there is not internet connectivity."); return; } Debug.LogFormat("Ads Manager::: -> Premium User: {0} \n-> Force Ads: {1}", _isPremiumUser, forceAds); //debug if (BaseSettings.Instance.ForceAds) { Debug.Log("Debug force Ads is true, ignoring premium status."); } if (!BaseSettings.Instance.ForceAds && !forceAds) //game setting->forceAds is use for debug { if (_isPremiumUser) { return; } } Debug.Log("Showing Ad: Type- " + type + " Extra options: " + JsonConvert.SerializeObject(extraOptions) + " with delay: " + invokeDelay); StartCoroutine(ShowAds(type, position, onAdShown, onAdClicked, onRewarded, onFail, extraOptions, invokeDelay, onSkipped)); }
public void ShowAd(AdsTypes type, float duration = -1, AdsPositions position = AdsPositions.Bottom, Action <object> onRewarded = null, bool skippable = false, Action onSkipped = null) { this.Activate(); switch (type) { case AdsTypes.Banner: { this.InvokeAfter(() => CloseButton.interactable = true, 0.1f, ignoreUnityTimeScale: true); switch (position) { case AdsPositions.Top: { ParentLayout.childAlignment = TextAnchor.UpperCenter; } break; case AdsPositions.Bottom: { ParentLayout.childAlignment = TextAnchor.LowerCenter; } break; } } break; case AdsTypes.Interstitial: case AdsTypes.RewardedVideo: { _remainingTimer = duration; _onRewarded = onRewarded; _onSkipped = onSkipped; if (skippable) { this.InvokeAfter(() => CloseButton.interactable = true, 0.1f, ignoreUnityTimeScale: true); } this.InvokeAfter(() => CloseButton.interactable = true, duration, (elapsedTime) => { _remainingTimer = duration - elapsedTime; TimerImage.fillAmount = elapsedTime / duration; }, 0.1f, ignoreUnityTimeScale: true); Time.timeScale = 0; } break; } _tween = DemoText.DOPunchRotation(Vector3.one, 0.3f).SetEase(Ease.InOutElastic).SetLoops(-1, LoopType.Incremental).SetUpdate(true); OnAdShown.SafeInvoke(); }
protected virtual IEnumerator ShowAds(AdsTypes type, AdsPositions position = AdsPositions.Top, Action onAdShown = null, Action onAdClicked = null, Action <object> onRewarded = null, Action <string> onFail = null, Dictionary <string, object> extraOptions = null, float invokeDelay = 0, Action onSkipped = null) { yield return(new WaitForSeconds(invokeDelay)); //early exit when there is init error or if the user is a premium user if (_errorInInitialization) { yield break; } if (_systemsWithAdsAvailable.Count < 1) { Debug.LogWarning("Error: No Ads Available"); onFail.SafeInvoke("No Ads Available"); yield break; } //sort the system that has ads based on priority _systemsWithAdsAvailable.Sort((x, y) => x.Priority.CompareTo(y.Priority)); var firstPriority = _systemsWithAdsAvailable.Find(x => x.IsAdsAvailable(type, extraOptions)); if (firstPriority == null) { Debug.LogWarning("Ad type of : " + type + " is not available in all systems."); yield break; } if (!firstPriority.IsApiInitialized() || !firstPriority.IsAdsAvailable(type, extraOptions)) { Debug.LogWarningFormat("System Initialized: {0}.\nAds available in the system: {1}", firstPriority.IsApiInitialized(), firstPriority.IsAdsAvailable(type, extraOptions)); yield break; } var adsInterface = firstPriority as IAdsInterface; if (adsInterface != null) { //adding the set status method to the on ad shown action onAdShown += () => { firstPriority.SetAdServedStatus(type, true); OnAdsShown.SafeInvoke(type, position); }; onRewarded += obj => { OnRewardVideoCompleted.SafeInvoke(); }; adsInterface.ShowAd(type, position, onAdShown, onAdClicked, onRewarded, extraOptions, onSkipped); _engagedAdvertisers.AddUnique(firstPriority); } }
public void RequestAdvertisement(AdsTypes type, Action <int> onSuccess = null, Action onFail = null, AdsPositions position = AdsPositions.Top, float timeOutDuration = 5, Dictionary <string, object> extraOptions = null) { if (!LocalDataManager.Instance.IsConnectedToInternet) { Debug.Log("Request ads ignored, as there is not internet connectivity."); onFail.SafeInvoke(); return; //early return if there is not internet connectivity } Debug.Log("Requesting Ad: Type- " + type + " Extra options: " + JsonConvert.SerializeObject(extraOptions)); StartCoroutine(RequestAds(type, onSuccess, onFail, position, timeOutDuration, extraOptions)); }
public void ShowAd(AdsTypes type, AdsPositions position, Action onAdShown, Action onAdClicked, Action <object> onRewarded, Dictionary <string, object> extraOptions = null, Action onSkipped = null) { if (!IsConsentGiven || !Isinitialized) { Debug.LogWarning("Consent: " + IsConsentGiven + ", Initialized: " + Isinitialized); return; } switch (type) { case AdsTypes.Banner: { if (_isBannerReady) { _demoAdsUi.BannerAd.ShowAd(AdsTypes.Banner, position: position); } } break; case AdsTypes.Interstitial: { if (_isInterstitialReady) { _demoAdsUi.InterstitialAd.ShowAd(AdsTypes.Interstitial, 5, skippable: extraOptions != null && (bool)extraOptions.SafeRetrieve("skippable"), onSkipped: onSkipped); } } break; case AdsTypes.RewardedVideo: { if (_isRewardVideoReady) { _demoAdsUi.RewardingVideoAd.ShowAd(AdsTypes.RewardedVideo, Random.Range(6, 10), onRewarded: onRewarded, skippable: extraOptions != null && (bool)extraOptions.SafeRetrieve("skippable"), onSkipped: onSkipped); } } break; default: { Debug.Log("Invalid ad type!!!"); } return; } onAdShown.SafeInvoke(); }
public void RequestAd(AdsTypes type, Action onSuccess, Action onFail, AdsPositions position, Dictionary <string, object> extraOptions = null) { if (!IsConsentGiven || !Isinitialized) { Debug.LogWarning("Consent: " + IsConsentGiven + ", Initialized: " + Isinitialized); return; } AssignedActions.CreateActions(type, onSuccess, Actions.ActionType.Success, Actions.EventType.Request); switch (type) { case AdsTypes.Banner: { OnBannerAdCached(); } break; case AdsTypes.Interstitial: { OnInterstitialAdCached(); } break; case AdsTypes.RewardedVideo: { OnRewardVideoAdCached(true); } break; default: { Debug.Log("Invalid ad type!!!"); return; } } }
protected virtual IEnumerator RequestAds(AdsTypes type, Action <int> onSuccess = null, Action onFail = null, AdsPositions position = AdsPositions.Top, float timeOutDuration = 8, Dictionary <string, object> extraOptions = null) { bool timeOut = false; //early exit when there is init error if (_errorInInitialization) { onFail.SafeInvoke(); yield return(new WaitForEndOfFrame()); _isRequestingAds = false; yield break; } _isRequestingAds = true; var requestCount = 0; try { _availableAdsSystem.ForEach(x => { var adsInterface = x as IAdsInterface; if (adsInterface != null) { adsInterface.RequestAd(type, () => { _systemsWithAdsAvailable.AddUnique(x); requestCount++; }, () => { requestCount++; }, position, extraOptions); } }); } catch (Exception e) { Debug.Log("Ad Request error: " + e); timeOut = true; } this.InvokeAfter(() => timeOut = true, timeOutDuration); //wait till all the request are processed and callbacks received yield return(new WaitUntil(() => requestCount.Equals(_availableAdsSystem.Count) || timeOut)); // _systemsWithAdsAvailable = _systemsWithAdsAvailable.Distinct().ToList(); var adsFound = _systemsWithAdsAvailable.Count(x => x.IsAdsAvailable(type, extraOptions)); if (adsFound > 0) { onSuccess.SafeInvoke(adsFound); } else { onFail.SafeInvoke(); } yield return(new WaitForEndOfFrame()); _isRequestingAds = false; }