#define IRONSOURCE
#if IRONSOURCE
using System;
using UnityEngine;
using DG.Tweening;
namespace LazyCoder
{
public class AdsIronSource
{
static readonly float s_reloadAdsInterval = 5f;
static readonly int s_reloadAdsMaxAttemp = 5;
static readonly IronSourceBannerSize s_bannerSize = IronSourceBannerSize.SMART;
static readonly IronSourceBannerPosition s_bannerPosition = IronSourceBannerPosition.BOTTOM;
#if UNITY_ANDROID
static readonly string ID = "1ad3ce98d";
#else
static readonly string ID = "1ad3d2315";
#endif
Tween _interstitialReloadTween;
Tween _bannerReloadTween;
Tween _rewardedTween;
int _interstitialReloadCount;
int _bannerReloadCount;
bool _videoRewarded = false;
Action<bool> _onVideoRewarded;
Action _onInterstitialClosed;
#region Core
public AdsIronSource()
{
IronSource.Agent.init(ID);
IronSource.Agent.validateIntegration();
IronSource.Agent.loadBanner(s_bannerSize, s_bannerPosition);
IronSource.Agent.loadInterstitial();
IronSourceRewardedVideoEvents.onAdRewardedEvent += IronSourceRewardedVideoEvents_OnAdRewardedEvent;
IronSourceRewardedVideoEvents.onAdClosedEvent += IronSourceRewardedVideoEvents_OnAdClosedEvent;
IronSourceRewardedVideoEvents.onAdShowFailedEvent += IronSourceRewardedVideoEvents_OnAdShowFailedEvent;
IronSourceInterstitialEvents.onAdClosedEvent += IronSourceInterstitialEvents_OnAdClosedEvent;
IronSourceInterstitialEvents.onAdShowFailedEvent += IronSourceInterstitialEvents_OnAdShowFailedEvent;
IronSourceInterstitialEvents.onAdLoadFailedEvent += IronSourceInterstitialEvents_OnAdLoadFailedEvent;
IronSourceInterstitialEvents.onAdReadyEvent += IronSourceInterstitialEvents_OnAdReadyEvent;
IronSourceBannerEvents.onAdLoadFailedEvent += IronSourceBannerEvents_OnAdLoadFailedEvent;
IronSourceBannerEvents.onAdLoadedEvent += IronSourceBannerEvents_OnAdLoadedEvent;
MonoCallback.Instance.ApplicationOnPause += MonoCallback_OnGamePaused;
}
void MonoCallback_OnGamePaused(bool paused)
{
IronSource.Agent.onApplicationPause(paused);
}
#endregion
#region Interstitial
private void IronSourceInterstitialEvents_OnAdReadyEvent(IronSourceAdInfo obj)
{
Debug.Log("Interstitial loaded");
_interstitialReloadCount = 0;
}
private void IronSourceInterstitialEvents_OnAdLoadFailedEvent(IronSourceError obj)
{
Debug.LogFormat("Interstitial failed to load: {0}", obj.getDescription());
if (_interstitialReloadCount > s_reloadAdsMaxAttemp)
return;
_interstitialReloadCount++;
_interstitialReloadTween?.Kill();
_interstitialReloadTween = DOVirtual.DelayedCall(s_reloadAdsInterval * _interstitialReloadCount, () =>
{
if (!IronSource.Agent.isInterstitialReady())
IronSource.Agent.loadInterstitial();
});
}
private void IronSourceInterstitialEvents_OnAdShowFailedEvent(IronSourceError error, IronSourceAdInfo info)
{
_onInterstitialClosed?.Invoke();
Debug.LogFormat("Interstitial failed to show: {0}", error.getDescription());
}
private void IronSourceInterstitialEvents_OnAdClosedEvent(IronSourceAdInfo obj)
{
_onInterstitialClosed?.Invoke();
IronSource.Agent.loadInterstitial();
}
#endregion
#region Rewarded
private void IronSourceRewardedVideoEvents_OnAdClosedEvent(IronSourceAdInfo obj)
{
// Because the OnRewardedVideoAdRewardedEvent and OnRewardedVideoAdClosedEvent are asynchronous
_rewardedTween?.Kill();
_rewardedTween = DOVirtual.DelayedCall(0.5f, () => { _onVideoRewarded?.Invoke(_videoRewarded); });
}
void IronSourceRewardedVideoEvents_OnAdRewardedEvent(IronSourcePlacement obj, IronSourceAdInfo info)
{
_videoRewarded = true;
}
private void IronSourceRewardedVideoEvents_OnAdShowFailedEvent(IronSourceError error, IronSourceAdInfo info)
{
_onVideoRewarded?.Invoke(_videoRewarded);
Debug.LogFormat(error.getDescription());
}
#endregion
#region Banner
private void IronSourceBannerEvents_OnAdLoadedEvent(IronSourceAdInfo obj)
{
}
private void IronSourceBannerEvents_OnAdLoadFailedEvent(IronSourceError obj)
{
Debug.LogFormat("Banner failed to load: {0}", obj.getDescription());
if (_bannerReloadCount > s_reloadAdsMaxAttemp)
return;
_bannerReloadCount++;
_bannerReloadTween?.Kill();
_bannerReloadTween = DOVirtual.DelayedCall(s_reloadAdsInterval * _interstitialReloadCount, () =>
{
IronSource.Agent.loadBanner(s_bannerSize, s_bannerPosition);
});
}
#endregion
#region Public
public void ShowInterstitial(Action onClosed)
{
if (!IronSource.Agent.isInterstitialReady())
{
onClosed?.Invoke();
}
else
{
_onInterstitialClosed = onClosed;
IronSource.Agent.showInterstitial();
}
}
public void ShowRewarded(Action<bool> onReward)
{
if (!IronSource.Agent.isRewardedVideoAvailable())
{
onReward?.Invoke(false);
}
else
{
_videoRewarded = false;
_onVideoRewarded = onReward;
IronSource.Agent.showRewardedVideo();
}
}
public void ShowBanner()
{
IronSource.Agent.displayBanner();
}
public void HideBanner()
{
IronSource.Agent.hideBanner();
}
public bool IsRewardedReady()
{
return IronSource.Agent.isRewardedVideoAvailable();
}
public bool IsInterstitialReady()
{
return IronSource.Agent.isInterstitialReady();
}
public Vector2 GetBannerSize()
{
return Vector2.zero;
}
#endregion
/*
static Vector2 GetBannerSizePixel()
{
#if UNITY_IOS
return (Vector2)GetBannerSize();
#elif UNITY_ANDROID && !UNITY_EDITOR
Vector2 bannerSize = GetBannerSize();
return new Vector2(bannerSize.x * (DisplayMetricsAndroid.DensityDPI / 160f), bannerSize.y * (DisplayMetricsAndroid.DensityDPI / 160f));
#else
return Vector2.zero;
#endif
}
static Vector2Int GetBannerSize()
{
#if UNITY_IOS
if (UnityEngine.iOS.Device.generation.ToString().IndexOf("iPad") > -1)
return new Vector2Int(728, 90);
else
return new Vector2Int(320, 50);
#elif UNITY_ANDROID && !UNITY_EDITOR
float actualHeight = Screen.height / DisplayMetricsAndroid.DensityDPI;
float dp = actualHeight * 160f;
if (dp > 720f)
return new Vector2Int(728, 90);
else
return new Vector2Int(320, 50);
#else
return Vector2Int.zero;
#endif
}
*/
}
}
#endif