Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Kiloo.Common.Ads;
- using Kiloo.Common.Utility;
- using Kiloo.Monetization.AdColonyPlugin;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Object = UnityEngine.Object;
- namespace Kiloo.Monetization
- {
- public class AdColonyAdProvider : AbstractAdProvider
- {
- public const string ID = "adcolony";
- private string _showLocation;
- private bool _addedListeners;
- private MonoBehaviour _coroutineRunner;
- List<string> _cachedLocationIds = new List<string>();
- bool _receivedReward;
- public override string id
- {
- get { return ID; }
- }
- public override bool supportsMulticaching
- {
- get { return false; }
- }
- protected override bool HasCachedVideo(string cacheID)
- {
- return _cachedLocationIds.Contains(cacheID);
- }
- protected override void OnInitialize(AdProviderConfiguration configuration)
- {
- if (!_addedListeners)
- {
- // Utku: Do not use the event below as it can be triggered AFTER the video has played on Android.
- // AdColony.onAdStarted += AdStarted;
- /*
- AdColony.onV4VCReward += success =>
- {
- // Log.Info("V4VC reward: " + success);
- _receivedReward = success;
- };
- AdColony.onAdAttemptFinished += shown =>
- {
- if (_receivedReward)
- {
- // Log.Info("Finished watching video");
- _receivedReward = false;
- eventInvoker.FinishedWatchingVideo(id, _showLocation);
- }
- // Log.Info("Ad dismissed");
- eventInvoker.DismissedAd(AdType.Video, id, _showLocation);
- };*/
- _coroutineRunner = new GameObject("AdColonyCoroutineRunner").AddComponent<MonoBehaviour>();
- Object.DontDestroyOnLoad(_coroutineRunner.gameObject);
- _addedListeners = true;
- }
- try
- {
- AdColony.Configure(configuration.appID, configuration.zoneIDs);
- FinishIntialization(true);
- }
- catch (Exception e)
- {
- Debug.LogError(e.Message);
- FinishIntialization(false);
- }
- }
- protected override void CacheVideo(string adLocationID, string cacheID)
- {
- if (!_cachedLocationIds.Contains(cacheID))
- {
- _coroutineRunner.StartCoroutine(CheckCacheState(adLocationID, cacheID));
- }
- else
- {
- eventInvoker.CachedAd(AdType.Video, id, adLocationID);
- }
- }
- protected override void ShowVideo(string adLocationID, string cacheID)
- {
- if (_cachedLocationIds.Contains(cacheID))
- {
- _cachedLocationIds.Remove(cacheID);
- }
- _showLocation = adLocationID;
- AdStarted();
- //AdColony.PlayVideoAdForZoneWithPopups(cacheID, false, false);
- }
- private IEnumerator CheckCacheState(string adLocationID, string cacheID)
- {
- eventInvoker.StartedCachingAd(AdType.Video, id, adLocationID);
- /*
- while (!AdColony.IsVirtualCurrencyAwardAvailable(cacheID))
- {
- yield return Wait.For(1f).UnscaledSeconds().StartCoroutine();
- }*/
- _cachedLocationIds.Add(cacheID);
- eventInvoker.CachedAd(AdType.Video, id, adLocationID);
- yield return null;
- }
- void AdStarted()
- {
- // Log.Info ("Ad started");
- _receivedReward = false;
- eventInvoker.ShowedAd (AdType.Video, id, _showLocation);
- }
- protected override void OnCacheTimeout(AdType adType, string adLocationID)
- {
- // Log.Warn("Cache timeout");
- _coroutineRunner.StopAllCoroutines();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement