Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Unity Ads Never Play if No Internet During Initialization

Discussion in 'Unity Ads' started by kumagostudios, Aug 10, 2018.

  1. kumagostudios


    Jul 29, 2017
    Hello, I'm having a problem with ads not showing. If a user switches the internet off at the start of my game and then on later, Unity Ads always fails. Strangely enough Advertisement.isIntialized always returns true, but
    Advertisement.Show(AD_KEY) will always fail. This allows users to just turn their internet off and back on once the game has started to circumvent all ads. Advertisement.IsReady() never returns true even when I call Advertisement.Initialize(<id>); once internet is switched back on.

    If internet was on the entire time, this issue never arises. Does anyone know how to fix this? Thank you.
  2. rasmus-unity


    Unity Technologies

    Aug 15, 2014
    Ads SDK will automatically fetch campaigns when internet is available again, however the model is that we increase the time between retries (to avoid DDoS'ing our servers, in case of general internet blackout cases). So just wait a bit, then it should work. There is no need to call Initialize() again. And you should check IsReady() instead of isInitialized to see if ads are available to be shown.

    And regarding not showing ads in this case, did you see e.g. on rewarded ads, which actually gives users an incentive to watch ads, as they get some sort of reward in game, so not "wasting" their time.

    kumagostudios likes this.
  3. kumagostudios


    Jul 29, 2017
    Thanks Rasmus, I was using IsReady() but it always returns false. What will trigger IsReady() to return true once internet is turned on? I've waited a LONG TIME after turning the internet back on but calling showAd still fails.

    Also and seem to show two different implementations in that one polls for ads while the other is called once. Would you be able to confirm which implementation is correct?
    Last edited: Aug 10, 2018
  4. kumagostudios


    Jul 29, 2017
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    3. using UnityEngine.Analytics;
    5. public sealed class AdManager
    6. {
    7.     // Instance
    8.     private static AdManager _instance;
    9.     public static AdManager Instance
    10.     {
    11.         get
    12.         {
    13.             if (_instance == null) _instance = new AdManager();
    14.             return _instance;
    15.         }
    16.     }
    18.     // Callback
    19.     public delegate void AdDelegate();
    20.     protected AdDelegate callback;
    22.     // Ad Configuration
    23.     private const string AD_KEY = "video";
    25.     #if UNITY_IOS
    26.     private const string GAME_ID = "xxx";
    27.     #elif UNITY_ANDROID
    28.     private const string GAME_ID = "xxx";
    29.     #endif
    31.     // Class Initialization
    32.     private AdManager()
    33.     {
    34.         Advertisement.Initialize(GAME_ID);
    35.     }
    37.     public void ShowAd(AdDelegate cb)
    38.     {
    39.         // If purchased remove ads, just run callback and skip rest
    40.         if (Preferences.item_removeAds > 0) {
    41.             Debug.Log ("Purchased Remove Ads.");
    42.             cb ();
    43.             return;
    44.         }
    46.         if (Advertisement.IsReady(AD_KEY)) {
    47.             callback = cb; // used in another function
    48.             Preferences.AdCount += 1;
    49.             Advertisement.Show(AD_KEY, new ShowOptions(){ resultCallback = HandleShowResult });
    51.             // Analytics: Ads
    52.             AnalyticsEvent.AdStart(false);
    53.         }
    54.     }
    56.     private void HandleShowResult(ShowResult result)
    57.     {
    58.         switch (result)
    59.         {
    60.         case ShowResult.Finished:
    61.             AnalyticsEvent.AdComplete(false);
    62.             break;
    63.         case ShowResult.Skipped:
    64.             AnalyticsEvent.AdSkip(false);
    65.             break;
    66.         case ShowResult.Failed:
    67.             Debug.LogError ("Ad launch failed (internet?)");
    68.             break;
    69.         }
    71.         callback();
    72.     }
    73. }
    Here is my code if it helps.