Home > front end >  Unity Ads 4.0.x- ads wont work on loading next scene or after reloading scene
Unity Ads 4.0.x- ads wont work on loading next scene or after reloading scene

Time:04-05

Well, I think my title states what the problem is.

DontDestroyOnLoad(); is pointless to use, because "bs" objects are different in the scenes, and AD Initialization should be called once. I get no error in the console. Initialization works as expected. In summary, everything works as it should, when I enter the new scene Initialization works as expected but when I click the button for reward nothing happens. The same happens after Reloading the scene.

What should I do next?

using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.UI;

public class AD_Manager : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
{
    [SerializeField] string _androidGameId = "12345678";
    [SerializeField] string _iOSGameId = "87654321";
    [SerializeField] bool _testMode = true;
    private string _gameId;

    [SerializeField] Button _showAdButton; //You can remove this if want to add the function manually from OnClick()
    [SerializeField] string _androidAdUnitId = "Rewarded_Android";
    [SerializeField] string _iOSAdUnitId = "Rewarded_iOS";
    string _adUnitId = null; // This will remain null for unsupported platforms

    private bool showAd = false;
    private bool onInitializationFailedToLoad = false;
    private bool onAdFailToLoad = false;
    // private bool networkIsNotActive = false;
    [SerializeField] GameObject errorPanel;
    //    [SerializeField] GameObject errorPanel02;
    [SerializeField] GameObject mainPanel;
    [SerializeField] GameObject bs01;//bs stands for bullshit
    [SerializeField] GameObject bs02;//ah yes, shit coming with they own parts ......
    [SerializeField] GameObject bs03;//...
    [SerializeField] GameObject bs04;//      . . . 
    [SerializeField] GameObject bs05;//                :|
    private string m_ReachabilityText;


    void Awake()
    {
        
        InitializeAds();
#if UNITY_EDITOR
        Debug.Log("Awake");
#endif
        _adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
            ? _iOSAdUnitId
            : _androidAdUnitId;
#if UNITY_EDITOR
        Debug.Log("the _adUnitId is: "   _adUnitId);
#endif
    }

    private void Update()
    {
        //Output the network reachability to the console window
#if UNITY_EDITOR
        Debug.Log("Internet : "   m_ReachabilityText);
#endif
        //Check if the device cannot reach the internet
        if (Application.internetReachability == NetworkReachability.NotReachable)
        {
            //Change the Text --- nope :)
            onAdFailToLoad = true;
            m_ReachabilityText = "Not Reachable.";
        }
        else
        {
            onAdFailToLoad = false;
            m_ReachabilityText = "Is Reachable.";
        }

    }



    public void Quit()
    {
        Application.Quit();
#if UNITY_EDITOR
        Debug.Log("Quit executed!");
#endif
    }

    public void EmptyCoins()
    {
        ScoreTextScript.coinAmount = 0;
    }

    public void InitializeAds()
    {
        _gameId = (Application.platform == RuntimePlatform.IPhonePlayer)
            ? _iOSGameId
            : _androidGameId;
        Advertisement.Initialize(_gameId, _testMode, this);

    }

    public void OnInitializationComplete()
    {
        
#if UNITY_EDITOR
        Debug.Log("Unity Ads initialization complete.");
#endif
        onInitializationFailedToLoad = false;
        LoadAd();
    }

    public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    {
        onInitializationFailedToLoad = true;
#if UNITY_EDITOR     
        Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
#endif
    }

    public void LoadAd()
    {
        // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled on top of the script).
#if UNITY_EDITOR
        Debug.Log("Loading Ad: "   _adUnitId);
#endif
        Advertisement.Load(_adUnitId, this);
    }

    // If the ad successfully loads, add a listener to the button and enable it:
    public void OnUnityAdsAdLoaded(string adUnitId)
    {
#if UNITY_EDITOR
        Debug.Log("Ad Loaded: "   adUnitId);
#endif


        if (adUnitId.Equals(_adUnitId))
        {
            // Configure the button to call the ShowAd() method when clicked:
            _showAdButton.onClick.AddListener(ShowAd); //You can remove this if want to add the function manually from OnClick()
            // Enable the button for users to click:
            _showAdButton.interactable = true; //You can remove this if want to add the function manually from OnClick()
        }
    }

    // Implement a method to execute when the user clicks the button:
    public void ShowAd()
    {
        if (showAd == false)
        {

#if UNITY_EDITOR
            Debug.Log("Showing Ad");
#endif
            // Disable the button:
            _showAdButton.interactable = false; //You can remove this if want to add the function manually from OnClick()
            // Then show the ad:
            Advertisement.Show(_adUnitId, this);
            _showAdButton.onClick.RemoveAllListeners(); //You can remove this if want to add the function manually from OnClick()
#if UNITY_EDITOR
            Debug.Log("All Listeners Removed");
#endif
            showAd = true;

        }
    }

    //Simply don't load AD if onInitializationFailed == true or if onAdFailToLoad == true or if newtwork is not Reachable
    public void DontShowAd()
    {


        if (onInitializationFailedToLoad == true || onAdFailToLoad == true)
        {
            mainPanel.SetActive(false);
            bs01.SetActive(false);
            bs02.SetActive(false);
            bs03.SetActive(false);
            bs04.SetActive(false);
            bs05.SetActive(false);
            errorPanel.SetActive(true);

        }
    }

    // Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
    public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
    {
        if (showAd == true)
        {
            if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
            {
#if UNITY_EDITOR
                Debug.Log("Unity Ads Rewarded Ad Completed");
#endif
                // Grant a reward.
                ScoreTextScript.coinAmount  = Random.Range(5, 25);
                // Load another ad:
                Advertisement.Load(_adUnitId, this);
                showAd = false;

            }
        }
    }

    public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
    {
#if UNITY_EDITOR
        Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
#endif
        onAdFailToLoad = true;
        // Use the error details to determine whether to try to load another ad.
    }

    public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
    {
#if UNITY_EDITOR
        Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
#endif
        onAdFailToLoad = true;
        // Use the error details to determine whether to try to load another ad.
    }

    public void OnUnityAdsShowStart(string adUnitId) { }
    public void OnUnityAdsShowClick(string adUnitId) { }




} // Class

CodePudding user response:

FIXED Code:

    void Awake()
    {
        
        InitializeAds();
#if UNITY_EDITOR
        Debug.Log("Awake");
#endif
        _adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
            ? _iOSAdUnitId
            : _androidAdUnitId;
#if UNITY_EDITOR
        Debug.Log("the _adUnitId is: "   _adUnitId);
#endif
        LoadAd();
    }
    public void LoadAd()
    {
        // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled on top of the script).
#if UNITY_EDITOR
        Debug.Log("Loading Ad: "   _adUnitId);
#endif
        Advertisement.Load(_adUnitId, this);
    }
  • Related