Hi,
Ive finished my game and have test ads working in Unity Editor but I can’t seem to get them working in test flight. Is there a foolproof guide anywhere that shows what needs to be set in Xcode, Apple Developer, App Store Connect etc so I can see if I’ve missed any settings that would stop ads showing?
Test Mode is active on my script
Ive seen things mentioning ATT Permission and also updating the apps plist but Im not 100% sure what Im doing with those to be honest
Ive checked the Unity Dashboard and the Game IDs and that all looks correct and as I say the ads work perfect in the editor
thank you!
Heres my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
public class AdManager : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsShowListener, IUnityAdsLoadListener
{
private static AdManager _instance;
// [SerializeField] private string androidGameId = "5365282"; // Android Game ID
[SerializeField] private string iOSGameId = "5365283"; // iOS Game ID
[SerializeField] private string interstitialAdUnitId = "Interstitial_iOS"; // Replace with your Interstitial Ad Unit ID
[SerializeField] private bool testMode = true;
private string gameId;
private bool isAdReady = false;
private bool isAdShowing = false; // Prevent multiple ads from showing at the same time
private float adCooldown = 30f; // 30 seconds cooldown between ads
private float lastAdTime = 0f; // Tracks the time when the last ad was shown
public static AdManager Instance
{
get
{
if (_instance == null)
{
var obj = new GameObject("AdManager");
_instance = obj.AddComponent<AdManager>();
DontDestroyOnLoad(obj);
}
return _instance;
}
}
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(gameObject);
}
else
{
_instance = this;
DontDestroyOnLoad(gameObject);
InitializeAds();
}
}
private void InitializeAds()
{
#if UNITY_IOS
gameId = iOSGameId;
// #elif UNITY_ANDROID
// gameId = androidGameId;
#elif UNITY_EDITOR
// gameId = androidGameId; // For testing in the editor
#endif
if (!Advertisement.isInitialized && Advertisement.isSupported)
{
Advertisement.Initialize(gameId, testMode, this);
}
}
public bool IsInterstitialAdReady()
{
return isAdReady && !isAdShowing && Time.time - lastAdTime >= adCooldown; // Ensure no ad is currently showing and cooldown has passed
}
public void ShowInterstitialAd()
{
if (Time.time - lastAdTime < adCooldown) // Check cooldown
{
Debug.Log("Ad is on cooldown. Not showing ad.");
return;
}
Advertisement.Load(interstitialAdUnitId, this);
if (IsInterstitialAdReady())
{
isAdShowing = true;
lastAdTime = Time.time; // Update last ad time
Advertisement.Show(interstitialAdUnitId, this);
}
else
{
Debug.LogWarning("Interstitial ad not ready yet.");
}
}
public void LoadInterstitialAd()
{
if (!isAdReady && !isAdShowing) // Only load if not already ready or showing
{
Advertisement.Load(interstitialAdUnitId, this);
Debug.Log("Loading interstitial ad...");
}
else
{
Debug.Log("Ad is already loaded or showing; no need to reload.");
}
}
private IEnumerator LoadAdWithDelay(float delay)
{
yield return new WaitForSeconds(delay);
LoadInterstitialAd();
}
// Callbacks for IUnityAdsInitializationListener
public void OnInitializationComplete()
{
Debug.Log("Unity Ads initialization complete.");
LoadInterstitialAd(); // Load the first ad after initialization
}
public void OnInitializationFailed(UnityAdsInitializationError error, string message)
{
Debug.LogError($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
}
// Callbacks for IUnityAdsLoadListener
public void OnUnityAdsAdLoaded(string placementId)
{
if (placementId == interstitialAdUnitId)
{
isAdReady = true;
Debug.Log("Interstitial ad loaded successfully.");
}
}
public void OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)
{
Debug.LogError($"Failed to load ad {placementId}: {error.ToString()} - {message}");
}
// Callbacks for IUnityAdsShowListener
public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
{
if (placementId == interstitialAdUnitId)
{
if (showCompletionState == UnityAdsShowCompletionState.COMPLETED)
{
Debug.Log("Ad completed successfully.");
}
// Reset ad state
isAdShowing = false;
isAdReady = false; // Mark as not ready until a new ad is loaded
StartCoroutine(LoadAdWithDelay(5f)); // Load next ad after a short delay
ResumeGame(); // Resume gameplay
}
}
public void OnUnityAdsShowFailed(string placementId, UnityAdsShowError error, string message)
{
Debug.LogError($"Ad Show Failed for {placementId}: {error.ToString()} - {message}");
isAdShowing = false; // Reset flag even if ad fails
}
public void OnUnityAdsShowStart(string placementId)
{
Debug.Log($"Ad {placementId} started.");
}
public void OnUnityAdsShowClick(string placementId)
{
Debug.Log($"Ad {placementId} clicked.");
}
public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
{
Debug.LogError($"Ad Show Failure for {placementId}: {error.ToString()} - {message}");
}
private void ResumeGame()
{
Debug.Log("Resuming gameplay after ad.");
// Add any logic here to resume gameplay if needed
}
}