Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help me with this snippet

Discussion in 'Unity Ads & User Acquisition' started by cephalo2, Apr 22, 2019.

  1. cephalo2

    cephalo2

    Joined:
    Feb 25, 2016
    Posts:
    262
    The following code has a flaw, in that if the device is off line, it never ends.

    Code (CSharp):
    1.     public void PlayInterstitialVideo()
    2.     {
    3.         if (Monetization.isSupported)
    4.         {
    5.             StartCoroutine(WaitForInterstitial());
    6.         }
    7.     }
    8.  
    9.     IEnumerator WaitForInterstitial()
    10.     {
    11.         while (!Monetization.IsReady(video))
    12.         {
    13.             yield return new WaitForSeconds(0.25f);
    14.         }
    15.  
    16.         ShowAdPlacementContent ad = null;
    17.         ad = Monetization.GetPlacementContent(video) as ShowAdPlacementContent;
    18.  
    19.         if (ad != null)
    20.         {
    21.             ad.Show(InterstitialAdFinishedCallback);
    22.             adTimer = 0.0f;
    23.         }
    24.  
    25.     }
    26.  
    Monetization.IsReady will always return false if the device is offline, so it waits forever, and AdFinishedCallback is never called. I'd like to call the callback myself if the device is offline and return a failed result maybe.

    How can I make this better? Instead of hanging the game like I am doing, I would like to respond appropriately.