Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question OnInitializationComplete Not working when game restarted.

Discussion in 'Unity Ads & User Acquisition' started by gHopperr, Jun 15, 2023.

  1. gHopperr

    gHopperr

    Joined:
    Jul 8, 2021
    Posts:
    1
    I followed the Unity Documentation for implementing Unity Ads to my mobile game. Here is my AdsInitializer script.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    3.  
    4. public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener
    5. {
    6.    
    7.  
    8.     [SerializeField] string _androidGameId = "5293323";
    9.     [SerializeField] string _iOSGameId = "5293322";
    10.     [SerializeField] bool _testMode = true;
    11.     private string _gameId;
    12.  
    13.     [SerializeField] RewardedAdsButton rewardedAdsButton;
    14.     [SerializeField] InterstitialAds interstitialAds;
    15.  
    16.  
    17.     void Awake()
    18.     {
    19.        
    20.             InitializeAds();
    21.  
    22.     }
    23.    
    24.     public void InitializeAds()
    25.     {
    26. #if UNITY_IOS
    27.             _gameId = _iOSGameId;
    28. #elif UNITY_ANDROID
    29.         _gameId = _androidGameId;
    30. #elif UNITY_EDITOR
    31.             _gameId = _androidGameId; //Only for testing the functionality in the Editor
    32. #endif
    33.         if (!Advertisement.isInitialized && Advertisement.isSupported)
    34.         {
    35.             Advertisement.Initialize(_gameId, _testMode, this);
    36.         }
    37.      
    38.     }
    39.  
    40.  
    41.     public void OnInitializationComplete()
    42.     {
    43.         Debug.Log("Unity Ads initialization complete.");
    44.        if(rewardedAdsButton != null) rewardedAdsButton.LoadAd();
    45.        if(interstitialAds != null) interstitialAds.LoadAd();
    46.     }
    47.  
    48.     public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    49.     {
    50.         Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
    51.         if (Application.internetReachability != NetworkReachability.NotReachable)
    52.         {
    53.             InitializeAds();
    54.         }
    55.     }
    56.     public void RestartAds()
    57.     {
    58.         if (Advertisement.isInitialized)
    59.         {
    60.             Advertisement.Initialize(_gameId, _testMode, this);
    61.         }
    62.     }
    63.  
    64. }
    It's simply initializing the ads and loading ads on OnInitializationComplete().

    and here is my RewardedAdsButton script
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Advertisements;
    4. using TMPro;
    5. public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
    6. {
    7.     public BallController ballController;
    8.     [SerializeField] GameObject cooldownTimer;
    9.     [SerializeField] TextMeshProUGUI totalCoinText;
    10.  
    11.     [SerializeField] Button _showAdButton;
    12.     [SerializeField] string _androidAdUnitId = "Rewarded_for_android";
    13.     [SerializeField] string _iOSAdUnitId = "Rewarded_for_ios";
    14.     int rewardedID = 0;
    15.     string _adUnitId = null; // This will remain null for unsupported platforms
    16.  
    17.     void Start()
    18.     {
    19.         GameObject ballGameObject = GameObject.Find("Ball");
    20.         if (ballGameObject != null)
    21.         {
    22.             ballController = ballGameObject.GetComponent<BallController>();
    23.         }
    24.     }
    25.         void Awake()
    26.     {
    27.         // Get the Ad Unit ID for the current platform:
    28. #if UNITY_IOS
    29.         _adUnitId = _iOSAdUnitId;
    30. #elif UNITY_ANDROID
    31.         _adUnitId = _androidAdUnitId;
    32. #endif
    33.  
    34.         // Disable the button until the ad is ready to show:
    35.      
    36.         _showAdButton.interactable = false;
    37.     }
    38.     public void setRewardedId(int x)
    39.     {
    40.         rewardedID = x;
    41.     }
    42.     // Call this public method when you want to get an ad ready to show.
    43.     public void LoadAd()
    44.     {
    45.         // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
    46.         Debug.Log("Loading Ad: " + _adUnitId);
    47.         Advertisement.Load(_adUnitId, this);
    48.     }
    49.  
    50.     // If the ad successfully loads, add a listener to the button and enable it:
    51.     public void OnUnityAdsAdLoaded(string adUnitId)
    52.     {
    53.         Debug.Log("Ad Loaded: " + adUnitId);
    54.  
    55.         if (adUnitId.Equals(_adUnitId))
    56.         {
    57.             // Configure the button to call the ShowAd() method when clicked:
    58.             _showAdButton.onClick.AddListener(ShowAd);
    59.             // Enable the button for users to click:
    60.             _showAdButton.interactable = true;
    61.         }
    62.     }
    63.  
    64.     // Implement a method to execute when the user clicks the button:
    65.     public void ShowAd()
    66.     {
    67.         // Disable the button:
    68.         _showAdButton.interactable = false;
    69.         // Then show the ad:
    70.         Advertisement.Show(_adUnitId, this);
    71.     }
    72.  
    73.     // Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
    74.     public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
    75.     {
    76.         if ( rewardedID==1 && adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
    77.         {
    78.             Debug.Log("Unity Ads Rewarded Ad Completed");
    79.             // Grant a reward.
    80.             ballController.ReviveTheBall();
    81.             LoadAd();
    82.  
    83.         }
    84.         if (rewardedID == 2 && adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
    85.         {
    86.             Debug.Log("Unity Ads Rewarded Ad Completed");
    87.             cooldownTimer.SetActive(true);
    88.             PlayerPrefs.SetInt("currentCoin", (PlayerPrefs.GetInt("currentCoin") + 25));
    89.             if (totalCoinText != null)
    90.             {
    91.                 totalCoinText.text = PlayerPrefs.GetInt("currentCoin").ToString();
    92.             }
    93.         }
    94.     }
    95.  
    96.     // Implement Load and Show Listener error callbacks:
    97.     public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
    98.     {
    99.         Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
    100.         // Use the error details to determine whether to try to load another ad.
    101.     }
    102.  
    103.     public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
    104.     {
    105.         Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
    106.         // Use the error details to determine whether to try to load another ad.
    107.     }
    108.  
    109.     public void OnUnityAdsShowStart(string adUnitId) { }
    110.     public void OnUnityAdsShowClick(string adUnitId) { }
    111.  
    112.     void OnDestroy()
    113.     {
    114.         // Clean up the button listeners:
    115.         _showAdButton.onClick.RemoveAllListeners();
    116.     }
    117. }
    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.
     
  2. Yasuyuki

    Yasuyuki

    Unity Technologies

    Joined:
    Sep 11, 2014
    Posts:
    163
    By quick look at your code, the ads won't load when reloading the scene if Unity Ads SDK has already been initialized. (isInitialized=true)
    Once the SDK is initialized, it will not be required to initialize again. It will keep during the game session.