Search Unity

Question How to fix this Interstitial Ad and Rewarded Ad show only one Time in my Unity Mediation?

Discussion in 'Unity Mediation' started by TATEY-APPS, May 5, 2022.

  1. TATEY-APPS

    TATEY-APPS

    Joined:
    Nov 19, 2021
    Posts:
    7
    Dear All,

    I am newbie developer. I can integrate UnityAds successfully into my game. However, I can't finish successfully with Unity Meditation Integration.

    When I test Interstitial Ad and Rewarded Ad, they show TestAd only onetime. I can't find the issue. I really grateful if you can lighten my path, which i can move on. Below are com full code.

    using System;
    using UnityEngine;
    using Unity.Services.Core;
    using Unity.Services.Mediation;

    public class UnityMediationManager : MonoBehaviour
    {
    public static UnityMediationManager instance;
    public string interstitialAdUnitId = "Interstitial_Android";
    IInterstitialAd interstitialAd;
    public string rewardedAdUnitId = "Rewarded_Android";
    IRewardedAd rewardedAd;
    public string gameID = "4538621";



    private void Awake()
    {
    if(instance == null)
    {
    instance = this;
    }
    }
    async void Start()
    {
    InitializationOptions options = new InitializationOptions();
    options.SetGameId("4538621");
    // Initialize the package to access API
    await UnityServices.InitializeAsync();

    // Instantiate an interstitial ad object with platform-specific Ad Unit ID
    if (Application.platform == RuntimePlatform.Android)
    {
    interstitialAd = MediationService.Instance.CreateInterstitialAd(interstitialAdUnitId);
    rewardedAd = MediationService.Instance.CreateRewardedAd(rewardedAdUnitId);
    }
    else if (Application.platform == RuntimePlatform.IPhonePlayer)
    {
    //interstitialAd = MediationService.Instance.CreateInterstitialAd(iosAdUnitId);
    }
    #if UNITY_EDITOR
    else
    {
    interstitialAd = MediationService.Instance.CreateInterstitialAd(interstitialAdUnitId);
    rewardedAd = MediationService.Instance.CreateRewardedAd(rewardedAdUnitId);
    }
    #endif

    // Subscribe callback methods to load events:
    interstitialAd.OnLoaded += AdLoaded;
    interstitialAd.OnFailedLoad += AdFailedToLoad;
    rewardedAd.OnLoaded += AdLoaded;
    rewardedAd.OnFailedLoad += AdFailedToLoad;

    // Subscribe callback methods to show events:
    interstitialAd.OnShowed += AdShown;
    interstitialAd.OnFailedShow += AdFailedToShow;
    interstitialAd.OnClosed += AdClosed;
    interstitialAd.Load();
    rewardedAd.OnShowed += AdShown;
    rewardedAd.OnFailedShow += AdFailedToShow;
    rewardedAd.OnUserRewarded += UserRewarded;
    rewardedAd.OnClosed += AdClosed;
    rewardedAd.Load();
    }

    // Implement load event callback methods:
    void AdLoaded(object sender, EventArgs args)
    {
    Debug.Log("Ad loaded.");
    // Execute logic for when the ad has loaded
    }

    void AdFailedToLoad(object sender, LoadErrorEventArgs args)
    {
    Debug.Log("Ad failed to load.");
    // Execute logic for the ad failing to load.
    }

    // Implement show event callback methods:
    void AdShown(object sender, EventArgs args)
    {
    Debug.Log("Ad shown successfully.");
    // Execute logic for the ad showing successfully.
    }
    void UserRewarded(object sender, RewardEventArgs args)
    {
    Debug.Log("Ad has rewarded user.");
    // Execute logic for rewarding the user.

    //Rewarded 100 Coins to User
    print("video close and User received Rewarded of 100 Coins");
    print("Coins " + PlayerPrefs.GetInt(ShopSystem.Coins));
    PlayerPrefs.SetInt(ShopSystem.Coins, PlayerPrefs.GetInt(ShopSystem.Coins) + 100);
    }

    void AdFailedToShow(object sender, ShowErrorEventArgs args)
    {
    Debug.Log("Ad failed to show.");
    // Execute logic for the ad failing to show.
    }
    void ImpressionEvent(object sender, ImpressionEventArgs args)
    {
    var impressionData = args.ImpressionData != null ? JsonUtility.ToJson(args.ImpressionData, true) : "null";
    Debug.Log("Impression event from ad unit id " + args.AdUnitId + " " + impressionData);
    }

    private void AdClosed(object sender, EventArgs e)
    {
    Debug.Log("Ad has closed");
    // Execute logic after an ad has been closed.
    }

    public void ShowInterstitialAd()
    {
    // Ensure the ad has loaded, then show it.
    if (interstitialAd.AdState == AdState.Loaded)
    {
    interstitialAd.Show();
    }
    }
    public void ShowRewardedAd()
    {
    // Ensure the ad has loaded, then show it.
    if (rewardedAd.AdState == AdState.Loaded)
    {
    rewardedAd.Show();
    }
    }

    }
     

    Attached Files:

  2. Fr3driiik

    Fr3driiik

    Joined:
    Feb 20, 2022
    Posts:
    4
    You need to load new ads so they are ready to be displayed:
    Code (CSharp):
    1. rewardedAd.Load()
    2. interstitialAd.Load()
    You only do this once currently in Start() when you subscribe to events. A good idea is probably to Load a new ad right after the current ad is closed.
    Note that ads aren't ready to be displayed immediatly after .Load(). .Load() will prepare an ad so it can be displayed later.
    In ShowRewardedAd() you check the AdState if it is loaded. Look at what other options are available in the AdState enum and you might understand a bit more. :)
     
  3. TATEY-APPS

    TATEY-APPS

    Joined:
    Nov 19, 2021
    Posts:
    7
    Dear Fr3Driiik,

    Many thanks for help . Would you mind to edit in my code ? So i can learn with real practice by see your edited code. I am new self-learner; i can't not go ahead with your recommendation. My gratitude for you. Look to see the edited code from you. Many Thanks
     
    PsijicNine likes this.
  4. Fr3driiik

    Fr3driiik

    Joined:
    Feb 20, 2022
    Posts:
    4
    Hmm you just need to add the 2 lines that I wrote to the AdClosed function. :)
    It will give you a "working" example but, as I said, you want the ad to completely load before you call the ShowRewardedAd / ShowInterstitialAd funtions.
    You can look at what is logged to the console to maybe understand in what order things happen and your code gets executed.

    It looks like you have taken the generated ad code and accidentaly removed the ad.Load().
     
    Last edited: May 7, 2022
    TATEY-APPS likes this.
  5. TATEY-APPS

    TATEY-APPS

    Joined:
    Nov 19, 2021
    Posts:
    7
    Dear Fr3Driiik,

    I would like to say Many Thanks You for your kindness to solve my issue. Now the Ads are showing perfectly. You have solved my issue, which it blocked for a month.

    I would like to attached my file in case someone having issue like me.

    My gratitude for you. I wish you best wishes.

    Sincerely yours,
    Seinita
     

    Attached Files:

    stellamocha and Fr3driiik like this.
  6. TATEY-APPS

    TATEY-APPS

    Joined:
    Nov 19, 2021
    Posts:
    7
    Also, i have one more issue : How to add GDPR Popup before game start. I don't know coding about this. I try to read UnityDocumentation, but i still don't know to do. I try o search Google and Youtube, but can't find the solution. My game cannot finish completely without DGPR popup. Would you mind to help me out? Look to hear from you as your convenience.

    Best regards
    Seinita
     
  7. DeclanMcPartlin

    DeclanMcPartlin

    Unity Technologies

    Joined:
    Nov 19, 2020
    Posts:
    146
    Hello @seinita-games,

    Thanks for reaching out. Unity Mediation doesn't currently provide a user interface (UI) to collect consent for GDPR or other privacy law consent. The options you have are:
    1. Do not collect consent directly. The underlying ad networks (Unity, Admob, etc.) will collect consent themselves.
    2. Provide your own UI. This can be a big task, be sure that your UI informs the user correctly to avoid any legal liabilities.
    3. Use a consent collection solution, for example Google's Funding Choices.
    We will let you know if an out of the box Unity Mediation solution will be available in the future.
     
  8. Fr3driiik

    Fr3driiik

    Joined:
    Feb 20, 2022
    Posts:
    4
    Glad I could help :)
     
    TATEY-APPS likes this.