I recently swapped over to the new Unity Ads v4 solution because of Google Play policies, and I can’t figure out what I’m doing wrong here.
When I build to my Android device, neither the OnInitializationComplete() or OnInitializationFailed() callbacks are triggered.
Here’s some more data:
- Unity 2020.3.46f1
- Android API 33
- Tried in Unity Ads 4.0.1 & 4.3.0
- Works in editor just fine!
- Tried in both test mode and not test mode on device.
To get Android API 33 to work I had to do these steps which could have caused issues maybe?
- In baseProjectTemplate.gradle: classpath ‘com.android.tools.build:gradle:4.0.1’(changed to 4.0.1)
- In
mainTemplate.gradle:
buildToolsVersion ‘30.0.3’ (changed
build tools to 30.3.3)
…
using System;
using UnityEngine;
using UnityEngine.Advertisements;
public class AdsController : Monobehavior, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener //, IUnityAdsListener
{
public static AdsController Instance;
/// <summary>
/// Occurs when an interstitial ad is closed.
/// </summary>
public event Action<InterstitialAdNetwork, AdPlacement> InterstitialAdCompleted;
/// <summary>
/// Occurs when a rewarded ad is skipped (the user didn't complete watching
/// the ad and therefore is not entitled to the reward).
/// </summary>
public event Action<RewardedAdNetwork, AdPlacement> RewardedAdSkipped;
/// <summary>
/// Occurs when a rewarded ad completed and the user should be rewarded.
/// </summary>
public event Action<RewardedAdNetwork, AdPlacement> RewardedAdCompleted;
private const string InterstitialAdPlacement = "video";
private const string RewardedVideoAdPlacement = "rewardedVideo";
private const string IosGameId = REMOVED_FOR_POST;
private const string AndroidGameId = REMOVED_FOR_POST;
#if UNITY_EDITOR
private bool _testMode = true;
#else
private bool _testMode = false;
#endif
public void Awake()
{
Instance = this;
Debug.LogError("AdsController.Awake()");
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
Advertisement.Initialize(IosGameId, _testMode, this);
}
else
{
DebugConsole.Log($"Trying to load Ads {AndroidGameId} test? {_testMode}.");
Advertisement.Initialize(AndroidGameId, _testMode, this);
}
}
private void OnRewardedAdCompleted(RewardedAdNetwork adNetwork, AdPlacement adPlacement)
{
RewardedAdCompleted?.Invoke(adNetwork, adPlacement);
}
private void OnRewardedAdSkipped(RewardedAdNetwork adNetwork, AdPlacement adPlacement)
{
RewardedAdSkipped?.Invoke(adNetwork, adPlacement);
}
private void OnInterstitialAdCompleted(InterstitialAdNetwork adNetwork, AdPlacement adPlacement)
{
InterstitialAdCompleted?.Invoke(adNetwork, adPlacement);
}
public void ShowRewardedAd()
{
if (!Advertisement.isShowing && m_rewardedAdReady)
{
ShowPlacement(RewardedVideoAdPlacement);
}
else
{
OnRewardedAdSkipped(RewardedAdNetwork.UnityAds, AdPlacement.Default);
}
}
public void ShowInterstitialAd()
{
if (!Advertisement.isShowing && m_adReady)
{
ShowPlacement(InterstitialAdPlacement);
}
else
{
OnInterstitialAdCompleted(InterstitialAdNetwork.UnityAds, AdPlacement.Default);
}
}
// ================================================================================================
// NEW ADS (V4.0)
// ================================================================================================
private bool m_initialized;
private bool m_isLoadingAd;
private bool m_isLoadingRewardedAd;
private bool m_adReady;
private bool m_rewardedAdReady;
private void Update()
{
if (m_initialized)
{
if (!m_isLoadingAd && !m_adReady)
{
LoadPlacement(InterstitialAdPlacement);
}
if (!m_isLoadingRewardedAd && !m_rewardedAdReady)
{
LoadPlacement(RewardedVideoAdPlacement);
}
}
}
private void LoadPlacement(string placementId)
{
if (placementId == InterstitialAdPlacement)
{
m_isLoadingAd = true;
}
else if (placementId == RewardedVideoAdPlacement)
{
m_isLoadingRewardedAd = true;
}
Debug.Log($"Start load: {placementId}");
Advertisement.Load(placementId, this);
}
private void ShowPlacement(string placementId)
{
Debug.Log($"Trying to show: {placementId}");
Advertisement.Show(placementId, this);
}
public void OnInitializationComplete()
{
m_initialized = true;
Debug.Log("Initialized Unity Ads!");
}
public void OnInitializationFailed(UnityAdsInitializationError error, string message)
{
m_initialized = false;
Debug.LogError($"{error}:{message}");
}
public void OnUnityAdsAdLoaded(string placementId)
{
Debug.Log($"Loaded {placementId} ad");
if (placementId == InterstitialAdPlacement)
{
m_adReady = true;
m_isLoadingAd = false;
}
else if (placementId == RewardedVideoAdPlacement)
{
m_rewardedAdReady = true;
m_isLoadingRewardedAd = false;
}
}
public void OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)
{
Debug.LogError($"Failed Load ({placementId}): {error}: {message}");
if (placementId == InterstitialAdPlacement)
{
m_adReady = false;
m_isLoadingAd = false;
}
else if (placementId == RewardedVideoAdPlacement)
{
m_rewardedAdReady = false;
m_isLoadingRewardedAd = false;
}
}
public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
{
DebugConsole.LogError($"Failed Show ({placementId}): {error}: {message}");
if (placementId == InterstitialAdPlacement)
{
m_adReady = false;
m_isLoadingAd = false;
}
else if (placementId == RewardedVideoAdPlacement)
{
m_rewardedAdReady = false;
m_isLoadingRewardedAd = false;
}
}
public void OnUnityAdsShowStart(string placementId)
{
}
public void OnUnityAdsShowClick(string placementId)
{
}
public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
{
if (placementId == InterstitialAdPlacement)
{
m_adReady = false;
m_isLoadingAd = false;
OnInterstitialAdCompleted(InterstitialAdNetwork.UnityAds, AdPlacement.Default);
}
else if (placementId == RewardedVideoAdPlacement)
{
m_rewardedAdReady = false;
m_isLoadingRewardedAd = false;
if (showCompletionState == UnityAdsShowCompletionState.COMPLETED ||
showCompletionState == UnityAdsShowCompletionState.UNKNOWN)
{
OnRewardedAdCompleted(RewardedAdNetwork.UnityAds, AdPlacement.Default);
}
else
{
OnRewardedAdSkipped(RewardedAdNetwork.UnityAds, AdPlacement.Default);
}
}
}
}