Created
April 13, 2023 15:33
-
-
Save ardaerbaharli/90ba4f75368bb1f5f8dc7d73debba391 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using Unity.Services.Core; | |
| using Unity.Services.Mediation; | |
| // using Analytics; | |
| using UnityEngine; | |
| using UnityEngine.Advertisements; | |
| namespace Ads | |
| { | |
| public class AdManager : MonoBehaviour | |
| { | |
| [SerializeField] string iOSGameId; | |
| [SerializeField] string androidGameId; | |
| [SerializeField] public bool ActivateAds; | |
| [SerializeField] private bool activateInterstitial; | |
| [SerializeField] private bool activateRewarded; | |
| [SerializeField] private bool activateBanner; | |
| [Space] [Header("Additional settings")] [SerializeField] | |
| private bool showInterstitialAfterXGames = true; | |
| [SerializeField] private int interstitialAfterXGames = 3; | |
| private int _interstitialCounter; | |
| private InterstitialAdController interstitialAdController; | |
| private BannerAdController bannerAdController; | |
| private RewardedAdsController rewardedAdsController; | |
| [Space] [Header("States")] public bool IsInterstitialReady; | |
| public bool IsRewardedReady; | |
| public bool IsBannerReady; | |
| private string _gameId; | |
| public bool AdsEnabled | |
| { | |
| get => ActivateAds && _adsEnabled; | |
| set => _adsEnabled = value; | |
| } | |
| private bool _adsEnabled; | |
| public static AdManager instance; | |
| private Action OnInterstitialFinishedAction; | |
| private Action OnRewardedAdFinishedAction; | |
| private Action OnRewardedAdUserEarnedRewardAction; | |
| private void Awake() | |
| { | |
| if (instance == null) | |
| { | |
| instance = this; | |
| DontDestroyOnLoad(gameObject); | |
| } | |
| else | |
| { | |
| Destroy(gameObject); | |
| return; | |
| } | |
| AdsEnabled = PlayerPrefs.GetInt("AdsEnabled", 1) == 1; | |
| if (!AdsEnabled) return; | |
| print("Activate ads: " + ActivateAds); | |
| _interstitialCounter = PlayerPrefs.GetInt("InterstitialCounter", 0); | |
| InitializeAds(); | |
| interstitialAdController = GetComponent<InterstitialAdController>(); | |
| rewardedAdsController = GetComponent<RewardedAdsController>(); | |
| bannerAdController = GetComponent<BannerAdController>(); | |
| } | |
| private async void InitializeAds() | |
| { | |
| #if UNITY_IOS | |
| _gameId = iOSGameId; | |
| #elif UNITY_ANDROID | |
| _gameId = androidGameId; | |
| #endif | |
| print("Initialize ads with game id: " + _gameId); | |
| try | |
| { | |
| Debug.Log("Initializing..."); | |
| await UnityServices.InitializeAsync(GetGameId()); | |
| Debug.Log("Initialized!"); | |
| InitializationComplete(); | |
| } | |
| catch (Exception e) | |
| { | |
| InitializationFailed(e); | |
| } | |
| } | |
| private InitializationOptions GetGameId() | |
| { | |
| var initializationOptions = new InitializationOptions(); | |
| #if UNITY_IOS | |
| if (!string.IsNullOrEmpty(iosGameId)) | |
| { | |
| initializationOptions.SetGameId(iosGameId); | |
| } | |
| #elif UNITY_ANDROID | |
| if (!string.IsNullOrEmpty(androidGameId)) | |
| { | |
| initializationOptions.SetGameId(androidGameId); | |
| } | |
| #endif | |
| return initializationOptions; | |
| } | |
| private void InitializationComplete() | |
| { | |
| MediationService.Instance.ImpressionEventPublisher.OnImpression += ImpressionEvent; | |
| print("Unity ads initialized"); | |
| if (activateInterstitial) | |
| { | |
| interstitialAdController.Initialize(); | |
| interstitialAdController.OnAdLoaded += OnInterstitialAdLoaded; | |
| interstitialAdController.OnAdFinished += OnInterstitialAdFinished; | |
| interstitialAdController.OnFailed += OnInterstitialAdFailed; | |
| } | |
| if (activateRewarded) | |
| { | |
| rewardedAdsController.Initialize(); | |
| rewardedAdsController.OnAdLoaded += OnRewardedAdLoaded; | |
| rewardedAdsController.OnAdFinished += OnRewardedAdFinished; | |
| rewardedAdsController.OnFailed += OnRewardedAdFailed; | |
| rewardedAdsController.OnRewarded += OnRewardedAdUserEarnedReward; | |
| } | |
| if (activateBanner) | |
| { | |
| bannerAdController.Initialize(); | |
| bannerAdController.OnAdLoaded += OnBannerAdLoaded; | |
| } | |
| } | |
| private void InitializationFailed(Exception error) | |
| { | |
| var initializationError = SdkInitializationError.Unknown; | |
| if (error is InitializeFailedException initializeFailedException) | |
| { | |
| initializationError = initializeFailedException.initializationError; | |
| } | |
| Debug.Log($"Initialization Failed: {initializationError}:{error.Message}"); | |
| } | |
| void ImpressionEvent(object sender, ImpressionEventArgs args) | |
| { | |
| var impressionData = args.ImpressionData != null ? JsonUtility.ToJson(args.ImpressionData, true) : "null"; | |
| Debug.Log($"Impression event from ad unit id {args.AdUnitId} : {impressionData}"); | |
| } | |
| public void ShowInterstitialAd(Action interstitialAdFinishedAction = null) | |
| { | |
| _interstitialCounter++; | |
| PlayerPrefs.SetInt("InterstitialCounter", _interstitialCounter); | |
| if (showInterstitialAfterXGames && _interstitialCounter < interstitialAfterXGames) return; | |
| print("is interstitial ad ready: " + IsInterstitialReady); | |
| OnInterstitialFinishedAction = interstitialAdFinishedAction; | |
| if (IsInterstitialReady) | |
| { | |
| IsInterstitialReady = false; | |
| interstitialAdController.ShowInterstitial(); | |
| } | |
| else | |
| interstitialAdController.LoadAd(); | |
| } | |
| public void ShowRewardedAd(Action rewardedAdFinishedAction = null) | |
| { | |
| print("is rewarded ad ready: " + IsRewardedReady); | |
| OnRewardedAdFinishedAction = rewardedAdFinishedAction; | |
| if (IsRewardedReady) | |
| { | |
| IsRewardedReady = false; | |
| rewardedAdsController.ShowRewarded(); | |
| } | |
| else | |
| rewardedAdsController.LoadAd(); | |
| } | |
| private void OnBannerAdLoaded() | |
| { | |
| IsBannerReady = true; | |
| } | |
| private void OnInterstitialAdLoaded() | |
| { | |
| IsInterstitialReady = true; | |
| } | |
| private void OnRewardedAdLoaded() | |
| { | |
| IsRewardedReady = true; | |
| } | |
| private void OnInterstitialAdFailed() | |
| { | |
| OnInterstitialFinishedAction?.Invoke(); | |
| OnInterstitialFinishedAction = null; | |
| } | |
| private void OnInterstitialAdFinished() | |
| { | |
| OnInterstitialFinishedAction?.Invoke(); | |
| OnInterstitialFinishedAction = null; | |
| } | |
| private void OnRewardedAdFailed() | |
| { | |
| OnRewardedAdFinishedAction?.Invoke(); | |
| OnInterstitialFinishedAction = null; | |
| } | |
| private void OnRewardedAdFinished() | |
| { | |
| OnRewardedAdFinishedAction?.Invoke(); | |
| OnInterstitialFinishedAction = null; | |
| } | |
| private void OnRewardedAdUserEarnedReward() | |
| { | |
| OnRewardedAdUserEarnedRewardAction?.Invoke(); | |
| OnRewardedAdUserEarnedRewardAction = null; | |
| } | |
| public void RemoveAds() | |
| { | |
| PlayerPrefs.SetInt("AdsEnabled", 0); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using Unity.Services.Core; | |
| using Unity.Services.Mediation; | |
| using UnityEngine; | |
| namespace Ads | |
| { | |
| public class InterstitialAdController : MonoBehaviour | |
| { | |
| [SerializeField] private string androidAdUnitId; | |
| [SerializeField] private string iosAdUnitId; | |
| private IInterstitialAd m_InterstitialAd; | |
| public Action OnAdLoaded; | |
| public Action OnAdFinished; | |
| public Action OnFailed; | |
| public void Initialize() | |
| { | |
| switch (Application.platform) | |
| { | |
| case RuntimePlatform.Android: | |
| m_InterstitialAd = MediationService.Instance.CreateInterstitialAd(androidAdUnitId); | |
| break; | |
| case RuntimePlatform.IPhonePlayer: | |
| m_InterstitialAd = MediationService.Instance.CreateInterstitialAd(iosAdUnitId); | |
| break; | |
| default: | |
| Debug.LogWarning("Mediation service is not available for this platform:" + Application.platform); | |
| return; | |
| } | |
| // Load Events | |
| m_InterstitialAd.OnLoaded += AdLoaded; | |
| m_InterstitialAd.OnFailedLoad += AdFailedLoad; | |
| // Show Events | |
| m_InterstitialAd.OnClosed += AdClosed; | |
| Debug.Log("Initialized On Start! Loading Ad..."); | |
| LoadAd(); | |
| } | |
| private void OnDestroy() | |
| { | |
| m_InterstitialAd?.Dispose(); | |
| } | |
| public async void ShowInterstitial() | |
| { | |
| if (m_InterstitialAd?.AdState == AdState.Loaded) | |
| { | |
| try | |
| { | |
| var showOptions = new InterstitialAdShowOptions {AutoReload = true}; | |
| await m_InterstitialAd.ShowAsync(showOptions); | |
| Debug.Log("Interstitial Shown!"); | |
| } | |
| catch (ShowFailedException e) | |
| { | |
| Debug.Log($"Interstitial failed to show : {e.Message}"); | |
| } | |
| } | |
| } | |
| public async void LoadAd() | |
| { | |
| try | |
| { | |
| await m_InterstitialAd.LoadAsync(); | |
| } | |
| catch (LoadFailedException) | |
| { | |
| // We will handle the failure in the OnFailedLoad callback | |
| } | |
| } | |
| private void AdClosed(object sender, EventArgs args) | |
| { | |
| Debug.Log("Interstitial Closed! Loading Ad..."); | |
| OnAdFinished?.Invoke(); | |
| } | |
| private void AdLoaded(object sender, EventArgs e) | |
| { | |
| Debug.Log("Ad loaded"); | |
| OnAdLoaded?.Invoke(); | |
| } | |
| private void AdFailedLoad(object sender, LoadErrorEventArgs e) | |
| { | |
| Debug.Log("Failed to load ad"); | |
| Debug.Log(e.Message); | |
| OnFailed?.Invoke(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using Unity.Services.Mediation; | |
| using UnityEngine; | |
| namespace Ads | |
| { | |
| public class RewardedAdsController : MonoBehaviour | |
| { | |
| [SerializeField] private string androidAdUnitId; | |
| [SerializeField] private string iosAdUnitId; | |
| public Action OnAdLoaded; | |
| public Action OnAdFinished; | |
| public Action OnFailed; | |
| public Action OnRewarded; | |
| private IRewardedAd m_RewardedAd; | |
| public void Initialize() | |
| { | |
| switch (Application.platform) | |
| { | |
| case RuntimePlatform.Android: | |
| m_RewardedAd = MediationService.Instance.CreateRewardedAd(androidAdUnitId); | |
| break; | |
| case RuntimePlatform.IPhonePlayer: | |
| m_RewardedAd = MediationService.Instance.CreateRewardedAd(iosAdUnitId); | |
| break; | |
| default: | |
| Debug.LogWarning("Mediation service is not available for this platform:" + Application.platform); | |
| return; | |
| } | |
| // Load Events | |
| m_RewardedAd.OnLoaded += AdLoaded; | |
| m_RewardedAd.OnFailedLoad += AdFailedLoad; | |
| // Show Events | |
| m_RewardedAd.OnUserRewarded += UserRewarded; | |
| m_RewardedAd.OnClosed += AdClosed; | |
| Debug.Log($"Initialized On Start. Loading Ad..."); | |
| LoadAd(); | |
| } | |
| public async void LoadAd() | |
| { | |
| try | |
| { | |
| await m_RewardedAd.LoadAsync(); | |
| } | |
| catch (LoadFailedException) | |
| { | |
| // We will handle the failure in the OnFailedLoad callback | |
| } | |
| } | |
| public async void ShowRewarded() | |
| { | |
| if (m_RewardedAd?.AdState == AdState.Loaded) | |
| { | |
| try | |
| { | |
| var showOptions = new RewardedAdShowOptions {AutoReload = true}; | |
| await m_RewardedAd.ShowAsync(showOptions); | |
| Debug.Log("Rewarded Shown!"); | |
| } | |
| catch (ShowFailedException e) | |
| { | |
| Debug.LogWarning($"Rewarded failed to show: {e.Message}"); | |
| } | |
| } | |
| } | |
| private void UserRewarded(object sender, RewardEventArgs e) | |
| { | |
| Debug.Log($"User Rewarded! Type: {e.Type} Amount: {e.Amount}"); | |
| OnRewarded?.Invoke(); | |
| } | |
| private void AdClosed(object sender, EventArgs args) | |
| { | |
| Debug.Log("Rewarded Closed! Loading Ad..."); | |
| OnAdFinished?.Invoke(); | |
| } | |
| private void AdLoaded(object sender, EventArgs e) | |
| { | |
| Debug.Log("Ad loaded"); | |
| OnAdLoaded?.Invoke(); | |
| } | |
| private void AdFailedLoad(object sender, LoadErrorEventArgs e) | |
| { | |
| Debug.Log("Failed to load ad"); | |
| Debug.Log(e.Message); | |
| OnFailed?.Invoke(); | |
| } | |
| private void OnDestroy() | |
| { | |
| m_RewardedAd?.Dispose(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment