using UnityEngine.Advertisements;
public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
[SerializeField] string _androidGameId = "4634758";
[SerializeField] string _iOSGameId = "4634759";
[SerializeField] bool _testMode = false;
[SerializeField] Button _showAdButton;
[SerializeField] string rewardAndroidAdUnitId = "Rewarded_Android";
[SerializeField] string rewardiOSAdUnitId = "Rewarded_iOS";
string rewardAdUnitId = null; // This will remain null for unsupported platforms
_gameId = (Application.platform == RuntimePlatform.IPhonePlayer)
rewardAdUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
Advertisement.Initialize(_gameId, _testMode, this);
rewardAdUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
public void OnInitializationComplete()
Debug.Log("Unity Ads initialization complete.");
public void OnInitializationFailed(UnityAdsInitializationError error, string message)
Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
public void LoadRewardedAd()
Debug.Log("Loading Ad: " + rewardAdUnitId);
Advertisement.Load(rewardAdUnitId, this);
public void ShowRewardedAd()
Advertisement.Show(rewardAdUnitId, this);
public void OnUnityAdsAdLoaded(string adUnitId)
if (adUnitId.Equals(rewardAdUnitId))
// Configure the button to call the ShowAd() method when clicked:
//_showAdButton.onClick.AddListener(ShowRewardedAd);
// Enable the button for users to click:
//_showAdButton.interactable = true;
Debug.Log("RewardedAds Loaded");
public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
if (adUnitId.Equals(rewardAdUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
Debug.Log("Unity Ads Rewarded Ad Completed");
_showAdButton.onClick.RemoveAllListeners(); //with this line of code the problem is solved but shows the NullReference.
Advertisement.Load(rewardAdUnitId, this);
public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
if (adUnitId.Equals(rewardAdUnitId))
Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
if (adUnitId.Equals(rewardAdUnitId))
Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
public void OnUnityAdsShowStart(string adUnitId) { }
public void OnUnityAdsShowClick(string adUnitId) { }
// Clean up the button listeners:
//_showAdButton.onClick.RemoveAllListeners();