Search Unity

Unity Ads without internet connection?

Discussion in 'Unity Ads & User Acquisition' started by Leyren, May 4, 2016.

  1. Leyren

    Leyren

    Joined:
    Mar 23, 2016
    Posts:
    29
    I saw this easy implementation to integrate Unity Ads in my project (Sorry, didn't find an option to insert code):

    Code (CSharp):
    1.   private IEnumerator PlayAd()
    2.   {
    3.      while (!Advertisement.isInitialized || !Advertisement.IsReady())
    4.      {
    5.         yield return new WaitForSeconds(0.5f);
    6.      }
    7.      Advertisement.Show();
    8.   }
    Will this cause an endless loop when the user has no connection to the internet? I mean, without internet connection, the ad can never be ready, thus I am stuck inside the while loop. If I am wrong, please explain why. :)

    Thanks
     
    Last edited: May 6, 2016
  2. unity-nikkolai

    unity-nikkolai

    Joined:
    Sep 18, 2014
    Posts:
    540
    Yes, you have the potential for an endless loop here. I'd recommend utilizing the OnDisable and OnDestroy MonoBehaviour methods to StopAllCoroutines running on the script.

    You can also implement a timeout, exiting the loop if unable to show after a length of time.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Advertisements;
    4.  
    5. public class UnityAdsExample : MonoBehaviour
    6. {
    7.     public string zoneId; // Specify a placement ID, or leave empty to use the default placement.
    8.  
    9.     public float initTimeout = 15f; // Time in seconds to allow for initialization.
    10.     public float showTimeout = 15f; // Time in seconds to allow ads to be ready.
    11.  
    12.     private float _yieldTime = 0.5f; // Time in seconds to wait between attempts.
    13.  
    14.     private IEnumerator ShowAdWhenReady ()
    15.     {
    16.         float startTime = Time.timeSinceLevelLoad;
    17.  
    18.         if (!Advertisement.isSupported) yield break;
    19.  
    20.         while (!Advertisement.isInitialized)
    21.         {
    22.             if (Time.timeSinceLevelLoad - startTime > initTimeout)
    23.             {
    24.                 Debug.LogWarning("Unity Ads failed to initialize in a timely manner.");
    25.                 yield break;
    26.             }
    27.  
    28.             yield return new WaitForSeconds(_yieldTime);
    29.         }
    30.  
    31.         Debug.Log("Unity Ads has finished initializing. Waiting for ads to be ready...");
    32.  
    33.         startTime = Time.timeSinceLevelLoad;
    34.  
    35.         while (!Advertisement.IsReady(zoneId))
    36.         {
    37.             if (Time.timeSinceLevelLoad - startTime > showTimeout)
    38.             {
    39.                 Debug.LogWarning("Unity Ads failed to be ready in a timely manner.");
    40.                 yield break;
    41.             }
    42.  
    43.             yield return new WaitForSeconds(_yieldTime);
    44.         }
    45.  
    46.         Debug.Log("Ads are available and ready.");
    47.  
    48.         Advertisement.Show(zoneId);
    49.     }
    50. }
    51.  
     
    Salazar likes this.
  3. Leyren

    Leyren

    Joined:
    Mar 23, 2016
    Posts:
    29
    Thanks, looks great! I didn't know anything about the yield keyword before, and therefore simple integration code was a bit irritating (as I said I wasn't sure if this produces an endless loop or yield has some special behavior on that).