Unity Ads Mediation
Perfect — let’s build a single, clean, production‑ready AdsManager that handles both Interstitial and Rewarded ads in one place.
This is the version most studios use in real projects because it’s simple, reliable, and reloads ads automatically.
Below is the full code, polished and ready to drop into your Unity project.
You will need Google ads plugin: https://github.com/googleads/googleads-mobile-unity/releases/tag/v10.4.1
🎮 AdsManager.cs (Interstitial + Rewarded in one script)
using UnityEngine;
using GoogleMobileAds.Api;
public class AdsManager : MonoBehaviour
{
public static AdsManager Instance;
[Header("Ad Unit IDs")]
[SerializeField] private string interstitialAdUnitId = "YOUR_INTERSTITIAL_AD_UNIT_ID";
[SerializeField] private string rewardedAdUnitId = "YOUR_REWARDED_AD_UNIT_ID";
private InterstitialAd interstitialAd;
private RewardedAd rewardedAd;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
return;
}
}
private void Start()
{
MobileAds.Initialize(initStatus => { });
LoadInterstitial();
LoadRewarded();
}
// -----------------------------
// INTERSTITIAL
// -----------------------------
public void LoadInterstitial()
{
if (interstitialAd != null)
{
interstitialAd.Destroy();
interstitialAd = null;
}
InterstitialAd.Load(interstitialAdUnitId, new AdRequest(),
(InterstitialAd ad, LoadAdError error) =>
{
if (error != null)
{
Debug.LogError("Interstitial failed to load: " + error);
return;
}
interstitialAd = ad;
RegisterInterstitialEvents();
Debug.Log("Interstitial loaded");
});
}
private void RegisterInterstitialEvents()
{
interstitialAd.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Interstitial closed — loading next one");
LoadInterstitial();
};
interstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Interstitial failed to show: " + error);
};
}
public void ShowInterstitial()
{
if (interstitialAd != null && interstitialAd.CanShowAd())
{
interstitialAd.Show();
}
else
{
Debug.Log("Interstitial not ready");
}
}
// -----------------------------
// REWARDED
// -----------------------------
public void LoadRewarded()
{
if (rewardedAd != null)
{
rewardedAd.Destroy();
rewardedAd = null;
}
RewardedAd.Load(rewardedAdUnitId, new AdRequest(),
(RewardedAd ad, LoadAdError error) =>
{
if (error != null)
{
Debug.LogError("Rewarded failed to load: " + error);
return;
}
rewardedAd = ad;
RegisterRewardedEvents();
Debug.Log("Rewarded loaded");
});
}
private void RegisterRewardedEvents()
{
rewardedAd.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Rewarded closed — loading next one");
LoadRewarded();
};
rewardedAd.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Rewarded failed to show: " + error);
};
}
public void ShowRewarded(System.Action onRewardEarned)
{
if (rewardedAd != null && rewardedAd.CanShowAd())
{
rewardedAd.Show((Reward reward) =>
{
Debug.Log("User earned reward: " + reward.Amount);
onRewardEarned?.Invoke();
});
}
else
{
Debug.Log("Rewarded not ready");
}
}
}
🧩 How to Use It in Your Game
1. Add AdsManager to your first scene
- Create an empty GameObject
- Name it AdsManager
- Attach the script
- Fill in your Ad Unit IDs
2. Show an Interstitial
AdsManager.Instance.ShowInterstitial();
3. Show a Rewarded Ad
AdsManager.Instance.ShowRewarded(() =>
{
// Give the player their reward
Debug.Log("Reward granted!");
});
⚡ Why this version is production‑ready
- Automatically reloads ads after showing
- Uses a singleton for easy access
- Handles all failure cases
- Clean separation of interstitial vs rewarded
- Works with any mediation network (Unity Ads, AdMob, MAX, LevelPlay, etc.)
Absolutely — let’s wire up a clean, flexible UI button system that triggers your Interstitial and Rewarded ads through the AdsManager you already have.
I’ll give you:
- A simple UI setup (Unity UI)
- A ButtonHandler script that connects UI buttons to your AdsManager
- Optional: a polished version with disabled buttons until ads are ready
Everything stays clean and production‑friendly.
🎮 1. Unity UI Setup
In your Unity scene:
- Create a Canvas
- Inside it, create two UI Buttons:
- Show Interstitial
- Show Rewarded
- Add a TextMeshProUGUI label to each button (optional but recommended)
Name them something like:
Btn_InterstitialBtn_Rewarded
🧩 2. ButtonHandler.cs (Connects UI to AdsManager)
This script sits on a GameObject in your scene and simply calls the AdsManager.
using UnityEngine;
public class ButtonHandler : MonoBehaviour
{
public void OnInterstitialButtonPressed()
{
AdsManager.Instance.ShowInterstitial();
}
public void OnRewardedButtonPressed()
{
AdsManager.Instance.ShowRewarded(() =>
{
Debug.Log("Reward granted from UI button!");
// Add your reward logic here
});
}
}
🎛️ 3. Hooking Up the Buttons
For each button:
- Select the button in the Hierarchy
- Scroll to the Button (Script) component
- In OnClick():
- Click +
- Drag your
ButtonHandlerGameObject into the slot - Choose the appropriate function:
ButtonHandler → OnInterstitialButtonPressedButtonHandler → OnRewardedButtonPressed
Done. Your UI now triggers ads.
⭐ Optional Upgrade: Disable Buttons Until Ads Are Ready
If you want a polished UX, here’s a version that disables the buttons until ads are loaded.
ButtonHandler.cs (Enhanced)
using UnityEngine;
using UnityEngine.UI;
public class ButtonHandler : MonoBehaviour
{
[Header("UI Buttons")]
[SerializeField] private Button interstitialButton;
[SerializeField] private Button rewardedButton;
private void Start()
{
// Disable buttons until ads are ready
interstitialButton.interactable = false;
rewardedButton.interactable = false;
// Poll ad readiness
InvokeRepeating(nameof(CheckAdStatus), 0.5f, 0.5f);
}
private void CheckAdStatus()
{
interstitialButton.interactable =
AdsManager.Instance != null &&
AdsManager.Instance.IsInterstitialReady();
rewardedButton.interactable =
AdsManager.Instance != null &&
AdsManager.Instance.IsRewardedReady();
}
public void OnInterstitialButtonPressed()
{
AdsManager.Instance.ShowInterstitial();
}
public void OnRewardedButtonPressed()
{
AdsManager.Instance.ShowRewarded(() =>
{
Debug.Log("Reward granted!");
});
}
}
Add these helper methods to your AdsManager:
public bool IsInterstitialReady()
{
return interstitialAd != null && interstitialAd.CanShowAd();
}
public bool IsRewardedReady()
{
return rewardedAd != null && rewardedAd.CanShowAd();
}
Alright — let’s build this out beautifully.
You’ll get two polished systems:
- A Reward Popup Window (appears after a rewarded ad finishes)
- A Cooldown Timer (e.g., “Watch again in 30s”) that disables the button until the timer ends
Everything plugs directly into your existing AdsManager.
I’ll keep the code clean, modular, and production‑ready.
🎁 1. Reward Popup Window (UI + Script)
A. Unity UI Setup
Inside your Canvas, create:
- Panel → name it
RewardPopup - Inside it:
- Text (TMP) → “You earned: X”
- Button → “OK”
Set the panel to inactive by default.
B. RewardPopup.cs
using UnityEngine;
using TMPro;
public class RewardPopup : MonoBehaviour
{
public static RewardPopup Instance;
[SerializeField] private GameObject popupRoot;
[SerializeField] private TextMeshProUGUI rewardText;
private void Awake()
{
Instance = this;
popupRoot.SetActive(false);
}
public void Show(string rewardMessage)
{
rewardText.text = rewardMessage;
popupRoot.SetActive(true);
}
public void Hide()
{
popupRoot.SetActive(false);
}
}
C. Triggering the popup from AdsManager
Modify your Rewarded call:
AdsManager.Instance.ShowRewarded(() =>
{
RewardPopup.Instance.Show("You earned 100 coins!");
});
⏳ 2. Cooldown Timer (e.g., “Watch again in 30s”)
This system:
- Disables the Rewarded button
- Shows a countdown
- Re-enables the button when ready
- Works even if the app pauses or loses focus
A. UI Setup
On your Rewarded button:
- Add a TextMeshProUGUI child → name it
CooldownText - Set it to something like “Watch Ad”
- Disable Raycast Target on the text
B. CooldownTimer.cs
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections;
public class CooldownTimer : MonoBehaviour
{
[Header("Rewarded Button")]
[SerializeField] private Button rewardedButton;
[SerializeField] private TextMeshProUGUI cooldownText;
[Header("Cooldown Settings")]
[SerializeField] private int cooldownSeconds = 30;
private bool isCoolingDown = false;
private void Start()
{
cooldownText.text = "Watch Ad";
}
public void StartCooldown()
{
if (!isCoolingDown)
StartCoroutine(CooldownRoutine());
}
private IEnumerator CooldownRoutine()
{
isCoolingDown = true;
rewardedButton.interactable = false;
int remaining = cooldownSeconds;
while (remaining > 0)
{
cooldownText.text = $"Watch again in {remaining}s";
yield return new WaitForSeconds(1f);
remaining--;
}
cooldownText.text = "Watch Ad";
rewardedButton.interactable = true;
isCoolingDown = false;
}
}
🎯 C. Hooking It All Together
1. When the user taps the Rewarded button
In your ButtonHandler:
public CooldownTimer cooldownTimer;
public void OnRewardedButtonPressed()
{
AdsManager.Instance.ShowRewarded(() =>
{
RewardPopup.Instance.Show("You earned 100 coins!");
cooldownTimer.StartCooldown();
});
}
🌟 Final Result
You now have:
✔ A Reward Popup Window
- Shows the reward message
- Clean, reusable, modular
✔ A Cooldown Timer
- Disables the button
- Shows “Watch again in 30s”
- Re-enables automatically
- Prevents spam watching
✔ Fully integrated with your AdsManager
- No duplicated logic
- No messy UI hacks
- Works in any scene
If you want, I can also add:
- Animated popup (scale-in, fade-in)
- Sound effects
- A reward inventory system
- A “No Ads” IAP that disables interstitials
Just tell me how fancy you want it.
Comments