Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UnityAds won't initialize if app started offline

Discussion in 'Unity Ads & User Acquisition' started by punppis, Sep 8, 2015.

  1. punppis

    punppis

    Joined:
    Sep 5, 2014
    Posts:
    2
    I'm using latest version of Unity Ads (1.3) with Unity 5 and I can't get ads working if app was started offline. Advertisement.Initialize() is getting called, but I always get ShowResult.Failed.

    Initialize() is called during game startup and at the start of several scenes to be sure UnityAds is initialized when user wants to watch an ad.

    I have tested on iOS and Android and the problem is with both platforms.

    It seems like if Initialize() is called once during offline, it never works after that. I am using this code and it works fine if started online.

    Code (CSharp):
    1.     static public void TryShowAd(System.Action<ShowResult> callback)
    2.     {
    3.         if (Advertisement.isInitialized && Advertisement.IsReady())
    4.         {
    5.             Advertisement.Show(null, new ShowOptions
    6.             {
    7.                 resultCallback = callback
    8.             });
    9.         }
    10.         else
    11.         {
    12.             if (Advertisement.isSupported)
    13.             {
    14.                 // this block is executed and text below is printed to log
    15.                 Debug.Log("UnityAds initialize");
    16.                 Advertisement.Initialize(GetGameId());
    17.             }
    18.             else
    19.             {
    20.                 Debug.Log("Platform not supported");
    21.             }
    22.             callback(ShowResult.Failed);
    23.         }
    24.     }
    Also, it would be great to get notified via callback when UnityAds is initialized.
     
    Last edited: Sep 8, 2015
  2. TheAdamastor

    TheAdamastor

    Joined:
    Aug 31, 2014
    Posts:
    5
    I'm having the same problem. I found this thread from a while back: http://forum.unity3d.com/threads/what-to-do-when-network-is-unavailable.284238/

    The IsReady() solution he ran into doesn't seem to apply anymore. I'm using the UnityAdsHelper code as well.

    I gave it a shot on Crossy Road and they somehow handle the initializing after first being offline.

    I've also tried updating to the Unity Ads SDK 1.5.0 and still can't get it to initialize after starting offline then going online.
     
    Last edited: Sep 9, 2015
  3. punppis

    punppis

    Joined:
    Sep 5, 2014
    Posts:
    2
    I got it working by checking internet connection before trying to initialize UnityAds. Basically Advertisement.Initialize() is not called ever if internet connection is not proven to be active. I'm doing this by comparing content of website with hardcoded string to eliminate WiFi networks with custom login sites and such.

    This works but there is some overhead because you have to wait for the internet check before even starting to init UnityAds.
     
  4. rasmus-unity

    rasmus-unity

    Unity Technologies

    Joined:
    Aug 15, 2014
    Posts:
    1,312
    Got it, and I agree with you that it should wait for internet connection to become available. Will see how we can get this implemented in a future version of the SDK.

    Thanks for feedback.

    /Rasmus
     
  5. supdawg

    supdawg

    Joined:
    Jul 8, 2012
    Posts:
    11
    Hi, prior to 5.2 I was implementing a ping check before initialisation and it worked great. However now Unity Ads is integrated initialisation seems to have been taken out of our hands, therefore this method is no longer feasible? Or have i missed something?

    Edit: Advertisement.Initialize now has the description "You should only call this method if you have disabled Unity Ads in Connect Window", which I guess means the Unity Services Window. So basically you cannot control initialisation from integrated Ads and will have to use the plugin. Fair enough.
     
    Last edited: Sep 10, 2015
  6. illusionpt

    illusionpt

    Joined:
    Feb 26, 2014
    Posts:
    4
    Hi,

    I'm using Unity Ads in 5.2 (integrated) and I'm having the same problem. Will this be fixed in next patch?
     
  7. actraiser3

    actraiser3

    Joined:
    Sep 11, 2014
    Posts:
    28
    I described a workaround here to be able to manually initialize Unity Ads in 5.2:
    http://forum.unity3d.com/threads/issues-in-migrating-to-5-2.353849/#post-2295957

    I hope Unity Devs make the variable in question public in the Service Menu though, so editing the actual asset to prevent automatic initialization is not required anymore.

    -act
     
    supdawg likes this.
  8. illusionpt

    illusionpt

    Joined:
    Feb 26, 2014
    Posts:
    4
    Thank you actraiser3, that actually worked.
     
  9. pareshkh

    pareshkh

    Joined:
    Feb 5, 2018
    Posts:
    3

    this issue is still remain in current sdk version. plz do something as early as possible.
     
  10. DenisasK

    DenisasK

    Unity Technologies

    Joined:
    Oct 13, 2016
    Posts:
    89
    Hello @pareshkh thanks for your feedback. We have fixed fixed it in the latest versions of the SDK. In which version are you able to reproduce it?
     
  11. pareshkh

    pareshkh

    Joined:
    Feb 5, 2018
    Posts:
    3
    thank @denisas for quick reply. but issue is still remain. after update on current version.
     
  12. pareshkh

    pareshkh

    Joined:
    Feb 5, 2018
    Posts:
    3
    my test ads code.


    public class TestAdsScript : MonoBehaviour {

    public string adsId = "MY_APS_ID";
    public bool connected;

    void Start()
    {
    StartCoroutine(tryToConnect());
    }

    IEnumerator tryToConnect()
    {
    //wait for networkconnection
    while(Application.internetReachability == NetworkReachability.NotReachable)
    yield return null;

    //after device reach network. wait for 5sec for ensure initialize proper.
    Invoke("inst", 5);
    }

    void inst()
    {
    // after double check network connection finally ini. id.
    Advertisement.Initialize(adsId, true);


    StartCoroutine(ShowAdWhenReady());
    }


    IEnumerator ShowAdWhenReady()
    {
    while (!Advertisement.IsReady())
    yield return null;

    Advertisement.Show();
    }
    }
     
  13. DenisasK

    DenisasK

    Unity Technologies

    Joined:
    Oct 13, 2016
    Posts:
    89
    Hello, @pareshkh thank for your code snippet. After the fix, you do not need manually listen for a connection. Please, try to Initialize on start and let Ads SDK handle it.
     
  14. RoyalScape

    RoyalScape

    Joined:
    Jul 14, 2017
    Posts:
    1
    I am using unity ads in my android game. When the internet is connected from starting of the game then the ads are shown and when internet is not connected from starting and then started in-between the game then ads are shown. But the problem is if the internet connection is lost in-between the game and regain then the ads are not shown. Please help me i am struggling with it.
     
  15. coolamigo

    coolamigo

    Joined:
    Mar 28, 2020
    Posts:
    25
    In my recent project and newly installed Unity and other tools, I am getting these errors whenever internet is turned off.

    Invalid configuration request for gameId: 3561129
    System.Threading._ThreadPoolWaitCallback:performWaitCallback()

    Error: NameResolutionFailure
    System.Threading._ThreadPoolWaitCallback:performWaitCallback()

    Please help as I need to upload ASAP
    I am using this code

    string gameId = "3561129";
    string placement = "rewardedVideo";
    bool testMode = true;
    void Start()
    {

    Advertisement.AddListener(this);
    Advertisement.Initialize(gameId, testMode);
    }

    public void rewardAdClicked()
    {
    flagAd = true;
    Advertisement.Show(placement);
    }

    public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    {
    MovControl.lives = 1;
    }
     
  16. coolamigo

    coolamigo

    Joined:
    Mar 28, 2020
    Posts:
    25
    Someone needs to reply this, I am not getting this information elsewhere
     
  17. kyle-unity

    kyle-unity

    Unity Technologies

    Joined:
    Jan 6, 2020
    Posts:
    336
    @coolamigo Which version of the SDK are you using? If the SDK is initialised and fails due to a connection issue, it is supposed to retry until it finds a valid connection.
     
  18. coolamigo

    coolamigo

    Joined:
    Mar 28, 2020
    Posts:
    25
  19. kyle-unity

    kyle-unity

    Unity Technologies

    Joined:
    Jan 6, 2020
    Posts:
    336
    @coolamigo The latest version of the Ads SDK is 3.4.4 so I think you might have accidentally gotten the wrong version number there.

    Have you installed the SDK via the package manager or asset store? If you are not already on the latest version I recommend you upgrade.
     
  20. coolamigo

    coolamigo

    Joined:
    Mar 28, 2020
    Posts:
    25
    I think I tried both, this is my first project actually, could someone tell me how I check the version
     
  21. kyle-unity

    kyle-unity

    Unity Technologies

    Joined:
    Jan 6, 2020
    Posts:
    336
  22. SeriouslyNot

    SeriouslyNot

    Joined:
    Nov 24, 2017
    Posts:
    121

    @kr-unity

    I'm using the latest version and the problem still exists!

    Reproduce:

    1- Start the game with no internet connection.
    2- Enable internet connection in the device.
    3- Click the ad button to show ad.
    4- Nothing happens --> the ad fails.
    5- Call Advertisement.Initialize (GameId, TestMode) again --> same error!

    This is my code:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4. using UnityEngine.Advertisements;
    5.  
    6. public class AdsManager : MonoBehaviour, IUnityAdsListener
    7. {
    8.     public static event Action OnAdCompleted = delegate {};
    9.     public static event Action OnContinueAdCompleted = delegate {};
    10.     public static event Action OnFreeGiftAdCompleted = delegate {};
    11.  
    12.     #if UNITY_IOS
    13.     private const string GameId = "11111111";
    14.     #elif UNITY_ANDROID
    15.     private const string GameId = "11111112";
    16.     #endif
    17.  
    18.  
    19.     private const string ChallengeCompleteVideo = "video";
    20.     private const string ContinueRewardedVideo = "rewardedVideo";
    21.     private const string FreeGiftRewardedVideo = "freeGiftRewardedVideo";
    22.     private const string BannerAd = "bannerPlacement";
    23.     private const bool TestMode = true;
    24.  
    25.     private static readonly string[] AdTypes = {ChallengeCompleteVideo, ContinueRewardedVideo, FreeGiftRewardedVideo, BannerAd};
    26.  
    27.     public static AdsManager instance;
    28.  
    29.     public enum AdType
    30.     {
    31.         ChallengeCompleteVideo = 0,
    32.         ContinueRewardedVideo = 1,
    33.         FreeGiftRewardedVideo = 2,
    34.         BannerAd = 3
    35.     }
    36.  
    37.     private void Awake()
    38.     {
    39.         instance = this;
    40.     }
    41.  
    42.     private void Start () {
    43.         Advertisement.AddListener (this);
    44.         Debug.Log("Initializing Ads");
    45.         Advertisement.Initialize (GameId, TestMode);
    46.     }
    47.  
    48.     public static void ShowRewardedVideo(AdType type)
    49.     {
    50.         var placementId = AdTypes[(int)type];
    51.         // Check if UnityAds ready before calling Show method:
    52.         if (Advertisement.IsReady(placementId)) {
    53.             Advertisement.Show(placementId);
    54.         }
    55.         else {
    56.             Debug.Log("Free Gift video is not ready at the moment! Please try again later!");
    57.         }
    58.     }
    59.  
    60.     private static void HandleFreeGiftAd(ShowResult result)
    61.     {
    62.         switch (result)
    63.         {
    64.             case ShowResult.Finished:
    65.                 Debug.Log("Player finished watching the ad, reward them with free coins");
    66.                 OnFreeGiftAdCompleted?.Invoke();
    67.                 break;
    68.             case ShowResult.Skipped:
    69.                 Debug.Log("Player skipped the ad.");
    70.                 break;
    71.             case ShowResult.Failed:
    72.                 Debug.Log("Player failed to load the ad, maybe internet connection?");
    73.                 break;
    74.         }
    75.     }
    76.  
    77.     private static void HandleContinueAdResult(ShowResult result)
    78.     {
    79.         switch (result)
    80.         {
    81.             case ShowResult.Finished:
    82.                 Debug.Log("Player finished watching the ad, continue the game");
    83.                 //GameManager.Instance.allowContinue = false;
    84.                 OnContinueAdCompleted?.Invoke();
    85.                 break;
    86.             case ShowResult.Skipped:
    87.                 Debug.Log("Player skipped the ad.");
    88.                 break;
    89.             case ShowResult.Failed:
    90.                 Debug.Log("Player failed to load the ad, maybe internet connection?");
    91.                 break;
    92.         }
    93.     }
    94.  
    95.     public void OnUnityAdsDidError (string message) {
    96.         // Log the error.
    97.     }
    98.  
    99.     public void OnUnityAdsDidStart (string placementId) {
    100.         // Optional actions to take when the end-users triggers an ad.
    101.     }
    102.  
    103.     public void OnUnityAdsDidFinish (string placementId, ShowResult showResult) {
    104.         switch (placementId)
    105.         {
    106.             case ContinueRewardedVideo:
    107.                 HandleContinueAdResult(showResult);
    108.                 break;
    109.             case FreeGiftRewardedVideo:
    110.                 HandleFreeGiftAd(showResult);
    111.                 break;
    112.         }
    113.     }
    114.  
    115.  
    116.  
    117.     public void OnUnityAdsReady (string placementId) {
    118.         /*
    119.         // If the ready Placement is rewarded, show the ad:
    120.         if (placementId == FreeGiftRewardedVideo) {
    121.             // Optional actions to take when the placement becomes ready(For example, enable the rewarded ads button)
    122.         }
    123.         */
    124.     }
    125.  
    126.  
    127.  
    128.     // When the object that subscribes to ad events is destroyed, remove the listener:
    129.     public void OnDestroy() {
    130.         Advertisement.RemoveListener(this);
    131.     }
    132.  
    133.     private IEnumerator ShowBannerWhenInitialized () {
    134.         while (!Advertisement.isInitialized) {
    135.             yield return new WaitForSeconds(0.5f);
    136.         }
    137.         Advertisement.Banner.Show (BannerAd);
    138.     }
    139.  
    140.     private void OnBecameInvisible()
    141.     {
    142.         Advertisement.RemoveListener(this);
    143.     }
    144.  
    145.     private void OnBecameVisible()
    146.     {
    147.         Advertisement.AddListener(this);
    148.     }
    149. }
    This script is attached to an empty gameObject in the Scene.
     
  23. Hoorza

    Hoorza

    Joined:
    May 8, 2016
    Posts:
    40
    I can confirm that while Ads won't re-initialize in the Editor when you got internet access again, no matter how long you wait. However, it does work on mobile (android to be precise, not tested on iOS). After re-enabling the internet connection ads will be available again. That was done in test mode.

    Unity version: 2020.1.4f1
    Advertisement package version: 3.4.9



    Hope it helps.