using UnityEngine; using GoogleMobileAds.Api; using GoogleMobileAds.Common; using System; using System.Collections; using System.Collections.Generic; public class AdMobAllInOne : MonoBehaviour { public static AdMobAllInOne Instance; [Header("REKLAM MODU")] [Tooltip("Geliştirme sırasında true bırak. Yayına çıkarken false yap.")] [SerializeField] private bool useTestAds = false; private const string TEST_INTERSTITIAL_ID = "ca-app-pub-3940256099942544/1033173712"; private const string TEST_REWARDED_ID = "ca-app-pub-3940256099942544/5224354917"; private const string REAL_INTERSTITIAL_ID = "ca-app-pub-9882956647539723/3464283079"; private const string REAL_REWARDED_ID = "ca-app-pub-9882956647539723/9566656997"; private string InterstitialId => useTestAds ? TEST_INTERSTITIAL_ID : REAL_INTERSTITIAL_ID; private string RewardedId => useTestAds ? TEST_REWARDED_ID : REAL_REWARDED_ID; [Header("TEST CİHAZLARI")] [Tooltip("Gerçek ID'lerle güvenli test için kendi cihazının test ID'sini ekle.")] [SerializeField] private List testDeviceIds = new List(); [Header("GEÇİŞ REKLAMI AYARI")] [SerializeField] private float minSecondsBetweenInterstitial = 45f; private InterstitialAd interstitialAd; private RewardedAd rewardedAd; private bool isInitialized = false; private bool isInterstitialLoading = false; private bool isRewardedLoading = false; private int interstitialRetry = 0; private int rewardedRetry = 0; private const int MAX_RETRY = 6; private float lastInterstitialTime = -999f; private void Awake() { if (Instance != null && Instance != this) { Destroy(gameObject); return; } Instance = this; DontDestroyOnLoad(gameObject); Debug.Log("[ADMOB] Reklam yöneticisi oluşturuldu. Banner sistemi KAPALI."); } private void Start() { InitializeAds(); } private void RunOnUnityThread(Action action) { if (action == null) return; MobileAdsEventExecutor.ExecuteInUpdate(action); } private void InitializeAds() { if (isInitialized) return; if (testDeviceIds != null && testDeviceIds.Count > 0) { RequestConfiguration config = new RequestConfiguration { TestDeviceIds = testDeviceIds }; MobileAds.SetRequestConfiguration(config); } MobileAds.Initialize(initStatus => { RunOnUnityThread(() => { isInitialized = true; Debug.Log("[ADMOB] MobileAds başlatıldı. Mod: " + (useTestAds ? "TEST" : "GERÇEK")); LoadInterstitial(); LoadRewarded(); }); }); } private IEnumerator RetryAfter(float seconds, Action action) { yield return new WaitForSecondsRealtime(seconds); action?.Invoke(); } private float RetryDelay(int retry) { return Mathf.Pow(2f, Mathf.Clamp(retry, 1, MAX_RETRY)); } // ===================================================== // INTERSTITIAL / GEÇİŞ REKLAMI // ===================================================== public void LoadInterstitial() { if (!isInitialized) { Debug.Log("[ADMOB] Interstitial yüklenemedi, AdMob daha başlamadı."); return; } if (isInterstitialLoading) return; if (interstitialAd != null) { interstitialAd.Destroy(); interstitialAd = null; } isInterstitialLoading = true; Debug.Log("[ADMOB] Interstitial yükleniyor: " + InterstitialId); AdRequest request = new AdRequest(); InterstitialAd.Load(InterstitialId, request, (InterstitialAd ad, LoadAdError error) => { RunOnUnityThread(() => { isInterstitialLoading = false; if (error != null || ad == null) { interstitialRetry = Mathf.Min(interstitialRetry + 1, MAX_RETRY); float delay = RetryDelay(interstitialRetry); Debug.LogError("[ADMOB] Interstitial yüklenemedi: " + (error != null ? error.GetMessage() : "ad null") + " | " + delay + " sn sonra tekrar denenecek."); StartCoroutine(RetryAfter(delay, LoadInterstitial)); return; } interstitialAd = ad; interstitialRetry = 0; Debug.Log("[ADMOB] Interstitial yüklendi."); RegisterInterstitialEvents(interstitialAd); }); }); } private void RegisterInterstitialEvents(InterstitialAd ad) { ad.OnAdFullScreenContentClosed += () => { RunOnUnityThread(() => { lastInterstitialTime = Time.unscaledTime; Debug.Log("[ADMOB] Interstitial kapandı, yenisi yükleniyor."); LoadInterstitial(); }); }; ad.OnAdFullScreenContentFailed += (AdError err) => { RunOnUnityThread(() => { Debug.LogError("[ADMOB] Interstitial gösterilemedi: " + (err != null ? err.GetMessage() : "bilinmeyen")); LoadInterstitial(); }); }; ad.OnAdPaid += (AdValue value) => { RunOnUnityThread(() => { Debug.Log("[ADMOB] Interstitial gelir: " + value.Value + " " + value.CurrencyCode); }); }; } public void ShowInterstitial() { if (!isInitialized) { Debug.Log("[ADMOB] Interstitial gösterilemedi, AdMob başlamadı."); return; } if ((Time.unscaledTime - lastInterstitialTime) < minSecondsBetweenInterstitial) { Debug.Log("[ADMOB] Interstitial süre limiti nedeniyle gösterilmedi."); return; } TryShowInterstitial(); } public void ShowInterstitialIgnoreTime() { if (!isInitialized) { Debug.Log("[ADMOB] Interstitial gösterilemedi, AdMob başlamadı."); return; } TryShowInterstitial(); } private void TryShowInterstitial() { if (interstitialAd != null && interstitialAd.CanShowAd()) { Debug.Log("[ADMOB] Interstitial gösteriliyor."); interstitialAd.Show(); } else { Debug.Log("[ADMOB] Interstitial hazır değil, tekrar yükleniyor."); LoadInterstitial(); } } public bool IsInterstitialReady() { return interstitialAd != null && interstitialAd.CanShowAd(); } // ===================================================== // REWARDED / ÖDÜLLÜ REKLAM // ===================================================== public void LoadRewarded() { if (!isInitialized) { Debug.Log("[ADMOB] Rewarded yüklenemedi, AdMob daha başlamadı."); return; } if (isRewardedLoading) return; if (rewardedAd != null) { rewardedAd.Destroy(); rewardedAd = null; } isRewardedLoading = true; Debug.Log("[ADMOB] Rewarded yükleniyor: " + RewardedId); AdRequest request = new AdRequest(); RewardedAd.Load(RewardedId, request, (RewardedAd ad, LoadAdError error) => { RunOnUnityThread(() => { isRewardedLoading = false; if (error != null || ad == null) { rewardedRetry = Mathf.Min(rewardedRetry + 1, MAX_RETRY); float delay = RetryDelay(rewardedRetry); Debug.LogError("[ADMOB] Rewarded yüklenemedi: " + (error != null ? error.GetMessage() : "ad null") + " | " + delay + " sn sonra tekrar denenecek."); StartCoroutine(RetryAfter(delay, LoadRewarded)); return; } rewardedAd = ad; rewardedRetry = 0; Debug.Log("[ADMOB] Rewarded yüklendi."); RegisterRewardedEvents(rewardedAd); }); }); } private void RegisterRewardedEvents(RewardedAd ad) { ad.OnAdFullScreenContentClosed += () => { RunOnUnityThread(() => { Debug.Log("[ADMOB] Rewarded kapandı, yenisi yükleniyor."); LoadRewarded(); }); }; ad.OnAdFullScreenContentFailed += (AdError err) => { RunOnUnityThread(() => { Debug.LogError("[ADMOB] Rewarded gösterilemedi: " + (err != null ? err.GetMessage() : "bilinmeyen")); LoadRewarded(); }); }; ad.OnAdPaid += (AdValue value) => { RunOnUnityThread(() => { Debug.Log("[ADMOB] Rewarded gelir: " + value.Value + " " + value.CurrencyCode); }); }; } public void ShowRewarded(Action onReward = null, Action onNotReady = null) { if (!isInitialized) { Debug.Log("[ADMOB] Rewarded gösterilemedi, AdMob başlamadı."); onNotReady?.Invoke(); return; } if (rewardedAd != null && rewardedAd.CanShowAd()) { Debug.Log("[ADMOB] Rewarded gösteriliyor."); rewardedAd.Show((Reward reward) => { RunOnUnityThread(() => { Debug.Log("[ADMOB] Rewarded ödül kazanıldı: " + reward.Amount + " " + reward.Type); onReward?.Invoke(); }); }); } else { Debug.Log("[ADMOB] Rewarded hazır değil, tekrar yükleniyor."); LoadRewarded(); onNotReady?.Invoke(); } } public bool IsRewardedReady() { return rewardedAd != null && rewardedAd.CanShowAd(); } // ===================================================== // BUTTON ONCLICK İÇİN // ===================================================== public void ShowInterstitialFromButton() { ShowInterstitial(); } public void ShowInterstitialIgnoreTimeFromButton() { ShowInterstitialIgnoreTime(); } public void ShowRewardedFromButton() { ShowRewarded(() => { Debug.Log("[ADMOB] Rewarded butondan izlendi, standart ödül verildi."); }); } public void LoadInterstitialFromButton() { LoadInterstitial(); } public void LoadRewardedFromButton() { LoadRewarded(); } public void DebugAdStatusFromButton() { Debug.Log("[ADMOB] Interstitial hazır mı: " + IsInterstitialReady()); Debug.Log("[ADMOB] Rewarded hazır mı: " + IsRewardedReady()); } // ===================================================== // BANNER TAMAMEN KAPALI // ===================================================== public void ShowBannerFromButton() { Debug.Log("[ADMOB] Banner kapalı. Gösterilmeyecek."); } public void HideBannerFromButton() { Debug.Log("[ADMOB] Banner kapalı. Gizlenecek banner yok."); } public void ReloadBannerFromButton() { Debug.Log("[ADMOB] Banner kapalı. Yeniden yüklenecek banner yok."); } public void ShowBanner() { Debug.Log("[ADMOB] Banner kapalı."); } public void HideBanner() { Debug.Log("[ADMOB] Banner kapalı."); } // ===================================================== // TEMİZLEME // ===================================================== private void OnDestroy() { if (Instance != this) return; if (interstitialAd != null) { interstitialAd.Destroy(); interstitialAd = null; } if (rewardedAd != null) { rewardedAd.Destroy(); rewardedAd = null; } Instance = null; } }