I followed the Unity Documentation for implementing Unity Ads to my mobile game. Here is my AdsInitializer script.
using UnityEngine;
using UnityEngine.Advertisements;
public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener
{
[SerializeField] string _androidGameId = "5293323";
[SerializeField] string _iOSGameId = "5293322";
[SerializeField] bool _testMode = true;
private string _gameId;
[SerializeField] RewardedAdsButton rewardedAdsButton;
[SerializeField] InterstitialAds interstitialAds;
void Awake()
{
InitializeAds();
}
public void InitializeAds()
{
#if UNITY_IOS
_gameId = _iOSGameId;
#elif UNITY_ANDROID
_gameId = _androidGameId;
#elif UNITY_EDITOR
_gameId = _androidGameId; //Only for testing the functionality in the Editor
#endif
if (!Advertisement.isInitialized && Advertisement.isSupported)
{
Advertisement.Initialize(_gameId, _testMode, this);
}
}
public void OnInitializationComplete()
{
Debug.Log("Unity Ads initialization complete.");
if(rewardedAdsButton != null) rewardedAdsButton.LoadAd();
if(interstitialAds != null) interstitialAds.LoadAd();
}
public void OnInitializationFailed(UnityAdsInitializationError error, string message)
{
Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
if (Application.internetReachability != NetworkReachability.NotReachable)
{
InitializeAds();
}
}
public void RestartAds()
{
if (Advertisement.isInitialized)
{
Advertisement.Initialize(_gameId, _testMode, this);
}
}
}
It’s simply initializing the ads and loading ads on OnInitializationComplete().
and here is my RewardedAdsButton script
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
using TMPro;
public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{
public BallController ballController;
[SerializeField] GameObject cooldownTimer;
[SerializeField] TextMeshProUGUI totalCoinText;
[SerializeField] Button _showAdButton;
[SerializeField] string _androidAdUnitId = "Rewarded_for_android";
[SerializeField] string _iOSAdUnitId = "Rewarded_for_ios";
int rewardedID = 0;
string _adUnitId = null; // This will remain null for unsupported platforms
void Start()
{
GameObject ballGameObject = GameObject.Find("Ball");
if (ballGameObject != null)
{
ballController = ballGameObject.GetComponent<BallController>();
}
}
void Awake()
{
// Get the Ad Unit ID for the current platform:
#if UNITY_IOS
_adUnitId = _iOSAdUnitId;
#elif UNITY_ANDROID
_adUnitId = _androidAdUnitId;
#endif
// Disable the button until the ad is ready to show:
_showAdButton.interactable = false;
}
public void setRewardedId(int x)
{
rewardedID = x;
}
// Call this public method when you want to get an ad ready to show.
public void LoadAd()
{
// IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
Debug.Log("Loading Ad: " + _adUnitId);
Advertisement.Load(_adUnitId, this);
}
// If the ad successfully loads, add a listener to the button and enable it:
public void OnUnityAdsAdLoaded(string adUnitId)
{
Debug.Log("Ad Loaded: " + adUnitId);
if (adUnitId.Equals(_adUnitId))
{
// Configure the button to call the ShowAd() method when clicked:
_showAdButton.onClick.AddListener(ShowAd);
// Enable the button for users to click:
_showAdButton.interactable = true;
}
}
// Implement a method to execute when the user clicks the button:
public void ShowAd()
{
// Disable the button:
_showAdButton.interactable = false;
// Then show the ad:
Advertisement.Show(_adUnitId, this);
}
// Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
{
if ( rewardedID==1 && adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
{
Debug.Log("Unity Ads Rewarded Ad Completed");
// Grant a reward.
ballController.ReviveTheBall();
LoadAd();
}
if (rewardedID == 2 && adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
{
Debug.Log("Unity Ads Rewarded Ad Completed");
cooldownTimer.SetActive(true);
PlayerPrefs.SetInt("currentCoin", (PlayerPrefs.GetInt("currentCoin") + 25));
if (totalCoinText != null)
{
totalCoinText.text = PlayerPrefs.GetInt("currentCoin").ToString();
}
}
}
// Implement Load and Show Listener error callbacks:
public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
{
Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
// Use the error details to determine whether to try to load another ad.
}
public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
{
Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
// Use the error details to determine whether to try to load another ad.
}
public void OnUnityAdsShowStart(string adUnitId) { }
public void OnUnityAdsShowClick(string adUnitId) { }
void OnDestroy()
{
// Clean up the button listeners:
_showAdButton.onClick.RemoveAllListeners();
}
}
The system works well until the GameOver state appears. I’ve found a "
An ad plays in your game.
While that ad is playing, the SDK starts loading a new ad for that placement.
The SDK ready event fires.
You call SceneManager.LoadScene(0) sometime later.
The SDK Initialize call happens, but the SDK knows it’s already initialised so does nothing.
The OnUnityAdsReady function never gets called because the Ready event already happened before you reloaded the scene!
" prediction about the problem but i couldnt find any solution about it.